AWS CloudFormation
User Guide (Version )

AWS::AutoScaling::LifecycleHook

Defines lifecycle hooks for an Amazon EC2 Auto Scaling group. Lifecycle hooks specify actions to perform when Amazon EC2 Auto Scaling launches or terminates instances. When you use a lifecycle hook, the Auto Scaling group pauses the instance either after it is launched (before it is put into service) or as it is terminated (before it is fully terminated).

For more information, see PutLifecycleHook in the Amazon EC2 Auto Scaling API Reference and Amazon EC2 Auto Scaling Lifecycle Hooks in the Amazon EC2 Auto Scaling User Guide.

Syntax

To declare this entity in your AWS CloudFormation template, use the following syntax:

JSON

{ "Type" : "AWS::AutoScaling::LifecycleHook", "Properties" : { "AutoScalingGroupName" : String, "DefaultResult" : String, "HeartbeatTimeout" : Integer, "LifecycleHookName" : String, "LifecycleTransition" : String, "NotificationMetadata" : String, "NotificationTargetARN" : String, "RoleARN" : String } }

YAML

Type: AWS::AutoScaling::LifecycleHook Properties: AutoScalingGroupName: String DefaultResult: String HeartbeatTimeout: Integer LifecycleHookName: String LifecycleTransition: String NotificationMetadata: String NotificationTargetARN: String RoleARN: String

Properties

AutoScalingGroupName

The name of the Auto Scaling group for the lifecycle hook.

Required: Yes

Type: String

Minimum: 1

Maximum: 1600

Pattern: [\u0020-\uD7FF\uE000-\uFFFD\uD800\uDC00-\uDBFF\uDFFF\r\n\t]*

Update requires: Replacement

DefaultResult

The action the Auto Scaling group takes when the lifecycle hook timeout elapses or if an unexpected failure occurs. The valid values are CONTINUE and ABANDON (default).

Required: No

Type: String

Update requires: No interruption

HeartbeatTimeout

The amount of time, in seconds, that can elapse before the lifecycle hook times out. If the lifecycle hook times out, Amazon EC2 Auto Scaling performs the action that you specified in the DefaultResult property.

Required: No

Type: Integer

Update requires: No interruption

LifecycleHookName

The name of the lifecycle hook.

Required: No

Type: String

Minimum: 1

Maximum: 255

Pattern: [A-Za-z0-9\-_\/]+

Update requires: Replacement

LifecycleTransition

The instance state to which you want to attach the lifecycle hook. The valid values are:

  • autoscaling:EC2_INSTANCE_LAUNCHING

  • autoscaling:EC2_INSTANCE_TERMINATING

Required: Yes

Type: String

Update requires: No interruption

NotificationMetadata

Additional information that is included any time Amazon EC2 Auto Scaling sends a message to the notification target.

Required: No

Type: String

Minimum: 1

Maximum: 1023

Pattern: [\u0020-\uD7FF\uE000-\uFFFD\uD800\uDC00-\uDBFF\uDFFF\r\n\t]*

Update requires: No interruption

NotificationTargetARN

The Amazon Resource Name (ARN) of the notification target that Amazon EC2 Auto Scaling uses to notify you when an instance is in the transition state for the lifecycle hook. You can specify an Amazon SQS queue or an Amazon SNS topic. The notification message includes the following information: lifecycle action token, user account ID, Auto Scaling group name, lifecycle hook name, instance ID, lifecycle transition, and notification metadata.

Required: No

Type: String

Minimum: 1

Maximum: 1600

Pattern: [\u0020-\uD7FF\uE000-\uFFFD\uD800\uDC00-\uDBFF\uDFFF\r\n\t]*

Update requires: No interruption

RoleARN

The ARN of the IAM role that allows the Auto Scaling group to publish to the specified notification target, for example, an Amazon SNS topic or an Amazon SQS queue. For information about creating this role, see Preparing for Notifications in the Amazon EC2 Auto Scaling User Guide.

Required: No

Type: String

Minimum: 1

Maximum: 1600

Pattern: [\u0020-\uD7FF\uE000-\uFFFD\uD800\uDC00-\uDBFF\uDFFF\r\n\t]*

