Skip to content

Instantly share code, notes, and snippets.

@singledigit
Last active February 24, 2023 06:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save singledigit/f01b0b1c7f979dee0c5ea5818c3e5799 to your computer and use it in GitHub Desktop.
Save singledigit/f01b0b1c7f979dee0c5ea5818c3e5799 to your computer and use it in GitHub Desktop.
boto sigv4 in python with Lambda
import boto3
import json
import requests
from requests_aws4auth import AWS4Auth
def lambda_handler(event, context):
signature = get_signature()
return fetch(signature)
def get_signature():
session = boto3.Session()
credentials = session.get_credentials()
sigv4auth = AWS4Auth(credentials.access_key, credentials.secret_key,
session.region_name, 'execute-api', session_token=credentials.token)
return sigv4auth
def fetch(signature):
url = <api gateway url>
try:
response = requests.request("GET", url, data={}, auth=signature, headers={
'Content-Type': "application/x-amz-json-1.1"})
json_data = json.loads(response.text)
return json_data
except Exception as e:
print(e)
@singledigit
Copy link
Author

Ensure the Lambda functions role has the rights to execute the API

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment