AWS CDK AutoScaling Lifecycle Hook Targets (v1)
This library provides high-level constructs for defining targets for AWS Auto Scaling group lifecycle hooks, such as sending notifications to an SNS topic or SQS queue, or invoking a Lambda function. It is part of the AWS Cloud Development Kit (CDK) v1 ecosystem, specifically module version 1.204.0, and follows the CDK's rapid release cadence.
Common errors
-
ModuleNotFoundError: No module named 'aws_cdk.aws_autoscaling_hooktargets'
cause Attempting to use this CDK v1-specific package in a CDK v2 project, or the package was not installed in a v1 project.fixIf using CDK v2, remove this package and use `aws_cdk.aws_autoscaling` directly. If using CDK v1, ensure `pip install aws-cdk.aws-autoscaling-hooktargets` has been run. -
TypeError: Argument 'notification_target' must be of type aws_cdk.aws_autoscaling.ILifecycleHookTarget. Got <class 'aws_cdk.aws_autoscaling_hooktargets.TopicHookTarget'>
cause Passing a CDK v1 hook target object to a CDK v2 `LifecycleHook` construct, which expects a v2-compatible target interface.fixRefactor your code for CDK v2. Instead of `TopicHookTarget(topic)`, you'll directly configure the `notification_target` property of the `LifecycleHook` (e.g., using `aws_cdk.aws_autoscaling.NotificationConfigurationProperty` or a custom v2 `ILifecycleHookTarget` implementation). -
The service-linked role for EC2 Auto Scaling does not have permissions to publish to the SNS topic.
cause The IAM permissions of the Auto Scaling Service-Linked Role are insufficient to perform actions on the specified lifecycle hook target.fixVerify that the Auto Scaling Service-Linked Role (`AWSServiceRoleForAutoScaling`) has the necessary permissions for the target resource. For an SNS topic, this means `sns:Publish` permission on the topic's ARN.
Warnings
- breaking This library (`aws-cdk.aws-autoscaling-hooktargets`) is specific to AWS CDK v1. It is not compatible with AWS CDK v2.
- deprecated As of CDK v2, dedicated `*-hooktargets` modules are deprecated in favor of direct integration into `aws-cdk-lib`'s primary service modules.
- gotcha AWS Auto Scaling lifecycle hooks require proper IAM permissions for the Auto Scaling Service-Linked Role to interact with the specified targets (SNS, SQS, Lambda).
- gotcha Incorrectly configured `heartbeat_timeout` or `default_result` for a lifecycle hook can lead to instances hanging or prematurely continuing/abandoning the lifecycle action.
Install
-
pip install aws-cdk.aws-autoscaling-hooktargets
Imports
- TopicHookTarget
from aws_cdk.aws_autoscaling.TopicHookTarget import ...
from aws_cdk.aws_autoscaling_hooktargets import TopicHookTarget
- QueueHookTarget
from aws_cdk.aws_autoscaling_hooktargets import QueueHookTarget
- LambdaFunctionHookTarget
from aws_cdk.aws_autoscaling_hooktargets import LambdaFunctionHookTarget
Quickstart
import os
from aws_cdk import (
Stack,
App,
Duration,
aws_ec2 as ec2,
aws_autoscaling as autoscaling,
aws_sns as sns,
)
from aws_cdk.aws_autoscaling_hooktargets import TopicHookTarget
from constructs import Construct
class AutoScalingHookStack(Stack):
def __init__(self, scope: Construct, id: str, **kwargs) -> None:
super().__init__(scope, id, **kwargs)
vpc = ec2.Vpc(self, "VPC", max_azs=2)
# Create an SNS Topic to receive notifications
topic = sns.Topic(self, "LifecycleHookTopic")
# Create an Auto Scaling Group
asg = autoscaling.AutoScalingGroup(self, "ASG",
vpc=vpc,
instance_type=ec2.InstanceType.of(ec2.InstanceClass.T2, ec2.InstanceSize.MICRO),
machine_image=ec2.AmazonLinuxImage(),
min_capacity=0,
max_capacity=1,
desired_capacity=0 # Start at 0 to see hooks fire on scale-out
)
# Add a lifecycle hook targeting the SNS Topic
asg.add_lifecycle_hook("ScalingOutHook",
lifecycle_transition=autoscaling.LifecycleTransition.AUTO_SCALING_LAUNCH,
notification_target=TopicHookTarget(topic),
default_result=autoscaling.DefaultResult.CONTINUE,
heartbeat_timeout=Duration.minutes(5)
)
# Example of how to synthesize the stack
# app = App()
# AutoScalingHookStack(app, "AutoScalingHookExampleStack",
# env={'region': os.environ.get('CDK_DEFAULT_REGION', 'us-east-1')})
# app.synth()