How to (properly) use external credentials in an AWS Lambda function?

2024/10/5 3:25:32

I have a (extremely basic but perfectly working) AWS lambda function written in Python that however has embedded credentials to connect to: 1) an external web service 2) a DynamoDB table.

What the function does is fairly basic: it POSTs a login against a service (with credentials #1) and then saves part of the response status into a DynamoDB table (with AWS credentials #2).

These are the relevant parts of the function:

h = httplib2.Http()
auth = base64.encodestring('myuser' + ':' + 'mysecretpassword')
(response, content) = h.request('https://vca.vmware.com/api/iam/login', 'POST', headers = {'Authorization':'Basic ' + auth,'Accept':'application/xml;version=5.7'})

and then

conn = boto.connect_dynamodb(aws_access_key_id='FAKEhhahahah',aws_secret_access_key='FAKEdhdhdudjjdjdjhdjjhdjdjjd')

How would you go about cleaning the code by NOT having these credentials inside the function?

FYI this function is scheduled to run every 5 minutes (there is no other external event that triggers it).

Answer

In your example you have 2 types of credentials:

  1. AWS creds
  2. None AWS creds

With AWS creds everything simple: create IAM Role, give it permission to dynamodb and you good to go.

With non AWS creds the most secure approach would be:

  1. Encrypt credentials upfront using kms service. (kms.encrypt('foo'))
  2. Once you have encrypted version of your information. Feel free to store it anywhere you want. Simplest way would be hard code it in lambda.
  3. Add permission to lambda IAM Role to decrypt information using kms key that you used in step 1.
  4. Then each time lambda is invoked, let it call kms to decrypt information.
https://en.xdnf.cn/q/70533.html

Related Q&A

How to set environment variable TF_Keras = 1 for onnx conversion?

Recently updated to tensorflow 2.0 and am having trouble getting my .h5 models into .onnx . Used to be a very simple procedure but now I am having an issue. When I run the following code:# onnx testing…

Django App Engine: AttributeError: AnonymousUser object has no attribute backend

I am using djangoappengine. When I try create a new user, authenticate that user, and log them in, I get the following error AttributeError: AnonymousUser object has no attribute backend.My code is sim…

python identity dictionary [duplicate]

This question already has answers here:Closed 12 years ago.Possible Duplicate:How to make a python dictionary that returns key for keys missing from the dictionary instead of raising KeyError? I need…

Whats a good library to manipulate Apache2 config files? [closed]

Closed. This question is seeking recommendations for books, tools, software libraries, and more. It does not meet Stack Overflow guidelines. It is not currently accepting answers.We don’t allow questi…

AttributeError: module object has no attribute webdriver

AttributeError: module object has no attribute webdriverwhy this error happen when write import selenium and when write code like this no error happenfrom selenium import webdriver

Mask Ocean or Land from data using Cartopy

I would like to mask the Land area from Sea Surface Temperature Data over the globe. I am using Cartopy to plot the data.import numpy as np import matplotlib.pyplot as plt import cartopy.crs as ccrs fr…

How to map func_closure entries to variable names?

I have a lambda object that is created in this function:def add_url_rule(self, rule, endpoint=None, view_func=None, **options):self.record(lambda s:s.add_url_rule(rule, endpoint, view_func, **options))…

How to use the convertScaleAbs() function in OpenCV?

I am trying to convert an image back to grayscale after applying Sobel filtering on it. I have the following code: import numpy as np import matplotlib.pyplot as plt import cv2image = cv2.imread("…

Register a Hello World DBus service, object and method using Python

Im trying to export a DBus service named com.example.HelloWorld, with an object /com/example/HelloWorld, and method com.example.HelloWorld.SayHello that prints "hello, world" if the method i…

Python 3 Timedelta OverflowError

I have a large database that I am loading into an in-memory cache. I have a process that does this iterating through the data day by day. Recently this process has started throwing the following error:…