Amazon EC2 notifying the instance when the autoscale service terminates it

I haven't found anyway to do this whole process through the CLI as it seems that Amazon hasn't added the SQS service to its api tools for linux. However, yes it is possible to do and its really not too difficult.

  1. Log into the AWS console
  2. Click the SNS tab

    • Create a new topic
    • Copy the Topic ARN example: arn:aws:sns:us-east-1:############:mytopic
    • Create a new subscription for whatever protocol you want (HTTP, Email, SMS, SQS)
  3. Using the command line tools or some other interface run the command below. After running this command all notifications will be sent using the defined protocol.

as-put-notification-configuration <auto-scaling-group> -t <topic-arn> -n autoscaling:EC2_INSTANCE_LAUNCH,autoscaling:EC2_INSTANCE_TERMINATE

I recommend sending them to SQS first then to your application. Its a little bit safer to have the messages stored in a queue and read from them. The only problem with SQS is its a little more complicated to get going, but there's plenty of documentation about SQS and a good API in almost any language you're using.