put_lifecycle_hook
async
¶
put_lifecycle_hook(
*,
config_overrides: Optional[AsyncAutoScalingClientConfig] = None,
lifecycle_hook_name: Optional[AsciiStringMaxLen255] = None,
auto_scaling_group_name: Optional[XmlStringMaxLen255] = None,
lifecycle_transition: Optional[LifecycleTransition] = None,
role_arn: Optional[XmlStringMaxLen255] = None,
notification_target_arn: Optional[NotificationTargetResourceName] = None,
notification_metadata: Optional[AnyPrintableAsciiStringMaxLen4000] = None,
heartbeat_timeout: Optional[HeartbeatTimeout] = None,
default_result: Optional[LifecycleActionResult] = None,
) -> PutLifecycleHookAnswer
Creates or updates a lifecycle hook for the specified Auto Scaling group.
Lifecycle hooks let you create solutions that are aware of events in the Auto Scaling instance lifecycle, and then perform a custom action on instances when the corresponding lifecycle event occurs.
This step is a part of the procedure for adding a lifecycle hook to an Auto Scaling group:
-
(Optional) Create a launch template or launch configuration with a user data script that runs while an instance is in a wait state due to a lifecycle hook.
-
(Optional) Create a Lambda function and a rule that allows Amazon EventBridge to invoke your Lambda function when an instance is put into a wait state due to a lifecycle hook.
-
(Optional) Create a notification target and an IAM role. The target can be either an Amazon SQS queue or an Amazon SNS topic. The role allows Amazon EC2 Auto Scaling to publish lifecycle notifications to the target.
-
Create the lifecycle hook. Specify whether the hook is used when the instances launch or terminate.
-
If you need more time, record the lifecycle action heartbeat to keep the instance in a wait state using the RecordLifecycleActionHeartbeat API call.
-
If you finish before the timeout period ends, send a callback by using the CompleteLifecycleAction API call.
For more information, see Amazon EC2 Auto Scaling lifecycle hooks in the Amazon EC2 Auto Scaling User Guide.
If you exceed your maximum limit of lifecycle hooks, which by default is 50 per Auto Scaling group, the call fails.
You can view the lifecycle hooks for an Auto Scaling group using the DescribeLifecycleHooks API call. If you are no longer using a lifecycle hook, you can delete it by calling the DeleteLifecycleHook API.
Parameters:
-
lifecycle_hook_name(Optional[AsciiStringMaxLen255], default:None) –The name of the lifecycle hook.
-
auto_scaling_group_name(Optional[XmlStringMaxLen255], default:None) –The name of the Auto Scaling group.
-
lifecycle_transition(Optional[LifecycleTransition], default:None) –The lifecycle transition. For Auto Scaling groups, there are two major lifecycle transitions.
-
To create a lifecycle hook for scale-out events, specify
autoscaling:EC2_INSTANCE_LAUNCHING. -
To create a lifecycle hook for scale-in events, specify
autoscaling:EC2_INSTANCE_TERMINATING.
Required for new lifecycle hooks, but optional when updating existing hooks.
-
-
role_arn(Optional[XmlStringMaxLen255], default:None) –The ARN of the IAM role that allows the Auto Scaling group to publish to the specified notification target.
Valid only if the notification target is an Amazon SNS topic or an Amazon SQS queue. Required for new lifecycle hooks, but optional when updating existing hooks.
-
notification_target_arn(Optional[NotificationTargetResourceName], default:None) –The Amazon Resource Name (ARN) of the notification target that Amazon EC2 Auto Scaling uses to notify you when an instance is in a wait state for the lifecycle hook. You can specify either an Amazon SNS topic or an Amazon SQS queue.
If you specify an empty string, this overrides the current ARN.
This operation uses the JSON format when sending notifications to an Amazon SQS queue, and an email key-value pair format when sending notifications to an Amazon SNS topic.
When you specify a notification target, Amazon EC2 Auto Scaling sends it a test message. Test messages contain the following additional key-value pair:
"Event": "autoscaling:TEST_NOTIFICATION". -
notification_metadata(Optional[AnyPrintableAsciiStringMaxLen4000], default:None) –Additional information that you want to include any time Amazon EC2 Auto Scaling sends a message to the notification target.
-
heartbeat_timeout(Optional[HeartbeatTimeout], default:None) –The maximum time, in seconds, that can elapse before the lifecycle hook times out. The range is from
30to7200seconds. The default value is3600seconds (1 hour). -
default_result(Optional[LifecycleActionResult], default:None) –The action the Auto Scaling group takes when the lifecycle hook timeout elapses or if an unexpected failure occurs. The default value is
ABANDON.Valid values:
CONTINUE|ABANDON
Raises:
-
LimitExceededFault–You have already reached a limit for your Amazon EC2 Auto Scaling resources (for example, Auto Scaling groups, launch configurations, or lifecycle hooks). For more information, see DescribeAccountLimits in the Amazon EC2 Auto Scaling API Reference.
-
ResourceContentionFault–You already have a pending update to an Amazon EC2 Auto Scaling resource (for example, an Auto Scaling group, instance, or load balancer).
-
UnknownServiceError–The service returned an error code this client does not model.
Examples:
To create a launch lifecycle hook This example creates a lifecycle hook for instance launch.
>>> await client.put_lifecycle_hook(lifecycle_hook_name='my-launch-lifecycle-hook', heartbeat_timeout=300, auto_scaling_group_name='my-auto-scaling-group', lifecycle_transition='autoscaling:EC2_INSTANCE_LAUNCHING', default_result='CONTINUE')
Source code in docs_src/capo_auto_scaling/_services/async_auto_scaling.py
4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 | |