Skip to content

rotate_secret async

rotate_secret(
    secret_id: SecretIdType,
    *,
    config_overrides: Optional[AsyncSecretsManagerClientConfig] = None,
    client_request_token: Optional[ClientRequestTokenType] = None,
    rotation_lambda_arn: Optional[RotationLambdaARNType] = None,
    rotation_rules: Optional[RotationRulesType] = None,
    external_secret_rotation_metadata: Optional[
        ExternalSecretRotationMetadataType
    ] = None,
    external_secret_rotation_role_arn: Optional[RoleARNType] = None,
    rotate_immediately: Optional[BooleanType] = None,
) -> RotateSecretResponse

Configures and starts the asynchronous process of rotating the secret. For information about rotation, see Rotate secrets in the Secrets Manager User Guide. If you include the configuration parameters, the operation sets the values for the secret and then immediately starts a rotation. If you don't include the configuration parameters, the operation starts a rotation with the values already stored in the secret.

When rotation is successful, the AWSPENDING staging label might be attached to the same version as the AWSCURRENT version, or it might not be attached to any version. If the AWSPENDING staging label is present but not attached to the same version as AWSCURRENT, then any later invocation of RotateSecret assumes that a previous rotation request is still in progress and returns an error. When rotation is unsuccessful, the AWSPENDING staging label might be attached to an empty secret version. For more information, see Troubleshoot rotation in the Secrets Manager User Guide.

Secrets Manager generates a CloudTrail log entry when you call this action. Do not include sensitive information in request parameters because it might be logged. For more information, see Logging Secrets Manager events with CloudTrail.

Required permissions: secretsmanager:RotateSecret. For more information, see IAM policy actions for Secrets Manager and Authentication and access control in Secrets Manager. You also need lambda:InvokeFunction permissions on the rotation function. For more information, see Permissions for rotation.

Parameters:

  • secret_id (SecretIdType) –

    The ARN or name of the secret to rotate.

    For an ARN, we recommend that you specify a complete ARN rather than a partial ARN. See Finding a secret from a partial ARN.

  • client_request_token (Optional[ClientRequestTokenType], default: None ) –

    A unique identifier for the new version of the secret. You only need to specify this value if you implement your own retry logic and you want to ensure that Secrets Manager doesn't attempt to create a secret version twice.

    If you use the Amazon Web Services CLI or one of the Amazon Web Services SDKs to call this operation, then you can leave this parameter empty. The CLI or SDK generates a random UUID for you and includes it as the value for this parameter in the request.

    If you generate a raw HTTP request to the Secrets Manager service endpoint, then you must generate a ClientRequestToken and include it in the request.

    This value helps ensure idempotency. Secrets Manager uses this value to prevent the accidental creation of duplicate versions if there are failures and retries during a rotation. We recommend that you generate a UUID-type value to ensure uniqueness of your versions within the specified secret.

  • rotation_lambda_arn (Optional[RotationLambdaARNType], default: None ) –

    For secrets that use a Lambda rotation function to rotate, the ARN of the Lambda rotation function.

    For secrets that use managed rotation, omit this field. For more information, see Managed rotation in the Secrets Manager User Guide.

  • rotation_rules (Optional[RotationRulesType], default: None ) –

    A structure that defines the rotation configuration for this secret.

    When changing an existing rotation schedule and setting RotateImmediately to false:

    • If using AutomaticallyAfterDays or a ScheduleExpression with rate(), the previously scheduled rotation might still occur.

    • To prevent unintended rotations, use a ScheduleExpression with cron() for granular control over rotation windows.

  • external_secret_rotation_metadata (Optional[ExternalSecretRotationMetadataType], default: None ) –

    The metadata needed to successfully rotate a managed external secret. A list of key value pairs in JSON format specified by the partner. For more information about the required information, see Using Secrets Manager managed external secrets

  • external_secret_rotation_role_arn (Optional[RoleARNType], default: None ) –

    The Amazon Resource Name (ARN) of the role that allows Secrets Manager to rotate a secret held by a third-party partner. For more information, see Security and permissions.

  • rotate_immediately (Optional[BooleanType], default: None ) –

    Specifies whether to rotate the secret immediately or wait until the next scheduled rotation window. The rotation schedule is defined in RotateSecretRequest$RotationRules.

    The default for RotateImmediately is true. If you don't specify this value, Secrets Manager rotates the secret immediately.

    If you set RotateImmediately to false, Secrets Manager tests the rotation configuration by running the testSecret step of the Lambda rotation function. This test creates an AWSPENDING version of the secret and then removes it.

    When changing an existing rotation schedule and setting RotateImmediately to false:

    • If using AutomaticallyAfterDays or a ScheduleExpression with rate(), the previously scheduled rotation might still occur.

    • To prevent unintended rotations, use a ScheduleExpression with cron() for granular control over rotation windows.

    Rotation is an asynchronous process. For more information, see How rotation works.

