{"id":9508,"library":"aws-cdk-aws-autoscaling-hooktargets","title":"AWS CDK AutoScaling Lifecycle Hook Targets (v1)","description":"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.","status":"deprecated","version":"1.204.0","language":"en","source_language":"en","source_url":"https://github.com/aws/aws-cdk.git","tags":["aws","cdk","autoscaling","cloud-infrastructure","iac","v1-only","lifecycle-hooks"],"install":[{"cmd":"pip install aws-cdk.aws-autoscaling-hooktargets","lang":"bash","label":"Install for CDK v1"}],"dependencies":[{"reason":"Core AWS CDK v1 functionality.","package":"aws-cdk.core","optional":false},{"reason":"Required for Auto Scaling Group definitions.","package":"aws-cdk.aws-autoscaling","optional":false},{"reason":"Needed for SNS Topic targets.","package":"aws-cdk.aws-sns","optional":true},{"reason":"Needed for SQS Queue targets.","package":"aws-cdk.aws-sqs","optional":true},{"reason":"Needed for Lambda Function targets.","package":"aws-cdk.aws-lambda","optional":true}],"imports":[{"note":"This specific construct resides in the hooktargets module, not directly under aws_autoscaling.","wrong":"from aws_cdk.aws_autoscaling.TopicHookTarget import ...","symbol":"TopicHookTarget","correct":"from aws_cdk.aws_autoscaling_hooktargets import TopicHookTarget"},{"symbol":"QueueHookTarget","correct":"from aws_cdk.aws_autoscaling_hooktargets import QueueHookTarget"},{"symbol":"LambdaFunctionHookTarget","correct":"from aws_cdk.aws_autoscaling_hooktargets import LambdaFunctionHookTarget"}],"quickstart":{"code":"import os\nfrom aws_cdk import (\n    Stack,\n    App,\n    Duration,\n    aws_ec2 as ec2,\n    aws_autoscaling as autoscaling,\n    aws_sns as sns,\n)\nfrom aws_cdk.aws_autoscaling_hooktargets import TopicHookTarget\nfrom constructs import Construct\n\nclass AutoScalingHookStack(Stack):\n    def __init__(self, scope: Construct, id: str, **kwargs) -> None:\n        super().__init__(scope, id, **kwargs)\n\n        vpc = ec2.Vpc(self, \"VPC\", max_azs=2)\n\n        # Create an SNS Topic to receive notifications\n        topic = sns.Topic(self, \"LifecycleHookTopic\")\n\n        # Create an Auto Scaling Group\n        asg = autoscaling.AutoScalingGroup(self, \"ASG\",\n            vpc=vpc,\n            instance_type=ec2.InstanceType.of(ec2.InstanceClass.T2, ec2.InstanceSize.MICRO),\n            machine_image=ec2.AmazonLinuxImage(),\n            min_capacity=0,\n            max_capacity=1,\n            desired_capacity=0 # Start at 0 to see hooks fire on scale-out\n        )\n\n        # Add a lifecycle hook targeting the SNS Topic\n        asg.add_lifecycle_hook(\"ScalingOutHook\",\n            lifecycle_transition=autoscaling.LifecycleTransition.AUTO_SCALING_LAUNCH,\n            notification_target=TopicHookTarget(topic),\n            default_result=autoscaling.DefaultResult.CONTINUE,\n            heartbeat_timeout=Duration.minutes(5)\n        )\n\n# Example of how to synthesize the stack\n# app = App()\n# AutoScalingHookStack(app, \"AutoScalingHookExampleStack\",\n#   env={'region': os.environ.get('CDK_DEFAULT_REGION', 'us-east-1')})\n# app.synth()\n","lang":"python","description":"This quickstart demonstrates how to create an AWS Auto Scaling Group with a lifecycle hook configured to publish notifications to an SNS Topic upon instance launch, using the `TopicHookTarget` from this library. This example is for CDK v1. Make sure to replace `os.environ.get('CDK_DEFAULT_REGION')` with a concrete region if not set."},"warnings":[{"fix":"For AWS CDK v2, do not install this package. Its functionality is integrated directly into `aws-cdk-lib.aws_autoscaling`. You should use `aws_cdk.aws_autoscaling.LifecycleHook` and pass appropriate `notification_target` properties or implement `aws_cdk.aws_autoscaling.ILifecycleHookTarget` directly.","message":"This library (`aws-cdk.aws-autoscaling-hooktargets`) is specific to AWS CDK v1. It is not compatible with AWS CDK v2.","severity":"breaking","affected_versions":"All versions (CDK v1 vs CDK v2)"},{"fix":"Migrate your CDK application to v2. Install `aws-cdk-lib` and `constructs`, then refactor your lifecycle hook definitions to use the v2 API within `aws_cdk.aws_autoscaling`.","message":"As of CDK v2, dedicated `*-hooktargets` modules are deprecated in favor of direct integration into `aws-cdk-lib`'s primary service modules.","severity":"deprecated","affected_versions":"All versions, when considering migration to CDK v2."},{"fix":"Ensure the Auto Scaling Service-Linked Role (`AWSServiceRoleForAutoScaling`) has the necessary permissions (e.g., `sns:Publish` for SNS, `sqs:SendMessage` for SQS, `lambda:InvokeFunction` for Lambda) on the target resources. While CDK often attempts to add these, explicit policies may be needed in complex scenarios or if default permissions are overridden.","message":"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).","severity":"gotcha","affected_versions":"All versions."},{"fix":"Carefully consider the expected duration of custom actions performed during the hook and set `heartbeat_timeout` accordingly. Use `default_result=autoscaling.DefaultResult.ABANDON` if you want instances to terminate without completing the action, or `default_result=autoscaling.DefaultResult.CONTINUE` if you want them to proceed even if the hook action fails or times out.","message":"Incorrectly configured `heartbeat_timeout` or `default_result` for a lifecycle hook can lead to instances hanging or prematurely continuing/abandoning the lifecycle action.","severity":"gotcha","affected_versions":"All versions."}],"env_vars":null,"last_verified":"2026-04-17T00:00:00.000Z","next_check":"2026-07-16T00:00:00.000Z","problems":[{"fix":"If 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.","cause":"Attempting to use this CDK v1-specific package in a CDK v2 project, or the package was not installed in a v1 project.","error":"ModuleNotFoundError: No module named 'aws_cdk.aws_autoscaling_hooktargets'"},{"fix":"Refactor 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).","cause":"Passing a CDK v1 hook target object to a CDK v2 `LifecycleHook` construct, which expects a v2-compatible target interface.","error":"TypeError: Argument 'notification_target' must be of type aws_cdk.aws_autoscaling.ILifecycleHookTarget. Got <class 'aws_cdk.aws_autoscaling_hooktargets.TopicHookTarget'>"},{"fix":"Verify 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.","cause":"The IAM permissions of the Auto Scaling Service-Linked Role are insufficient to perform actions on the specified lifecycle hook target.","error":"The service-linked role for EC2 Auto Scaling does not have permissions to publish to the SNS topic."}]}