def lambda_handler()

in ec2-spot-interruption-logging-insights/SpotInterruptionTriggerFunction/app.py [0:0]


def lambda_handler(event, context):

    print(event)

    # Get Instance ID
    instance_id = event['detail']['instance-id']
    instance_details = {}

    # Describe Instance Details
    try:
        response = ec2client.describe_instances(
            InstanceIds=[instance_id]
            )

        print(response)
        instance_details = response['Reservations'][0]['Instances'][0]

    except ClientError as e: 
        print("Unexpected error: %s" % e)

    # Create CloudWatch Log Stream
    try: 
        now = datetime.datetime.now()
        cloudwatch_stream_name = '{}/{}/{}/{}/{}'.format(now.year, now.month, now.day, now.hour, uuid.uuid4().hex)

        response = logclient.create_log_stream(
            logGroupName=cloudwatch_log_group_name,
            logStreamName=cloudwatch_stream_name
        )
        print(response)
    except ClientError as e: 
        if e.response['Error']['Code'] == 'ResourceAlreadyExistsException':
            print("CloudWatch Stream Exists: %s" % cloudwatch_stream_name)
        else:
            print("Unexpected error: %s" % e)

    # Write Event to CloudWatch Log Stream
    try:
        log_event= {
            'Event': event,
            'InstanceDetails' : {
                'InstanceId':   instance_details['InstanceId'],
                'InstanceType': instance_details['InstanceType'],
                'Placement':    instance_details['Placement'],
                'Tags':         instance_details['Tags']
            },
        }

        print(log_event)

        response = logclient.put_log_events(
            logGroupName=cloudwatch_log_group_name,
            logStreamName=cloudwatch_stream_name,
            logEvents=[ 
                {
                    'timestamp': int(round(time.time() * 1000)),
                    'message': json.dumps(log_event)
                }
            ]
        )

        # TODO: If reusing the CloudWatch Log Stream then you'll need to handle the nextSequenceToken:
        # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/logs.html#CloudWatchLogs.Client.put_log_events
        
        print(response)        

    except ClientError as e: 
        print("Unexpected error: %s" % e)

    # End
    print('Execution Complete')
    return