Raises:

Examples:

To configure rotation for a secret The following example configures rotation for a secret using a cron expression. The first rotation happens immediately after the changes are stored in the secret. The rotation schedule is the first and 15th day of every month. The rotation window begins at 4:00 PM UTC and ends at 6:00 PM.

>>> await client.rotate_secret(secret_id='MyTestDatabaseSecret', rotation_lambda_arn='arn:aws:lambda:us-west-2:123456789012:function:MyTestDatabaseRotationLambda', rotation_rules={'ScheduleExpression': 'cron(0 16 1,15 * ? *)', 'Duration': '2h'})
To request an immediate rotation for a secret
The following example requests an immediate invocation of the secret's Lambda rotation function. It assumes that the specified secret already has rotation configured. The rotation function runs asynchronously in the background.
>>> await client.rotate_secret(secret_id='MyTestDatabaseSecret')
Source code in docs_src/capo_secrets_manager/_services/async_secrets_manager.py
async def rotate_secret(
    self,
    secret_id: "capo_secrets_manager.types.secret_id_type.SecretIdType",
    *,
    config_overrides: Optional[AsyncSecretsManagerClientConfig] = None,
    client_request_token: Optional[
        "capo_secrets_manager.types.client_request_token_type.ClientRequestTokenType"
    ] = None,
    rotation_lambda_arn: Optional[
        "capo_secrets_manager.types.rotation_lambda_arn_type.RotationLambdaARNType"
    ] = None,
    rotation_rules: Optional[
        "capo_secrets_manager.types.rotation_rules_type.RotationRulesType"
    ] = None,
    external_secret_rotation_metadata: Optional[
        "capo_secrets_manager.types.external_secret_rotation_metadata_type.ExternalSecretRotationMetadataType"
    ] = None,
    external_secret_rotation_role_arn: Optional[
        "capo_secrets_manager.types.role_arn_type.RoleARNType"
    ] = None,
    rotate_immediately: Optional[
        "capo_secrets_manager.types.boolean_type.BooleanType"
    ] = None,
) -> "capo_secrets_manager.types.rotate_secret_response.RotateSecretResponse":
    """<p>Configures and starts the asynchronous process of rotating the secret. For information about rotation, see <a href="https://docs.aws.amazon.com/secretsmanager/latest/userguide/rotating-secrets.html">Rotate secrets</a> in the <i>Secrets Manager User Guide</i>. If you include the configuration parameters, the operation sets the values for the secret and then immediately starts a rotation. If you don't include the configuration parameters, the operation starts a rotation with the values already stored in the secret. </p> <p>When rotation is successful, the <code>AWSPENDING</code> staging label might be attached to the same version as the <code>AWSCURRENT</code> version, or it might not be attached to any version. If the <code>AWSPENDING</code> staging label is present but not attached to the same version as <code>AWSCURRENT</code>, then any later invocation of <code>RotateSecret</code> assumes that a previous rotation request is still in progress and returns an error. When rotation is unsuccessful, the <code>AWSPENDING</code> staging label might be attached to an empty secret version. For more information, see <a href="https://docs.aws.amazon.com/secretsmanager/latest/userguide/troubleshoot_rotation.html">Troubleshoot rotation</a> in the <i>Secrets Manager User Guide</i>.</p> <p>Secrets Manager generates a CloudTrail log entry when you call this action. Do not include sensitive information in request parameters because it might be logged. For more information, see <a href="https://docs.aws.amazon.com/secretsmanager/latest/userguide/retrieve-ct-entries.html">Logging Secrets Manager events with CloudTrail</a>.</p> <p> <b>Required permissions: </b> <code>secretsmanager:RotateSecret</code>. For more information, see <a href="https://docs.aws.amazon.com/secretsmanager/latest/userguide/reference_iam-permissions.html#reference_iam-permissions_actions"> IAM policy actions for Secrets Manager</a> and <a href="https://docs.aws.amazon.com/secretsmanager/latest/userguide/auth-and-access.html">Authentication and access control in Secrets Manager</a>. You also need <code>lambda:InvokeFunction</code> permissions on the rotation function. For more information, see <a href="https://docs.aws.amazon.com/secretsmanager/latest/userguide/rotating-secrets-required-permissions-function.html"> Permissions for rotation</a>.</p>

    Args:
        secret_id: <p>The ARN or name of the secret to rotate.</p> <p>For an ARN, we recommend that you specify a complete ARN rather than a partial ARN. See <a href="https://docs.aws.amazon.com/secretsmanager/latest/userguide/troubleshoot.html#ARN_secretnamehyphen">Finding a secret from a partial ARN</a>.</p>
        client_request_token: <p>A unique identifier for the new version of the secret. You only need to specify this value if you implement your own retry logic and you want to ensure that Secrets Manager doesn't attempt to create a secret version twice.</p> <note> <p>If you use the Amazon Web Services CLI or one of the Amazon Web Services SDKs to call this operation, then you can leave this parameter empty. The CLI or SDK generates a random UUID for you and includes it as the value for this parameter in the request. </p> </note> <p>If you generate a raw HTTP request to the Secrets Manager service endpoint, then you must generate a <code>ClientRequestToken</code> and include it in the request.</p> <p>This value helps ensure idempotency. Secrets Manager uses this value to prevent the accidental creation of duplicate versions if there are failures and retries during a rotation. We recommend that you generate a <a href="https://wikipedia.org/wiki/Universally_unique_identifier">UUID-type</a> value to ensure uniqueness of your versions within the specified secret. </p>
        rotation_lambda_arn: <p>For secrets that use a Lambda rotation function to rotate, the ARN of the Lambda rotation function. </p> <p>For secrets that use <i>managed rotation</i>, omit this field. For more information, see <a href="https://docs.aws.amazon.com/secretsmanager/latest/userguide/rotate-secrets_managed.html">Managed rotation</a> in the <i>Secrets Manager User Guide</i>.</p>
        rotation_rules: <p>A structure that defines the rotation configuration for this secret.</p> <important> <p>When changing an existing rotation schedule and setting <code>RotateImmediately</code> to <code>false</code>:</p> <ul> <li> <p>If using <code>AutomaticallyAfterDays</code> or a <code>ScheduleExpression</code> with <code>rate()</code>, the previously scheduled rotation might still occur.</p> </li> <li> <p>To prevent unintended rotations, use a <code>ScheduleExpression</code> with <code>cron()</code> for granular control over rotation windows.</p> </li> </ul> </important>
        external_secret_rotation_metadata: <p>The metadata needed to successfully rotate a managed external secret. A list of key value pairs in JSON format specified by the partner. For more information about the required information, see <a href="https://docs.aws.amazon.com/secretsmanager/latest/userguide/managed-external-secrets.html">Using Secrets Manager managed external secrets</a> </p>
        external_secret_rotation_role_arn: <p>The Amazon Resource Name (ARN) of the role that allows Secrets Manager to rotate a secret held by a third-party partner. For more information, see <a href="https://docs.aws.amazon.com/secretsmanager/latest/userguide/mes-security.html">Security and permissions</a>.</p>
        rotate_immediately: <p>Specifies whether to rotate the secret immediately or wait until the next scheduled rotation window. The rotation schedule is defined in <a>RotateSecretRequest$RotationRules</a>.</p> <p>The default for <code>RotateImmediately</code> is <code>true</code>. If you don't specify this value, Secrets Manager rotates the secret immediately.</p> <p>If you set <code>RotateImmediately</code> to <code>false</code>, Secrets Manager tests the rotation configuration by running the <a href="https://docs.aws.amazon.com/secretsmanager/latest/userguide/rotate-secrets_how.html"> <code>testSecret</code> step</a> of the Lambda rotation function. This test creates an <code>AWSPENDING</code> version of the secret and then removes it.</p> <p>When changing an existing rotation schedule and setting <code>RotateImmediately</code> to <code>false</code>:</p> <ul> <li> <p>If using <code>AutomaticallyAfterDays</code> or a <code>ScheduleExpression</code> with <code>rate()</code>, the previously scheduled rotation might still occur.</p> </li> <li> <p>To prevent unintended rotations, use a <code>ScheduleExpression</code> with <code>cron()</code> for granular control over rotation windows.</p> </li> </ul> <p>Rotation is an asynchronous process. For more information, see <a href="https://docs.aws.amazon.com/secretsmanager/latest/userguide/rotate-secrets_how.html">How rotation works</a>.</p>

    Raises:
        capo_secrets_manager.errors.internal_service_error.InternalServiceError: <p>An error occurred on the server side.</p>
        capo_secrets_manager.errors.invalid_parameter_exception.InvalidParameterException: <p>The parameter name or value is invalid.</p>
        capo_secrets_manager.errors.invalid_request_exception.InvalidRequestException: <p>A parameter value is not valid for the current state of the resource.</p> <p>Possible causes:</p> <ul> <li> <p>The secret is scheduled for deletion.</p> </li> <li> <p>You tried to enable rotation on a secret that doesn't already have a Lambda function ARN configured and you didn't include such an ARN as a parameter in this call. </p> </li> <li> <p>The secret is managed by another service, and you must use that service to update it. For more information, see <a href="https://docs.aws.amazon.com/secretsmanager/latest/userguide/service-linked-secrets.html">Secrets managed by other Amazon Web Services services</a>.</p> </li> </ul>
        capo_secrets_manager.errors.resource_not_found_exception.ResourceNotFoundException: <p>Secrets Manager can't find the resource that you asked for.</p>
        capo_secrets_manager.errors.UnknownServiceError: The service returned an error code this client does not model.

    Examples:
        To configure rotation for a secret
        The following example configures rotation for a secret using a cron expression. The first rotation happens immediately after the changes are stored in the secret. The rotation schedule is the first and 15th day of every month. The rotation window begins at 4:00 PM UTC and ends at 6:00 PM.

        >>> await client.rotate_secret(secret_id='MyTestDatabaseSecret', rotation_lambda_arn='arn:aws:lambda:us-west-2:123456789012:function:MyTestDatabaseRotationLambda', rotation_rules={'ScheduleExpression': 'cron(0 16 1,15 * ? *)', 'Duration': '2h'})
        To request an immediate rotation for a secret
        The following example requests an immediate invocation of the secret's Lambda rotation function. It assumes that the specified secret already has rotation configured. The rotation function runs asynchronously in the background.

        >>> await client.rotate_secret(secret_id='MyTestDatabaseSecret')
    """

    async def _handler(
        req: "AsyncOperationRequest[capo_secrets_manager.types.rotate_secret_request.RotateSecretRequest]",
    ) -> AsyncOperationResponse[
        "capo_secrets_manager.types.rotate_secret_response.RotateSecretResponse"
    ]:
        import capo_secrets_manager._operations.secretsmanager.rotate_secret

        (
            output,
            http_response,
        ) = await capo_secrets_manager._operations.secretsmanager.rotate_secret.async_rotate_secret(
            req.options, req.input
        )
        return AsyncOperationResponse(output=output, response=http_response)

    interceptors_, options_ = self.operation_options(
        config_overrides, OperationLogging("RotateSecret")
    )
    input_: capo_secrets_manager.types.rotate_secret_request.RotateSecretRequest = {
        "secret_id": secret_id
    }
    if client_request_token is None:
        client_request_token = str(uuid.uuid4())
    input_["client_request_token"] = client_request_token
    if rotation_lambda_arn is not None:
        input_["rotation_lambda_arn"] = rotation_lambda_arn
    if rotation_rules is not None:
        input_["rotation_rules"] = rotation_rules
    if external_secret_rotation_metadata is not None:
        input_["external_secret_rotation_metadata"] = (
            external_secret_rotation_metadata
        )
    if external_secret_rotation_role_arn is not None:
        input_["external_secret_rotation_role_arn"] = (
            external_secret_rotation_role_arn
        )
    if rotate_immediately is not None:
        input_["rotate_immediately"] = rotate_immediately

    response = await aexecute_pipeline(
        AsyncOperationRequest(input=input_, options=options_),
        handler=_handler,
        interceptors=list(interceptors_),
    )
    await response.response.aclose()
    return response.output