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.
You can't generate a secret with a SecretBinary
secret value using AWS
CloudFormation. You can only create a SecretString
text-based secret
value.
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:
-
Configure the secret with details of the Secrets Manager supported database or service with credentials stored in the secret.
-
Attach a resource-based permissions policy to the secret by defining a AWS::SecretsManager::ResourcePolicy resource type.
-
Configure the secret to rotate after a specified number of days by defining a AWS::SecretsManager::RotationSchedule resource type.
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
, "SecretString" :String
, "Tags" :[ Tag, ... ]
} }
YAML
Type: AWS::SecretsManager::Secret Properties: Description:
String
GenerateSecretString:GenerateSecretString
KmsKeyId:String
Name:String
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
-
A structure that specifies generating a random password by using the functionality of the GetRandomPassword API. You can return that string directly to use as the secret value, or you can specify both the
SecretStringTemplate
and theGenerateSecretKey
parameters. Secrets Manager uses the value inGenerateSecretKey
parameters. Secrets Manager uses the value inGenerateSecretKey
as the key name and combines it with the randomly generated password to make a JSON key-value pair. Secrets Manager then inserts the pair into the JSON structure specified in theSecretStringTemplate
parameter. Secrets Manager stores the completed string as the secret value in the initial version of the secret. For more information about how to use this property, see Secrets Manager Secret GenerateSecretString and the first example in the following Examples section.Either
SecretString
orGenerateSecretString
must have a value, but not both. They cannot both be empty.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
orSecretBinary
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 versionSecretString
orSecretBinary
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 nameddbserver1
in the folderdatabases
in the folderprod
.Required: No
Type: String
Minimum:
1
Maximum:
256
Update requires: Replacement
SecretString
-
(Optional) Specifies text data that you want to encrypt and store in this new version of the secret.
Either
SecretString
orSecretBinary
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
See also
-
CreateSecret API in the AWS Secrets Manager API Reference
-
Secret in the AWS Secrets Manager User Guide