Last active
February 24, 2023 06:04
-
-
Save singledigit/f01b0b1c7f979dee0c5ea5818c3e5799 to your computer and use it in GitHub Desktop.
boto sigv4 in python with Lambda
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Ensure the Lambda functions role has the rights to execute the API