Reputation: 765
Is there a way to send in SQS message the list of Spawned "scaled" instances configured to launch by Auto Scale service of EC2 ?
Upvotes: 1
Views: 226
Reputation: 4491
If you want to know the Instance ID of an ec2 instance during a scaling event you can set up Lifecycle hooks with SQS.
Receive Notification Using Amazon SQS
You can use Amazon SQS to set up a notification target to receive notifications when a lifecycle action occurs.
To set up notifications using Amazon SQS
Create the target using Amazon SQS. For more information, see Getting Started with Amazon SQS in the Amazon Simple Queue Service Developer Guide. Note the ARN of the target.
Create an IAM role to grant Auto Scaling permission to access your notification target, using the steps in Creating a Role to Delegate Permissions to an AWS Service in the IAM User Guide. When prompted to select a role type, select AWS Service Roles, AutoScaling Notification Access. Note the ARN of the role. For example, arn:aws:iam::123456789012:role/my-notification-role.
When Auto Scaling responds to a scale out or scale in event, it puts the instance in a wait state. While the instance is in a wait state, Auto Scaling publishes a message to the notification target.
Example Message:
Service: AWS Auto Scaling
Time: 2016-09-30T20:42:11.305Z
RequestId: 18b2ec17-3e9b-4c15-8024-ff2e8ce8786a
LifecycleActionToken: 71514b9d-6a40-4b26-8523-05e7ee35fa40
AccountId: 123456789012
AutoScalingGroupName: my-asg
LifecycleHookName: my-hook
EC2InstanceId: i-0598c7d356eba48d7
LifecycleTransition: autoscaling:EC2_INSTANCE_LAUNCHING
NotificationMetadata: null
Then Add Lifecycle Hook
You can create lifecycle hooks using the put-lifecycle-hook command.
To perform an action on scale out, use the following command:
aws autoscaling put-lifecycle-hook --lifecycle-hook-name my-hook --auto-scaling-group-name my-asg --lifecycle-transition autoscaling:EC2_INSTANCE_LAUNCHING --notification-target-arn arn:aws:sns:us-west-2:123456789012:my-sqs --role-arn arn:aws:iam::123456789012:role/my-notification-role
http://docs.aws.amazon.com/autoscaling/latest/userguide/lifecycle-hooks.html#adding-lifecycle-hooks
http://docs.aws.amazon.com/cli/latest/reference/autoscaling/put-lifecycle-hook.html
Upvotes: 1