This example solution deploys an Auto Scaling group within a VPC. A lifecycle hook is enabled for the Auto Scaling group and a Lambda Function invokes in response to Lifecycle Action Events. The Lambda function uses AWS Systems Manager to install a sample application onto instances in the Auto Scaling group as they are Launched.
We recommend deploying the following Example AWS Cloud9 Environment to get started quickly with this example. Otherwise, you can attempt to run this example using your own environment with the following prerequisites installed.
- AWS CLI installed and configured with Administrator credentials.
- Python 3 installed
- Docker installed
- SAM CLI installed
Once you've deployed and accessed the Example AWS Cloud9 Environment execute the following steps from within the Example AWS Cloud9 Environment to deploy this example.
- Create a
S3 bucket
where we can upload our packaged Lambda functions for deployment.
aws s3 mb s3://BUCKET_NAME
- Change directories to this example.
cd ~/environment/amazon-ec2-auto-scaling-group-examples/features/lifecycle-hooks/lambda-managed-linux
- Build the solution.
sam build --use-container
- Package the solution. You will need to replace
REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME
with the name of the S3 bucket created in the first step.
sam package \
--output-template-file packaged.yaml \
--s3-bucket REPLACE_THIS_WITH_YOUR_S3_BUCKET_NAME
- Deploy the solution. You will need to replace
REPLACE_THIS_WITH_YOUR_KEY_PAIR_NAME
with the name of an SSH key in the region you are deploying the example to.
sam deploy \
--template-file packaged.yaml \
--stack-name lifecycle-hook-example \
--capabilities CAPABILITY_IAM \
--parameter-overrides \
InstanceKeyPair=REPLACE_THIS_WITH_YOUR_KEY_PAIR_NAME
- Delete the stack.
aws cloudformation delete-stack --stack-name lifecycle-hook-example