SecretValue

class aws_cdk.SecretValue(protected_value, *, stack_trace=None, type_hint=None)

Bases: Intrinsic

Work with secret values in the CDK.

Constructs that need secrets will declare parameters of type SecretValue.

The actual values of these secrets should not be committed to your repository, or even end up in the synthesized CloudFormation template. Instead, you should store them in an external system like AWS Secrets Manager or SSM Parameter Store, and you can reference them by calling SecretValue.secretsManager() or SecretValue.ssmSecure().

You can use SecretValue.unsafePlainText() to construct a SecretValue from a literal string, but doing so is highly discouraged.

To make sure secret values don’t accidentally end up in readable parts of your infrastructure definition (such as the environment variables of an AWS Lambda Function, where everyone who can read the function definition has access to the secret), using secret values directly is not allowed. You must pass them to constructs that accept SecretValue properties, which are guaranteed to use the value only in CloudFormation properties that are write-only.

If you are sure that what you are doing is safe, you can call secretValue.unsafeUnwrap() to access the protected string of the secret value.

(If you are writing something like an AWS Lambda Function and need to access a secret inside it, make the API call to GetSecretValue directly inside your Lamba’s code, instead of using environment variables.)

ExampleMetadata:

infused

Example:

# my_hosted_zone: route53.IPublicHostedZone


ses.EmailIdentity(self, "Identity",
    identity=ses.Identity.public_hosted_zone(my_hosted_zone),
    dkim_identity=ses.DkimIdentity.byo_dkim(
        private_key=SecretValue.secrets_manager("dkim-private-key"),
        public_key="...base64-encoded-public-key...",
        selector="selector"
    )
)

Construct a SecretValue (do not use!).

Do not use the constructor directly: use one of the factory functions on the class instead.

Parameters:
  • protected_value (Any) –

  • stack_trace (Optional[bool]) – Capture the stack trace of where this token is created. Default: true

  • type_hint (Optional[ResolutionTypeHint]) – Type that this token is expected to evaluate to. Default: ResolutionTypeHint.STRING

Methods

resolve(context)

Resolve the secret.

If the feature flag is not set, resolve as normal. Otherwise, throw a descriptive error that the usage guard is missing.

Parameters:

context (IResolveContext) –

Return type:

Any

to_json()

Turn this Token into JSON.

Called automatically when JSON.stringify() is called on a Token.

Return type:

Any

to_string()

Convert an instance of this Token to a string.

This method will be called implicitly by language runtimes if the object is embedded into a string. We treat it the same as an explicit stringification.

Return type:

str

to_string_list()

Convert an instance of this Token to a string list.

This method will be called implicitly by language runtimes if the object is embedded into a list. We treat it the same as an explicit stringification.

Return type:

List[str]

unsafe_unwrap()

Disable usage protection on this secret.

Call this to indicate that you want to use the secret value held by this object in an unchecked way. If you don’t call this method, using the secret value directly in a string context or as a property value somewhere will produce an error.

This method has ‘unsafe’ in the name on purpose! Make sure that the construct property you are using the returned value in is does not end up in a place in your AWS infrastructure where it could be read by anyone unexpected.

When in doubt, don’t call this method and only pass the object to constructs that accept SecretValue parameters.

Return type:

str

Attributes

creation_stack

The captured stack trace which represents the location in which this token was created.

type_hint

Type that the Intrinsic is expected to evaluate to.

Static Methods

classmethod cfn_dynamic_reference(ref)

Obtain the secret value through a CloudFormation dynamic reference.

If possible, use SecretValue.ssmSecure or SecretValue.secretsManager directly.

Parameters:

ref (CfnDynamicReference) – The dynamic reference to use.

Return type:

SecretValue

classmethod cfn_parameter(param)

Obtain the secret value through a CloudFormation parameter.

Generally, this is not a recommended approach. AWS Secrets Manager is the recommended way to reference secrets.

Parameters:

param (CfnParameter) – The CloudFormation parameter to use.

Return type:

SecretValue

classmethod is_secret_value(x)

Test whether an object is a SecretValue.

Parameters:

x (Any) –

Return type:

bool

classmethod plain_text(secret)

(deprecated) Construct a literal secret value for use with secret-aware constructs.

Do not use this method for any secrets that you care about! The value will be visible to anyone who has access to the CloudFormation template (via the AWS Console, SDKs, or CLI).

The only reasonable use case for using this method is when you are testing.

Parameters:

secret (str) –

Deprecated:

Use unsafePlainText() instead.

Stability:

deprecated

Return type:

SecretValue

classmethod resource_attribute(attr)

Use a resource’s output as secret value.

Parameters:

attr (str) –

Return type:

SecretValue

classmethod secrets_manager(secret_id, *, json_field=None, version_id=None, version_stage=None)

Creates a SecretValue with a value which is dynamically loaded from AWS Secrets Manager.

If you rotate the value in the Secret, you must also change at least one property on the resource where you are using the secret, to force CloudFormation to re-read the secret.

Parameters:
  • secret_id (str) – The ID or ARN of the secret.

  • json_field (Optional[str]) – The key of a JSON field to retrieve. This can only be used if the secret stores a JSON object. Default: - returns all the content stored in the Secrets Manager secret.

  • version_id (Optional[str]) – Specifies the unique identifier of the version of the secret you want to use. Can specify at most one of versionId and versionStage. Default: AWSCURRENT

  • version_stage (Optional[str]) – Specifies the secret version that you want to retrieve by the staging label attached to the version. Can specify at most one of versionId and versionStage. Default: AWSCURRENT

Return type:

SecretValue

classmethod ssm_secure(parameter_name, version=None)

Use a secret value stored from a Systems Manager (SSM) parameter.

This secret source in only supported in a limited set of resources and properties. Click here for the list of supported properties.

Parameters:
  • parameter_name (str) – The name of the parameter in the Systems Manager Parameter Store. The parameter name is case-sensitive.

  • version (Optional[str]) – An integer that specifies the version of the parameter to use. If you don’t specify the exact version, AWS CloudFormation uses the latest version of the parameter.

Return type:

SecretValue

classmethod unsafe_plain_text(secret)

Construct a literal secret value for use with secret-aware constructs.

Do not use this method for any secrets that you care about! The value will be visible to anyone who has access to the CloudFormation template (via the AWS Console, SDKs, or CLI).

The primary use case for using this method is when you are testing.

The other use case where this is appropriate is when constructing a JSON secret. For example, a JSON secret might have multiple fields where only some are actual secret values.

Parameters:

secret (str) –

Return type:

SecretValue

Example:

# secret: SecretValue

json_secret = {
    "username": SecretValue.unsafe_plain_text("myUsername"),
    "password": secret
}