Update requires: No interruption

Return Values

Ref

When the logical ID of this resource is provided to the Ref intrinsic function, Ref returns the resource name. For example: mylifecyclehook.

For more information about using the Ref function, see Ref.

Examples

The following examples specify lifecycle hooks.

Lifecycle Hook for Instance Termination

The following example specifies a lifecycle hook that supports a custom action at instance termination. It uses the Ref intrinsic function to refer to an Auto Scaling group (whose logical name is myASG) that is declared elsewhere in the same template.

Note that the snippet uses the NotificationTargetARN and RoleARN properties to specify the Amazon SNS topic and IAM role to use to receive notification when a lifecycle action occurs.

JSON

{ "myLifecycleHook":{ "Type":"AWS::AutoScaling::LifecycleHook", "Properties":{ "AutoScalingGroupName":{ "Ref":"myASG" }, "LifecycleTransition":"autoscaling:EC2_INSTANCE_TERMINATING", "NotificationTargetARN":{ "Ref":"lifecycleHookTopic" }, "RoleARN":{ "Fn::GetAtt":[ "lifecycleHookRole", "Arn" ] } } } }

YAML

myLifecycleHook: Type: AWS::AutoScaling::LifecycleHook Properties: AutoScalingGroupName: Ref: myASG LifecycleTransition: "autoscaling:EC2_INSTANCE_TERMINATING" NotificationTargetARN: Ref: lifecycleHookTopic RoleARN: Fn::GetAtt: - lifecycleHookRole - Arn

Lifecycle Hook for Instance Launch

The following example specifies a lifecycle hook that supports a custom action at instance launch. The lifecycle hook is added to a new Auto Scaling group that's created from the same snippet. For more information, see LifecycleHookSpecification.

Note that the snippet uses the NotificationTargetARN and RoleARN properties to specify the Amazon SQS queue and IAM role to use to receive notification when a lifecycle action occurs.

JSON

{ "AWSTemplateFormatVersion":"2010-09-09", "Parameters":{ "Subnets":{ "Type":"CommaDelimitedList" }, "AZs":{ "Type":"CommaDelimitedList" } }, "Resources":{ "myASG":{ "Type":"AWS::AutoScaling::AutoScalingGroup", "Properties":{ "AvailabilityZones":[ { "Ref":"AZs" } ], "VPCZoneIdentifier":{ "Ref":"Subnets" }, "DesiredCapacity":"2", "MaxSize":"3", "MinSize":"1", "LaunchConfigurationName":{ "Ref":"myLaunchConfig" }, "LifecycleHookSpecificationList":[ { "LifecycleTransition":"autoscaling:EC2_INSTANCE_LAUNCHING", "LifecycleHookName":"myLifecycleHook", "HeartbeatTimeout":4800, "NotificationTargetARN":{ "Fn::GetAtt":[ "SQS", "Arn" ] }, "RoleArn":{ "Fn::Join":[ ":", [ "arn:aws:iam:", { "Ref":"AWS::AccountId" }, "role/role-name" ] ] } } ] } }, "SQS":{ "Type":"AWS::SQS::Queue" } } }

YAML

AWSTemplateFormatVersion: 2010-09-09 Parameters: Subnets: Type: CommaDelimitedList AZs: Type: CommaDelimitedList Resources: myASG: Type: AWS::AutoScaling::AutoScalingGroup Properties: AvailabilityZones: - !Ref AZs VPCZoneIdentifier: !Ref Subnets DesiredCapacity: '2' MaxSize: '3' MinSize: '1' LaunchConfigurationName: !Ref myLaunchConfig LifecycleHookSpecificationList: - LifecycleTransition: "autoscaling:EC2_INSTANCE_LAUNCHING" LifecycleHookName: "myLifecycleHook" HeartbeatTimeout: 4800 NotificationTargetARN: !GetAtt SQS.Arn RoleARN: !Join - ':' - - 'arn:aws:iam:' - !Ref 'AWS::AccountId' - role/role-name SQS: Type: AWS::SQS::Queue