Select your cookie preferences

We use essential cookies and similar tools that are necessary to provide our site and services. We use performance cookies to collect anonymous statistics, so we can understand how customers use our site and make improvements. Essential cookies cannot be deactivated, but you can choose “Customize” or “Decline” to decline performance cookies.

If you agree, AWS and approved third parties will also use cookies to provide useful site features, remember your preferences, and display relevant content, including relevant advertising. To accept or decline all non-essential cookies, choose “Accept” or “Decline.” To make more detailed choices, choose “Customize.”

Use PutLifecycleHook with a CLI

Focus mode
Use PutLifecycleHook with a CLI - Amazon EC2 Auto Scaling

The following code examples show how to use PutLifecycleHook.

CLI
AWS CLI

Example 1: To create a lifecycle hook

This example creates a lifecycle hook that will invoke on any newly launched instances, with a timeout of 4800 seconds. This is useful for keeping the instances in a wait state until the user data scripts have finished, or for invoking an AWS Lambda function using EventBridge.

aws autoscaling put-lifecycle-hook \ --auto-scaling-group-name my-asg \ --lifecycle-hook-name my-launch-hook \ --lifecycle-transition autoscaling:EC2_INSTANCE_LAUNCHING \ --heartbeat-timeout 4800

This command produces no output. If a lifecycle hook with the same name already exists, it will be overwritten by the new lifecycle hook.

For more information, see Amazon EC2 Auto Scaling lifecycle hooks in the Amazon EC2 Auto Scaling User Guide.

Example 2: To send an Amazon SNS email message to notify you of instance state transitions

This example creates a lifecycle hook with the Amazon SNS topic and IAM role to use to receive notification at instance launch.

aws autoscaling put-lifecycle-hook \ --auto-scaling-group-name my-asg \ --lifecycle-hook-name my-launch-hook \ --lifecycle-transition autoscaling:EC2_INSTANCE_LAUNCHING \ --notification-target-arn arn:aws:sns:us-west-2:123456789012:my-sns-topic \ --role-arn arn:aws:iam::123456789012:role/my-auto-scaling-role

This command produces no output.

For more information, see Amazon EC2 Auto Scaling lifecycle hooks in the Amazon EC2 Auto Scaling User Guide.

Example 3: To publish a message to an Amazon SQS queue

This example creates a lifecycle hook that publishes a message with metadata to the specified Amazon SQS queue.

aws autoscaling put-lifecycle-hook \ --auto-scaling-group-name my-asg \ --lifecycle-hook-name my-launch-hook \ --lifecycle-transition autoscaling:EC2_INSTANCE_LAUNCHING \ --notification-target-arn arn:aws:sqs:us-west-2:123456789012:my-sqs-queue \ --role-arn arn:aws:iam::123456789012:role/my-notification-role \ --notification-metadata "SQS message metadata"

This command produces no output.

For more information, see Amazon EC2 Auto Scaling lifecycle hooks in the Amazon EC2 Auto Scaling User Guide.

PowerShell
Tools for PowerShell

Example 1: This example adds the specified lifecycle hook to the specified Auto Scaling group.

Write-ASLifecycleHook -AutoScalingGroupName my-asg -LifecycleHookName "myLifecycleHook" -LifecycleTransition "autoscaling:EC2_INSTANCE_LAUNCHING" -NotificationTargetARN "arn:aws:sns:us-west-2:123456789012:my-sns-topic" -RoleARN "arn:aws:iam::123456789012:role/my-iam-role"
  • For API details, see PutLifecycleHook in AWS Tools for PowerShell Cmdlet Reference.

AWS CLI

Example 1: To create a lifecycle hook

This example creates a lifecycle hook that will invoke on any newly launched instances, with a timeout of 4800 seconds. This is useful for keeping the instances in a wait state until the user data scripts have finished, or for invoking an AWS Lambda function using EventBridge.

aws autoscaling put-lifecycle-hook \ --auto-scaling-group-name my-asg \ --lifecycle-hook-name my-launch-hook \ --lifecycle-transition autoscaling:EC2_INSTANCE_LAUNCHING \ --heartbeat-timeout 4800

This command produces no output. If a lifecycle hook with the same name already exists, it will be overwritten by the new lifecycle hook.

For more information, see Amazon EC2 Auto Scaling lifecycle hooks in the Amazon EC2 Auto Scaling User Guide.

Example 2: To send an Amazon SNS email message to notify you of instance state transitions

This example creates a lifecycle hook with the Amazon SNS topic and IAM role to use to receive notification at instance launch.

aws autoscaling put-lifecycle-hook \ --auto-scaling-group-name my-asg \ --lifecycle-hook-name my-launch-hook \ --lifecycle-transition autoscaling:EC2_INSTANCE_LAUNCHING \ --notification-target-arn arn:aws:sns:us-west-2:123456789012:my-sns-topic \ --role-arn arn:aws:iam::123456789012:role/my-auto-scaling-role

This command produces no output.

For more information, see Amazon EC2 Auto Scaling lifecycle hooks in the Amazon EC2 Auto Scaling User Guide.

Example 3: To publish a message to an Amazon SQS queue

This example creates a lifecycle hook that publishes a message with metadata to the specified Amazon SQS queue.

aws autoscaling put-lifecycle-hook \ --auto-scaling-group-name my-asg \ --lifecycle-hook-name my-launch-hook \ --lifecycle-transition autoscaling:EC2_INSTANCE_LAUNCHING \ --notification-target-arn arn:aws:sqs:us-west-2:123456789012:my-sqs-queue \ --role-arn arn:aws:iam::123456789012:role/my-notification-role \ --notification-metadata "SQS message metadata"

This command produces no output.

For more information, see Amazon EC2 Auto Scaling lifecycle hooks in the Amazon EC2 Auto Scaling User Guide.

For a complete list of AWS SDK developer guides and code examples, see Using this service with an AWS SDK. This topic also includes information about getting started and details about previous SDK versions.

PrivacySite termsCookie preferences
© 2025, Amazon Web Services, Inc. or its affiliates. All rights reserved.