λ
A client needed to retrieve logs from instances before termination. Auto scaling lifecycle hooks make this possible.
For documentation on lifecycle hooks, see http://docs.aws.amazon.com/AutoScaling/latest/DeveloperGuide/introducing-lifecycle-hooks.html. This post focuses on practical implementation.
Three requirements for successful auto scaling lifecycle hooks:
The challenge is putting everything together, as there are few existing code examples online.
My implementation process:
The complete Python code is available here. Deploy it via CloudFormation template to set the required variables. The OnTermination scripts must be manually placed in the bucket after template creation.
Example OnTermination script:
#!/bin/bash
INSTANCE_ID=$(wget -q -O - http://instance-data/latest/meta-data/instance-id)
aws s3 cp /var/log/messages "s3://test2-s3bucket-1asdfg1et/${INSTANCE_ID}-messages.txt"
Tip for CloudFormation templates: Instead of hard-coding scripts, create a single bootstrap script in S3 and use userdata to download and execute it. The template can generate environment variables via export statements that the bootstrap script loads. This enables testing on existing instances without modifying the CloudFormation template or triggering scaling events.
Comments