Secret

class aws_cdk.aws_secretsmanager.Secret(scope, id, *, description=None, encryption_key=None, generate_secret_string=None, removal_policy=None, replica_regions=None, secret_name=None, secret_object_value=None, secret_string_beta1=None, secret_string_value=None)

Bases: aws_cdk.Resource

Creates a new secret in AWS SecretsManager.

ExampleMetadata

infused

Example:

# stack: Stack
user = iam.User(stack, "User")
access_key = iam.AccessKey(stack, "AccessKey", user=user)

secretsmanager.Secret(stack, "Secret",
    secret_object_value={
        "username": SecretValue.unsafe_plain_text(user.user_name),
        "database": SecretValue.unsafe_plain_text("foo"),
        "password": access_key.secret_access_key
    }
)
Parameters
  • scope (Construct) –

  • id (str) –

  • description (Optional[str]) – An optional, human-friendly description of the secret. Default: - No description.

  • encryption_key (Optional[IKey]) – The customer-managed encryption key to use for encrypting the secret value. Default: - A default KMS key for the account and region is used.

  • generate_secret_string (Union[SecretStringGenerator, Dict[str, Any], None]) – Configuration for how to generate a secret value. Only one of secretString and generateSecretString can be provided. Default: - 32 characters with upper-case letters, lower-case letters, punctuation and numbers (at least one from each category), per the default values of SecretStringGenerator.

  • removal_policy (Optional[RemovalPolicy]) – Policy to apply when the secret is removed from this stack. Default: - Not set.

  • replica_regions (Optional[Sequence[Union[ReplicaRegion, Dict[str, Any]]]]) – A list of regions where to replicate this secret. Default: - Secret is not replicated

  • secret_name (Optional[str]) – A name for the secret. Note that deleting secrets from SecretsManager does not happen immediately, but after a 7 to 30 days blackout period. During that period, it is not possible to create another secret that shares the same name. Default: - A name is generated by CloudFormation.

  • secret_object_value (Optional[Mapping[str, SecretValue]]) – Initial value for a JSON secret. NOTE: It is **highly* encouraged to leave this field undefined and allow SecretsManager to create the secret value. The secret object – if provided – will be included in the output of the cdk as part of synthesis, and will appear in the CloudFormation template in the console. This can be secure(-ish) if that value is merely reference to another resource (or one of its attributes), but if the value is a plaintext string, it will be visible to anyone with access to the CloudFormation template (via the AWS Console, SDKs, or CLI). Specifies a JSON object that you want to encrypt and store in this new version of the secret. To specify a simple string value instead, use {@link SecretProps.secretStringValue} Only one of secretStringBeta1, secretStringValue, ‘secretObjectValue’, and generateSecretString can be provided. Default: - SecretsManager generates a new secret value.

  • secret_string_beta1 (Optional[SecretStringValueBeta1]) – (deprecated) Initial value for the secret. NOTE: It is **highly* encouraged to leave this field undefined and allow SecretsManager to create the secret value. The secret string – if provided – will be included in the output of the cdk as part of synthesis, and will appear in the CloudFormation template in the console. This can be secure(-ish) if that value is merely reference to another resource (or one of its attributes), but if the value is a plaintext string, it will be visible to anyone with access to the CloudFormation template (via the AWS Console, SDKs, or CLI). Specifies text data that you want to encrypt and store in this new version of the secret. May be a simple string value, or a string representation of a JSON structure. Only one of secretStringBeta1, secretStringValue, and generateSecretString can be provided. Default: - SecretsManager generates a new secret value.

  • secret_string_value (Optional[SecretValue]) – Initial value for the secret. NOTE: It is **highly* encouraged to leave this field undefined and allow SecretsManager to create the secret value. The secret string – if provided – will be included in the output of the cdk as part of synthesis, and will appear in the CloudFormation template in the console. This can be secure(-ish) if that value is merely reference to another resource (or one of its attributes), but if the value is a plaintext string, it will be visible to anyone with access to the CloudFormation template (via the AWS Console, SDKs, or CLI). Specifies text data that you want to encrypt and store in this new version of the secret. May be a simple string value. To provide a string representation of JSON structure, use {@link SecretProps.secretObjectValue} instead. Only one of secretStringBeta1, secretStringValue, ‘secretObjectValue’, and generateSecretString can be provided. Default: - SecretsManager generates a new secret value.

Methods

add_replica_region(region, encryption_key=None)

Adds a replica region for the secret.

Parameters
  • region (str) – The name of the region.

  • encryption_key (Optional[IKey]) – The customer-managed encryption key to use for encrypting the secret value.

Return type

None

add_rotation_schedule(id, *, automatically_after=None, hosted_rotation=None, rotation_lambda=None)

Adds a rotation schedule to the secret.

Parameters
  • id (str) –

  • automatically_after (Optional[Duration]) – Specifies the number of days after the previous rotation before Secrets Manager triggers the next automatic rotation. A value of zero will disable automatic rotation - Duration.days(0). Default: Duration.days(30)

  • hosted_rotation (Optional[HostedRotation]) – Hosted rotation. Default: - either rotationLambda or hostedRotation must be specified

  • rotation_lambda (Optional[IFunction]) – A Lambda function that can rotate the secret. Default: - either rotationLambda or hostedRotation must be specified

Return type

RotationSchedule

add_to_resource_policy(statement)

Adds a statement to the IAM resource policy associated with this secret.

If this secret was created in this stack, a resource policy will be automatically created upon the first call to addToResourcePolicy. If the secret is imported, then this is a no-op.

Parameters

statement (PolicyStatement) –

Return type

AddToResourcePolicyResult

apply_removal_policy(policy)

Apply the given removal policy to this resource.

The Removal Policy controls what happens to this resource when it stops being managed by CloudFormation, either because you’ve removed it from the CDK application or because you’ve made a change that requires the resource to be replaced.

The resource can be deleted (RemovalPolicy.DESTROY), or left in your AWS account for data recovery and cleanup later (RemovalPolicy.RETAIN).

Parameters

policy (RemovalPolicy) –

Return type

None

attach(target)

Attach a target to this secret.

Parameters

target (ISecretAttachmentTarget) – The target to attach.

Return type

ISecret

Returns

An attached secret

deny_account_root_delete()

Denies the DeleteSecret action to all principals within the current account.

Return type

None

grant_read(grantee, version_stages=None)

Grants reading the secret value to some role.

Parameters
  • grantee (IGrantable) –

  • version_stages (Optional[Sequence[str]]) –

Return type

Grant

grant_write(grantee)

Grants writing and updating the secret value to some role.

Parameters

grantee (IGrantable) –

Return type

Grant

secret_value_from_json(json_field)

Interpret the secret as a JSON object and return a field’s value from it as a SecretValue.

Parameters

json_field (str) –

Return type

SecretValue

to_string()

Returns a string representation of this construct.

Return type

str

Attributes

encryption_key

The customer-managed encryption key that is used to encrypt this secret, if any.

When not specified, the default KMS key for the account and region is being used.

Return type

Optional[IKey]

env

The environment this resource belongs to.

For resources that are created and managed by the CDK (generally, those created by creating new class instances like Role, Bucket, etc.), this is always the same as the environment of the stack they belong to; however, for imported resources (those obtained from static methods like fromRoleArn, fromBucketName, etc.), that might be different than the stack they were imported into.

Return type

ResourceEnvironment

exclude_characters

The string of the characters that are excluded in this secret when it is generated.

Return type

Optional[str]

node

The tree node.

Return type

Node

secret_arn

The ARN of the secret in AWS Secrets Manager.

Will return the full ARN if available, otherwise a partial arn. For secrets imported by the deprecated fromSecretName, it will return the secretName.

Return type

str

secret_full_arn

The full ARN of the secret in AWS Secrets Manager, which is the ARN including the Secrets Manager-supplied 6-character suffix.

This is equal to secretArn in most cases, but is undefined when a full ARN is not available (e.g., secrets imported by name).

Return type

Optional[str]

secret_name

The name of the secret.

For “owned” secrets, this will be the full resource name (secret name + suffix), unless the @aws-cdk/aws-secretsmanager:parseOwnedSecretName’ feature flag is set.

Return type

str

secret_value

Retrieve the value of the stored secret as a SecretValue.

Return type

SecretValue

stack

The stack in which this resource is defined.

Return type

Stack

Static Methods

classmethod from_secret_attributes(scope, id, *, encryption_key=None, secret_complete_arn=None, secret_partial_arn=None)

Import an existing secret into the Stack.

Parameters
  • scope (Construct) – the scope of the import.

  • id (str) – the ID of the imported Secret in the construct tree.

  • encryption_key (Optional[IKey]) – The encryption key that is used to encrypt the secret, unless the default SecretsManager key is used.

  • secret_complete_arn (Optional[str]) – The complete ARN of the secret in SecretsManager. This is the ARN including the Secrets Manager 6-character suffix. Cannot be used with secretArn or secretPartialArn.

  • secret_partial_arn (Optional[str]) – The partial ARN of the secret in SecretsManager. This is the ARN without the Secrets Manager 6-character suffix. Cannot be used with secretArn or secretCompleteArn.

Return type

ISecret

classmethod from_secret_complete_arn(scope, id, secret_complete_arn)

Imports a secret by complete ARN.

The complete ARN is the ARN with the Secrets Manager-supplied suffix.

Parameters
  • scope (Construct) –

  • id (str) –

  • secret_complete_arn (str) –

Return type

ISecret

classmethod from_secret_name_v2(scope, id, secret_name)

Imports a secret by secret name.

A secret with this name must exist in the same account & region. Replaces the deprecated fromSecretName.

Parameters
  • scope (Construct) –

  • id (str) –

  • secret_name (str) –

Return type

ISecret

classmethod from_secret_partial_arn(scope, id, secret_partial_arn)

Imports a secret by partial ARN.

The partial ARN is the ARN without the Secrets Manager-supplied suffix.

Parameters
  • scope (Construct) –

  • id (str) –

  • secret_partial_arn (str) –

Return type

ISecret

classmethod is_construct(x)

Checks if x is a construct.

Use this method instead of instanceof to properly detect Construct instances, even when the construct library is symlinked.

Explanation: in JavaScript, multiple copies of the constructs library on disk are seen as independent, completely different libraries. As a consequence, the class Construct in each copy of the constructs library is seen as a different class, and an instance of one class will not test as instanceof the other class. npm install will not create installations like this, but users may manually symlink construct libraries together or use a monorepo tool: in those cases, multiple copies of the constructs library can be accidentally installed, and instanceof will behave unpredictably. It is safest to avoid using instanceof, and using this type-testing method instead.

Parameters

x (Any) – Any object.

Return type

bool

Returns

true if x is an object created from a class which extends Construct.

classmethod is_owned_resource(construct)

Returns true if the construct was created by CDK, and false otherwise.

Parameters

construct (IConstruct) –

Return type

bool

classmethod is_resource(construct)

Check whether the given construct is a Resource.

Parameters

construct (IConstruct) –

Return type

bool

classmethod is_secret(x)

Return whether the given object is a Secret.

Parameters

x (Any) –

Return type

bool