def cloudformation_create()

in aws/solutions/lambda-backed-cloudformation-custom-resources/get_latest_ami_id/lambda_function.py [0:0]


def cloudformation_create(event, context):
    """Cloudformation called us with CreateStack."""
    if DEBUG_MODE is True:
        print("Create Option: Attempting to run creation")
    image_dict = describe_images(event, context)
    if DEBUG_MODE is True:
        print(json.dumps(image_dict, indent=2))
    if len(image_dict['Images']) < 1:
        custom_raise_exception(event, context, 'AMI Search returned no results.')
    newest_entry = {"CreationDate": "1980-01-01T01:01:01.000Z"}
    counter = 0
    for image in image_dict['Images']:
        counter = counter + 1
        if DEBUG_MODE is True:
            print("Loop count: %d" % counter)
        if time.strptime(image['CreationDate'], "%Y-%m-%dT%H:%M:%S.000Z") > time.strptime(newest_entry['CreationDate'], "%Y-%m-%dT%H:%M:%S.000Z"):
            newest_entry = image
            if DEBUG_MODE is True:
                print("Found newer entry time of %s" % str(newest_entry['CreationDate']))
    if DEBUG_MODE is True:
        print(json.dumps(newest_entry, indent=2))
    response_data = {
        'ami-id': newest_entry['ImageId']
        }
    if event['StackId'] == '012345678910/fake-stack-id':
        print("Skipping sending CloudFormation response due to local testing.")
        return
    send(event, context, 'SUCCESS', response_data, event['StackId'])
    if DEBUG_MODE is True:
        print("Exiting successfully")
    return