Search code examples
amazon-web-servicesaws-cloudformationaws-secrets-manager

Reference Secrets Manager Parameters to Secret String


Is there any way to reference parameters in SecretString field in Secrets Manager via CloudFormation?

The way I made the script, the !Ref parameter is a text and not a reference to the parameter.

AWSTemplateFormatVersion: 2010-09-09

Parameters:

  Name:
    Type: String
  myuserparameter:
    Type: String
  mypasswordparameter:
    Type: String

Resources:  
  
  SecretsManager:
    Type: AWS::SecretsManager::Secret
    Properties:
      Name: !Ref Name
      SecretString: '{"username":"!Ref myuserparameter,"password":"Ref mypasswordparameter"}'

Solution

  • this will work:

    AWSTemplateFormatVersion: 2010-09-09
    
    Parameters:
    
      Name:
        Type: String
      myuserparameter:
        Type: String
      mypasswordparameter:
        Type: String
    
    Resources:  
      
      SecretsManager:
        Type: AWS::SecretsManager::Secret
        Properties:
          Name: !Ref Name
          SecretString: !Sub '{"username": "${myuserparameter}","password": "${mypasswordparameter}"}'