aws cloud-formation create secret with variables

26 Views Asked by At

I am trying to take username and password from user as input parameters and create the secret in secret manager. Using ${Username} doesnt seem to replace and could not find a documentation on how to go about this. Is it even supported? if not, any recommended workarounds?

AWSTemplateFormatVersion: '2010-09-09'
Resources:
  MySecret:
    Type: 'AWS::SecretsManager::Secret'
    Properties:
      Description: 'My example secret'
      GenerateSecretString:
        SecretStringTemplate: '{"username": "${Username}", "password": "${Password}"}'
        PasswordLength: 16
        ExcludePunctuation: true
      Tags:
        - Key: 'Name'
          Value: 'MySecret'
Parameters:
  Username:
    Type: String
    Description: 'Username for the secret'
  Password:
    Type: String
    Description: 'Password for the secret'
1

There are 1 best solutions below

0
Eric Z Beard On

You may have wanted SecretString instead of GenerateSecretString.

The GenerateSecretString property generates random passwords for you. The template lets you specify text that contains the password, but not the password itself. If you specify SecretStringTemplate, you also need to provide GenerateStringKey.

This template:

Resources:
  Secret:
    Type: AWS::SecretsManager::Secret
    Properties: 
      GenerateSecretString:
        GenerateStringKey: password
        SecretStringTemplate: '{"username": "admin"}'

Creates a secret that looks like this:

{"password":"q0*99P1dd46@_K*zW#pA+nK5fhkqA!/u","username":"admin"}