Skip to content

Instantly share code, notes, and snippets.

@seansummers
Last active February 19, 2021 22:39
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 seansummers/c534f705f25ca837aea6c82383dc6655 to your computer and use it in GitHub Desktop.
Save seansummers/c534f705f25ca837aea6c82383dc6655 to your computer and use it in GitHub Desktop.
Lambda Gists
import secrets
import boto3
import cfnresponse
ec2 = boto3.resource('ec2')
def create_ami(instance_id, ami_name, description):
instance = ec2.Instance(instance_id)
instance.stop()
instance.wait_until_stopped()
image = instance.create_image(Name=ami_name, Description=description)
image.wait_until_exists()
return {'ami': image.id}
def deregister_ami(image_id):
image = ec2.Image(image_id)
image.deregister()
def handler(event, context=None):
description = event.get('Description', 'Created with Lambda')
instance_id = event['InstanceId']
ami_name = f"{event['AMIName']}-{secrets.token_urlsafe(6)}"
return create_ami(instance_id, ami_name, description)
def cfn_handler(event, context=None):
result = cfnresponse.FAILED
response_data = {}
try:
resource_properties = event['ResourceProperties']
if event['RequestType'] == 'Delete':
deregister_ami(resource_properties['AMIId'])
else:
if 'Description' not in resource_properties:
resource_properties[
'Description'] = 'Created with Cloudformation'
response_data = handler(resource_properties, context)
result = cfnresponse.SUCCESS
except Exception as e:
print(e)
finally:
cfnresponse.send(event, context, result, response_data)
import base64, contextlib, functools, json, zlib
json_dumps = functools.partial(
json.dumps,
separators=(',', ':'),
check_circular=False,
sort_keys=True,
default=str)
def parse_event(event):
if 'awslogs' in event:
awslogs_data = event.get['awslogs']['data']
event = json.loads(
zlib.decompress(
base64.b64decode(awslogs_data), 16 + zlib.MAX_WBITS))
for log_event in event['logEvents']:
with contextlib.suppress(json.decoder.JSONDecodeError):
log_event['message'] = json.loads(log_event['message'])
return event
def handler(event=None, context=None):
event = parse_event(event)
print(json_dumps(event))
import sys
try:
from pip._internal.operations import freeze
except ImportError: # pip < 10.0
from pip.operations import freeze
def print_freeze():
"""Print output from `pip freeze`.
>>> print_freeze() #doctest: +ELLIPSIS
# Python ...
...
...==...
"""
print('# Python {}.{}.{}'.format(*sys.version_info))
for pkg in freeze.freeze():
print(pkg)
def lambda_handler(event=None, context=None):
"""Handle AWS Lambda Invocations."""
print_freeze()
"""
# 2019-11-27 output for python3.8:
# Python 3.8.0
boto3==1.10.2
botocore==1.13.2
docutils==0.15.2
jmespath==0.9.4
pip==19.2.3
python-dateutil==2.8.0
s3transfer==0.2.1
setuptools==41.2.0
six==1.12.0
urllib3==1.25.6
## 2019-11-27 output for python3.7:
# Python 3.7.5
boto3==1.9.221
botocore==1.12.221
docutils==0.15.2
jmespath==0.9.4
pip==19.2.3
python-dateutil==2.8.0
s3transfer==0.2.1
setuptools==41.2.0
six==1.12.0
urllib3==1.25.3
## 2019-11-27 output for python3.6:
# Python 3.6.9
boto3==1.9.221
botocore==1.12.221
docutils==0.15.2
jmespath==0.9.4
pip==18.1
python-dateutil==2.8.0
s3transfer==0.2.1
setuptools==40.6.2
six==1.12.0
urllib3==1.25.3
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment