Created
January 10, 2020 14:40
-
-
Save polomarcus/a4f6ee55fe515044c02ba705f4e94baf to your computer and use it in GitHub Desktop.
Graceful shutdown using Simple Sytems Manager and Terraform on AWS
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def lambda_handler(event, context): | |
for record in event['Records']: | |
parsed_event = (json.loads(record["body"])) | |
msg_receipt_handle = str(record['receiptHandle']) | |
if parsed_event.get("LifecycleTransition") == "autoscaling:EC2_INSTANCE_TERMINATING" and parsed_event.get("LifecycleHookName") == "my-service": | |
auto_scaling_group = get_env_variable('ASG_NAME').split()[0] | |
ec2_instance_id = parsed_event.get("EC2InstanceId") | |
delete_sqs_message(msg_receipt_handle) # delete first to avoid infinite loop on unvalid instance ID | |
ssm_command([ec2_instance_id]) # must be an array | |
else: | |
print "not a EC2_INSTANCE_TERMINATING event - skipping lambda" | |
delete_sqs_message(msg_receipt_handle) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
parsed_event.get not returns anything.. as json that it parses has a nested formatted..How to extract the data...