Skip to content

put_lifecycle_hook

put_lifecycle_hook(
    *,
    config_overrides: Optional[AutoScalingClientConfig] = 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:

  1. (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.

  2. (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.

  3. (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.

  4. Create the lifecycle hook. Specify whether the hook is used when the instances launch or terminate.

  5. If you need more time, record the lifecycle action heartbeat to keep the instance in a wait state using the RecordLifecycleActionHeartbeat API call.

  6. 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 30 to 7200 seconds. The default value is 3600 seconds (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.

>>> 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/auto_scaling.py
def put_lifecycle_hook(
    self,
    *,
    config_overrides: Optional[AutoScalingClientConfig] = None,
    lifecycle_hook_name: Optional[
        "capo_auto_scaling.types.ascii_string_max_len255.AsciiStringMaxLen255"
    ] = None,
    auto_scaling_group_name: Optional[
        "capo_auto_scaling.types.xml_string_max_len255.XmlStringMaxLen255"
    ] = None,
    lifecycle_transition: Optional[
        "capo_auto_scaling.types.lifecycle_transition.LifecycleTransition"
    ] = None,
    role_arn: Optional[
        "capo_auto_scaling.types.xml_string_max_len255.XmlStringMaxLen255"
    ] = None,
    notification_target_arn: Optional[
        "capo_auto_scaling.types.notification_target_resource_name.NotificationTargetResourceName"
    ] = None,
    notification_metadata: Optional[
        "capo_auto_scaling.types.any_printable_ascii_string_max_len4000.AnyPrintableAsciiStringMaxLen4000"
    ] = None,
    heartbeat_timeout: Optional[
        "capo_auto_scaling.types.heartbeat_timeout.HeartbeatTimeout"
    ] = None,
    default_result: Optional[
        "capo_auto_scaling.types.lifecycle_action_result.LifecycleActionResult"
    ] = None,
) -> "capo_auto_scaling.types.put_lifecycle_hook_answer.PutLifecycleHookAnswer":
    """<p>Creates or updates a lifecycle hook for the specified Auto Scaling group.</p> <p>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.</p> <p>This step is a part of the procedure for adding a lifecycle hook to an Auto Scaling group:</p> <ol> <li> <p>(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.</p> </li> <li> <p>(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.</p> </li> <li> <p>(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.</p> </li> <li> <p> <b>Create the lifecycle hook. Specify whether the hook is used when the instances launch or terminate.</b> </p> </li> <li> <p>If you need more time, record the lifecycle action heartbeat to keep the instance in a wait state using the <a href="https://docs.aws.amazon.com/autoscaling/ec2/APIReference/API_RecordLifecycleActionHeartbeat.html">RecordLifecycleActionHeartbeat</a> API call.</p> </li> <li> <p>If you finish before the timeout period ends, send a callback by using the <a href="https://docs.aws.amazon.com/autoscaling/ec2/APIReference/API_CompleteLifecycleAction.html">CompleteLifecycleAction</a> API call.</p> </li> </ol> <p>For more information, see <a href="https://docs.aws.amazon.com/autoscaling/ec2/userguide/lifecycle-hooks.html">Amazon EC2 Auto Scaling lifecycle hooks</a> in the <i>Amazon EC2 Auto Scaling User Guide</i>.</p> <p>If you exceed your maximum limit of lifecycle hooks, which by default is 50 per Auto Scaling group, the call fails.</p> <p>You can view the lifecycle hooks for an Auto Scaling group using the <a href="https://docs.aws.amazon.com/autoscaling/ec2/APIReference/API_DescribeLifecycleHooks.html">DescribeLifecycleHooks</a> API call. If you are no longer using a lifecycle hook, you can delete it by calling the <a href="https://docs.aws.amazon.com/autoscaling/ec2/APIReference/API_DeleteLifecycleHook.html">DeleteLifecycleHook</a> API.</p>

    Args:
        lifecycle_hook_name: <p>The name of the lifecycle hook.</p>
        auto_scaling_group_name: <p>The name of the Auto Scaling group.</p>
        lifecycle_transition: <p>The lifecycle transition. For Auto Scaling groups, there are two major lifecycle transitions.</p> <ul> <li> <p>To create a lifecycle hook for scale-out events, specify <code>autoscaling:EC2_INSTANCE_LAUNCHING</code>.</p> </li> <li> <p>To create a lifecycle hook for scale-in events, specify <code>autoscaling:EC2_INSTANCE_TERMINATING</code>.</p> </li> </ul> <p>Required for new lifecycle hooks, but optional when updating existing hooks.</p>
        role_arn: <p>The ARN of the IAM role that allows the Auto Scaling group to publish to the specified notification target.</p> <p>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.</p>
        notification_target_arn: <p>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.</p> <p>If you specify an empty string, this overrides the current ARN.</p> <p>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.</p> <p>When you specify a notification target, Amazon EC2 Auto Scaling sends it a test message. Test messages contain the following additional key-value pair: <code>"Event": "autoscaling:TEST_NOTIFICATION"</code>.</p>
        notification_metadata: <p>Additional information that you want to include any time Amazon EC2 Auto Scaling sends a message to the notification target.</p>
        heartbeat_timeout: <p>The maximum time, in seconds, that can elapse before the lifecycle hook times out. The range is from <code>30</code> to <code>7200</code> seconds. The default value is <code>3600</code> seconds (1 hour).</p>
        default_result: <p>The action the Auto Scaling group takes when the lifecycle hook timeout elapses or if an unexpected failure occurs. The default value is <code>ABANDON</code>.</p> <p>Valid values: <code>CONTINUE</code> | <code>ABANDON</code> </p>

    Raises:
        capo_auto_scaling.errors.limit_exceeded_fault.LimitExceededFault: <p>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 <a href="https://docs.aws.amazon.com/autoscaling/ec2/APIReference/API_DescribeAccountLimits.html">DescribeAccountLimits</a> in the <i>Amazon EC2 Auto Scaling API Reference</i>.</p>
        capo_auto_scaling.errors.resource_contention_fault.ResourceContentionFault: <p>You already have a pending update to an Amazon EC2 Auto Scaling resource (for example, an Auto Scaling group, instance, or load balancer).</p>
        capo_auto_scaling.errors.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.

        >>> 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')
    """

    def _handler(
        req: "OperationRequest[capo_auto_scaling.types.put_lifecycle_hook_type.PutLifecycleHookType]",
    ) -> OperationResponse[
        "capo_auto_scaling.types.put_lifecycle_hook_answer.PutLifecycleHookAnswer"
    ]:
        import capo_auto_scaling._operations.auto_scaling_2011_01_01.put_lifecycle_hook

        output, http_response = (
            capo_auto_scaling._operations.auto_scaling_2011_01_01.put_lifecycle_hook.put_lifecycle_hook(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("PutLifecycleHook")
    )
    input_: capo_auto_scaling.types.put_lifecycle_hook_type.PutLifecycleHookType = {}
    if lifecycle_hook_name is not None:
        input_["lifecycle_hook_name"] = lifecycle_hook_name
    if auto_scaling_group_name is not None:
        input_["auto_scaling_group_name"] = auto_scaling_group_name
    if lifecycle_transition is not None:
        input_["lifecycle_transition"] = lifecycle_transition
    if role_arn is not None:
        input_["role_arn"] = role_arn
    if notification_target_arn is not None:
        input_["notification_target_arn"] = notification_target_arn
    if notification_metadata is not None:
        input_["notification_metadata"] = notification_metadata
    if heartbeat_timeout is not None:
        input_["heartbeat_timeout"] = heartbeat_timeout
    if default_result is not None:
        input_["default_result"] = default_result

    response = execute_pipeline(
        OperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    response.response.close()
    return response.output