Skip to content

Instantly share code, notes, and snippets.

@ejdoh1
Created February 14, 2019 04:41
Show Gist options
  • Save ejdoh1/e74ed6df7d94de5f7a6b2a567b6306b6 to your computer and use it in GitHub Desktop.
Save ejdoh1/e74ed6df7d94de5f7a6b2a567b6306b6 to your computer and use it in GitHub Desktop.
AWS Lambda function to call API Gateway with IAM auth
# Note: Allow Lambda action "execute-api:*" on your AWS API Gateway resources
import os
import requests
from requests_aws4auth import AWS4Auth
REGION = 'ap-southeast-2'
METHOD = 'POST'
SERVICE = 'execute-api'
DATA = {'key':'value'}
URL = "https://xyz.execute-api.ap-southeast-2.amazonaws.com/stage/method"
def handler(event,context):
aws_auth = AWS4Auth(
os.environ['AWS_ACCESS_KEY_ID'],
os.environ['AWS_SECRET_ACCESS_KEY'],
REGION,
SERVICE,
session_token=os.environ['AWS_SESSION_TOKEN']
)
r = requests.request(METHOD, URL,data=DATA,auth=aws_auth)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment