AWS::SecretsManager::Secret - AWS CloudFormation

AWS::SecretsManager::Secret

The AWS::SecretsManager::Secret resource creates a secret and stores it in Secrets Manager. For more information, see Secret in the AWS Secrets Manager User Guide, and the CreateSecret API in the AWS Secrets Manager API Reference.

To specify the SecretString encrypted value for the secret, specify either the SecretString or the GenerateSecretString property in this resource. You must specify one or the other, but you can't specify both. See the first two examples later in this topic.

Note

You can't generate a secret with a SecretBinary secret value using AWS CloudFormation. You can only create a SecretString text-based secret value.

Note

Do not create a dynamic reference using a backslash (\) as the final value. AWS CloudFormation cannot resolve those references, which causes a resource failure.

After you create the basic secret, you can do any of the following:

Syntax

To declare this entity in your AWS CloudFormation template, use the following syntax:

JSON

{ "Type" : "AWS::SecretsManager::Secret", "Properties" : { "Description" : String, "GenerateSecretString" : GenerateSecretString, "KmsKeyId" : String, "Name" : String, "ReplicaRegions" : [ ReplicaRegion, ... ], "SecretString" : String, "Tags" : [ Tag, ... ] } }

YAML

Type: AWS::SecretsManager::Secret Properties: Description: String GenerateSecretString: GenerateSecretString KmsKeyId: String Name: String ReplicaRegions: - ReplicaRegion SecretString: String Tags: - Tag

Properties

Description

(Optional) Specifies a user-provided description of the secret.

Required: No

Type: String

Maximum: 2048

Update requires: No interruption

GenerateSecretString

(Optional) Specifies text data that you want to encrypt and store in this new version of the secret.

Either SecretString or SecretBinary must have a value, but not both. They cannot both be empty.

If you create a secret by using the Secrets Manager console then Secrets Manager puts the protected secret text in only the SecretString parameter. The Secrets Manager console stores the information as a JSON structure of key/value pairs that the Lambda rotation function knows how to parse.

For storing multiple values, we recommend that you use a JSON text string argument and specify key/value pairs. For information on how to format a JSON parameter for the various command line tool environments, see Using JSON for Parameters in the AWS CLI User Guide. For example:

{"username":"bob","password":"abc123xyz456"}

If your command-line tool or SDK requires quotation marks around the parameter, you should use single quotes to avoid confusion with the double quotes required in the JSON text.

Required: No

Type: GenerateSecretString

Minimum: 0

Maximum: 65536

Update requires: No interruption

KmsKeyId

(Optional) Specifies the ARN, Key ID, or alias of the AWS KMS customer master key (CMK) used to encrypt the SecretString or SecretBinary values for versions of this secret. If you don't specify this value, then Secrets Manager defaults to the AWS account CMK, aws/secretsmanager. If an AWS KMS CMK with that name doesn't exist, Secrets Manager creates the CMK for you automatically the first time it encrypts a version SecretString or SecretBinary fields.

Important

You can use the account default CMK to encrypt and decrypt only if you call this operation using credentials from the same account that owns the secret. If you use a secret from a different account, then you must create a custom CMK and specify the ARN in this field.

Required: No

Type: String

Minimum: 0

Maximum: 2048

Update requires: No interruption

Name

The friendly name of the secret. You can use forward slashes in the name to represent a path hierarchy. For example, /prod/databases/dbserver1 could represent the secret for a server named dbserver1 in the folder databases in the folder prod.

Required: No

Type: String

Minimum: 1

Maximum: 256

Update requires: Replacement

ReplicaRegions

(Optional) A list of ReplicaRegion objects. The ReplicaRegion type consists of a Region (required) and the KmsKeyId which can be an ARN, Key ID, or Alias.

Required: No

Type: List of ReplicaRegion

Update requires: No interruption

SecretString

(Optional) Specifies text data that you want to encrypt and store in this new version of the secret.

Either SecretString or SecretBinary must have a value, but not both. They cannot both be empty.

If you create a secret by using the Secrets Manager console then Secrets Manager puts the protected secret text in only the SecretString parameter. The Secrets Manager console stores the information as a JSON structure of key/value pairs that the Lambda rotation function knows how to parse.

For storing multiple values, we recommend that you use a JSON text string argument and specify key/value pairs. For information on how to format a JSON parameter for the various command line tool environments, see Using JSON for Parameters in the AWS CLI User Guide. For example:

{"username":"bob","password":"abc123xyz456"}

If your command-line tool or SDK requires quotation marks around the parameter, you should use single quotes to avoid confusion with the double quotes required in the JSON text.

Required: No

Type: String

Minimum: 0

Maximum: 65536

Update requires: No interruption

Tags

The list of user-defined tags associated with the secret. Use tags to manage your AWS resources. For additional information about tags, see TagResource.

Required: No

Type: List of Tag

Update requires: No interruption

Return values

Ref

When you pass the logical ID of an AWS::SecretsManager::Secret resource to the intrinsic Ref function, the function returns the ARN of the secret configured such as:

arn:aws:secretsmanager:us-west-2:123456789012:secret:my-path/my-secret-name-1a2b3c

If you know the ARN of a secret, you can reference a secret you created in one part of the stack template from within the definition of another resource in the same template. You typically use the Ref function with the AWS::SecretsManager::SecretTargetAttachment resource type to get references to both the secret and its associated database.

For more information about using the Ref function, see Ref.

Examples

Creating a Secret with a Dynamically Generated Password

The following example creates a secret, constructing the secret value from a string template combined with a dynamically generated random password. The result of this example is a SecretString value that looks like the following:

{"username": "test-user", "password": "rzDtILsQNfmmHwkJBPsTVhkRvWRtSn" )

JSON

{ "MySecretA": { "Type": "AWS::SecretsManager::Secret", "Properties": { "Name": "MySecretForAppA", "Description": "This secret has a dynamically generated secret password.", "GenerateSecretString": { "SecretStringTemplate": "{\"username\":\"test-user\"}", "GenerateStringKey": "password", "PasswordLength": 30, "ExcludeCharacters": "\"@/\\" }, "Tags": [ { "Key": "AppName", "Value": "AppA" } ] } } }

YAML

#This is a Secret resource with a randomly generated password in its SecretString JSON. MySecretA: Type: 'AWS::SecretsManager::Secret' Properties: Name: MySecretForAppA Description: "This secret has a dynamically generated secret password." GenerateSecretString: SecretStringTemplate: '{"username": "test-user"}' GenerateStringKey: "password" PasswordLength: 30 ExcludeCharacters: '"@/\' Tags: - Key: AppName Value: AppA

Creating a Secret with a Hardcoded Password

The following example creates a secret and provides the secret value as a literal string stored in the secret. We recommend that you don't hardcode your password this way. Instead use the SecretsManager Secret GenerateSecretString property. See the previous example for the recommended option.

JSON

{ "MySecretB": { "Type": "AWS::SecretsManager::Secret", "Properties": { "Name": "MySecretForAppB", "Description": "This secret has a hardcoded password in SecretString (use GenerateSecretString instead)", "SecretString": "{\"username\":\"MasterUsername\",\"password\":\"secret-password\"}", "Tags": [ { "Key": "AppName", "Value": "AppB" } ] } } }

YAML

# This is another secret that has its password hardcoded into the template (NOT RECOMMENDED) MySecretB: Type: 'AWS::SecretsManager::Secret' Properties: Name: MySecretForAppB Description: This secret has a hardcoded password in SecretString (use GenerateSecretString instead) SecretString: '{"username":"MasterUsername","password":"secret-password"}' Tags: - Key: AppName Value: AppB

Replicating a Secret

The following example replicates a primary secret to us-east-1 and us-east-2.

JSON

{ "MyReplicatedSecret": { "Type": "AWS::SecretsManager::Secret", "Properties": { "Name": "MyReplicatedSecret", "Description": "This secret is replicated to two regions. One with a CMK, and one with the default SecretsManager key.", "ReplicaRegions": [ { "Region": "us-east-1", "KmsKeyId": "alias/exampleAlias" }, { "Region": "us-east-2" } ] } } }

YAML

#This is a Secret resource which is replicated to two other regions. MyReplicatedSecret: Type: 'AWS::SecretsManager::Secret' Properties: Name: MyReplicatedSecret Description: "This secret is replicated to two regions. One with a CMK, and one with the default SecretsManager key." ReplicaRegions: - Region: 'us-east-1' KmsKeyId: 'alias/exampleAlias' - Region: 'us-east-1'

See also