Skip to content

Instantly share code, notes, and snippets.

@b-b3rn4rd
Created January 16, 2019 23:52
Show Gist options
  • Save b-b3rn4rd/3bafcef9b5e3678730f579bd55c0aa55 to your computer and use it in GitHub Desktop.
Save b-b3rn4rd/3bafcef9b5e3678730f579bd55c0aa55 to your computer and use it in GitHub Desktop.
AWS lambda using RAM by sending HTTP API request without external dependencies
import os
import boto3
import json
import botocore.auth
import botocore.credentials
from botocore.awsrequest import AWSRequest
from botocore.endpoint import BotocoreHTTPSession
from botocore.auth import SigV4Auth
def lambda_handler(event, context):
account_id = context.invoked_function_arn.split(':')[4]
host = 'ram.{}.amazonaws.com'.format(os.environ['AWS_REGION'])
params = {
'name': 'sharename',
'resourceArns': ['arn:aws:ec2:{}:{}:transit-gateway/{}'.format(os.environ['AWS_REGION'], account_id, 'tgw-01234567890')],
'principals': ['123456789012']
}
headers = {'Host': host}
request = AWSRequest(method="POST", url="https://{}/createresourceshare".format(host), data=json.dumps(params), headers=headers)
SigV4Auth(boto3.Session().get_credentials(), "ram", os.environ['AWS_REGION']).add_auth(request)
session = BotocoreHTTPSession()
r = session.send(request.prepare())
if r.status_code >= 200 and r.status_code <= 299:
return json.loads(r._content)
else:
raise Exception(r.status_code, r._content)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment