Skip to content

get_secret_value

get_secret_value(
    secret_id: SecretIdType,
    *,
    config_overrides: Optional[SecretsManagerClientConfig] = None,
    version_id: Optional[SecretVersionIdType] = None,
    version_stage: Optional[SecretVersionStageType] = None,
) -> GetSecretValueResponse

Retrieves the contents of the encrypted fields SecretString or SecretBinary from the specified version of a secret, whichever contains content.

To retrieve the values for a group of secrets, call BatchGetSecretValue.

We recommend that you cache your secret values by using client-side caching. Caching secrets improves speed and reduces your costs. For more information, see Cache secrets for your applications.

To retrieve the previous version of a secret, use VersionStage and specify AWSPREVIOUS. To revert to the previous version of a secret, call UpdateSecretVersionStage.

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:GetSecretValue. If the secret is encrypted using a customer-managed key instead of the Amazon Web Services managed key aws/secretsmanager, then you also need kms:Decrypt permissions for that key. For more information, see IAM policy actions for Secrets Manager and Authentication and access control in Secrets Manager.

Parameters:

  • secret_id (SecretIdType) –

    The ARN or name of the secret to retrieve. To retrieve a secret from another account, you must use an ARN.

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

  • version_id (Optional[SecretVersionIdType], default: None ) –

    The unique identifier of the version of the secret to retrieve. If you include both this parameter and VersionStage, the two parameters must refer to the same secret version. If you don't specify either a VersionStage or VersionId, then Secrets Manager returns the AWSCURRENT version.

    This value is typically a UUID-type value with 32 hexadecimal digits.

  • version_stage (Optional[SecretVersionStageType], default: None ) –

    The staging label of the version of the secret to retrieve.

    Secrets Manager uses staging labels to keep track of different versions during the rotation process. If you include both this parameter and VersionId, the two parameters must refer to the same secret version. If you don't specify either a VersionStage or VersionId, Secrets Manager returns the AWSCURRENT version.

Raises:

Examples:

To retrieve the encrypted secret value of a secret The following example shows how to retrieve a secret string value.

>>> client.get_secret_value(secret_id='MyTestDatabaseSecret')
Source code in docs_src/capo_secrets_manager/_services/secrets_manager.py
def get_secret_value(
    self,
    secret_id: "capo_secrets_manager.types.secret_id_type.SecretIdType",
    *,
    config_overrides: Optional[SecretsManagerClientConfig] = None,
    version_id: Optional[
        "capo_secrets_manager.types.secret_version_id_type.SecretVersionIdType"
    ] = None,
    version_stage: Optional[
        "capo_secrets_manager.types.secret_version_stage_type.SecretVersionStageType"
    ] = None,
) -> "capo_secrets_manager.types.get_secret_value_response.GetSecretValueResponse":
    """<p>Retrieves the contents of the encrypted fields <code>SecretString</code> or <code>SecretBinary</code> from the specified version of a secret, whichever contains content.</p> <p>To retrieve the values for a group of secrets, call <a>BatchGetSecretValue</a>.</p> <p>We recommend that you cache your secret values by using client-side caching. Caching secrets improves speed and reduces your costs. For more information, see <a href="https://docs.aws.amazon.com/secretsmanager/latest/userguide/retrieving-secrets.html">Cache secrets for your applications</a>.</p> <p>To retrieve the previous version of a secret, use <code>VersionStage</code> and specify AWSPREVIOUS. To revert to the previous version of a secret, call <a href="https://docs.aws.amazon.com/cli/latest/reference/secretsmanager/update-secret-version-stage.html">UpdateSecretVersionStage</a>.</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:GetSecretValue</code>. If the secret is encrypted using a customer-managed key instead of the Amazon Web Services managed key <code>aws/secretsmanager</code>, then you also need <code>kms:Decrypt</code> permissions for that key. 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>. </p>

    Args:
        secret_id: <p>The ARN or name of the secret to retrieve. To retrieve a secret from another account, you must use an ARN.</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>
        version_id: <p>The unique identifier of the version of the secret to retrieve. If you include both this parameter and <code>VersionStage</code>, the two parameters must refer to the same secret version. If you don't specify either a <code>VersionStage</code> or <code>VersionId</code>, then Secrets Manager returns the <code>AWSCURRENT</code> version.</p> <p>This value is typically a <a href="https://wikipedia.org/wiki/Universally_unique_identifier">UUID-type</a> value with 32 hexadecimal digits.</p>
        version_stage: <p>The staging label of the version of the secret to retrieve. </p> <p>Secrets Manager uses staging labels to keep track of different versions during the rotation process. If you include both this parameter and <code>VersionId</code>, the two parameters must refer to the same secret version. If you don't specify either a <code>VersionStage</code> or <code>VersionId</code>, Secrets Manager returns the <code>AWSCURRENT</code> version.</p>

    Raises:
        capo_secrets_manager.errors.decryption_failure.DecryptionFailure: <p>Secrets Manager can't decrypt the protected secret text using the provided KMS key. </p>
        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 retrieve the encrypted secret value of a secret
        The following example shows how to retrieve a secret string value.

        >>> client.get_secret_value(secret_id='MyTestDatabaseSecret')
    """

    def _handler(
        req: "OperationRequest[capo_secrets_manager.types.get_secret_value_request.GetSecretValueRequest]",
    ) -> OperationResponse[
        "capo_secrets_manager.types.get_secret_value_response.GetSecretValueResponse"
    ]:
        import capo_secrets_manager._operations.secretsmanager.get_secret_value

        output, http_response = (
            capo_secrets_manager._operations.secretsmanager.get_secret_value.get_secret_value(
                req.options, req.input
            )
        )
        return OperationResponse(output=output, response=http_response)

    def _redact_output(
        output: "capo_secrets_manager.types.get_secret_value_response.GetSecretValueResponse",
    ) -> dict[str, Any]:
        import capo_secrets_manager.types.get_secret_value_response

        return (
            capo_secrets_manager.types.get_secret_value_response.redact_sensitive(
                output
            )
        )

    interceptors_, options_ = self.operation_options(
        config_overrides,
        OperationLogging("GetSecretValue", redact_output=_redact_output),
    )
    input_: capo_secrets_manager.types.get_secret_value_request.GetSecretValueRequest = {
        "secret_id": secret_id
    }
    if version_id is not None:
        input_["version_id"] = version_id
    if version_stage is not None:
        input_["version_stage"] = version_stage

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