This application will check the instance statues action / event codes for an instance that is slated for hardware retirement and must be stopped and started to be moved to new hardware. This will check to ensure any instances stopped by AWS will be started back up with automation.
You can not see the action / event codes for an instance unless you use the CLI. The following command will show you any that exist:
$ aws ec2 describe-instance-status --instance-ids <INSTANCE_IDS> | grep "InstanceId\|Code\|Description"
- Tag-Retirement-Instances_Role contains Policy with access to EC2 & CloudWatch Logs.
- Start-Stopped-Retirement-Instances_Role contains Policy with access EC2, KMS & CloudWatch Logs.
- Remove-Retirement-Instances-Tag_Role contains Policy with access to EC2 & CloudWatch logs.
- Tag-Retirement-Instances function - Tag instances with ‘event.code - instance-retirement' in their status.
- Start-Stopped-Retirement-Instances function - Start any stopped instances that have the tag RetirementScheduled = Yes
- Remove-Retirement-Instances-Tag function - Remove the RetirementScheduled tag after the instance has been started. We do not want these tags to exist after the instances are started incase a user intentionally stopped the instance.
- Run Tag-Retirement-Instances Lambda using cron(00 23 * * ? *)
- Run Start-Stopped-Retirement-Instances Lambda every 5 minutes using rate(5 minutes)
- Run Remove-Retirement-Instances-Tag once a day just before the Tag-Retirement-Instances Lambda using cron(50 22 * * ? *)
- Update TagRetirementInstances-Policy.template REGION and ACCOUNT_ID place holders in the template (alternatively this could be converted to use Parameters.)
- Create a new CloudFormation Stack and populate the Parameter Policy Name: Tag-Retirement-Instances_Policy
- Launch the Stack.
- Grab the TagRetirementInstances-Role.template
- Create a new CloudFormation Stack and populate the Parameters Role Name: Tag-Retirement-Instances_Role; ARN with the Policy ARN; Services: lambda.amazonaws.com
- Launch the Stack.
- Repeat 1-6 for the StartStoppedRetirementInstances-Policy.template and StartStoppedRetirementInstances-Role.template
- Repeat 1-6 for the RemoveRetirementInstancesTag-Policy.template and RemoveRetirementInstancesTag-Role.template
- Verify all stacks have launched successfully.
- Zip each of the Lambda functions into zip files called Tag-Retirement-Instances.zip, Remove-Retirement-Instances-Tag.zip and Start-Stopped-Retirement-Instances.zip
- Upload the zip files to a new S3 bucket
- Grab the RetirementInstancesLambdaFunctions.template
- Create a new CloudFormation Stack and populate the Parameters S3Bucket with the bucket the zip files are in and 3 Role fields with the 3 IAM Role ARNs.
- Launch the stack.