Automating secret creation in AWS CloudFormation

Know How Guide and Hands on Guide for AWS

Automating secret creation in AWS CloudFormation

Official guide for Automating secret creation in AWS CloudFormation

Example: Using CloudFormation templates create an Amazon RDS MySQL DB instance using the credentials stored in the Secrets Manager as the master user and password.

Test cases cfn1

The secret has a resource-based policy attached that specifies access to the secret. The template also creates a Lambda rotation function and configures the secret to automatically rotate every 30 days.

  1. Deploy CloudFormation Template
  1. Connect the RDS with credentials stored in the Secrets Manager as the master user and password.

    The RDS endpoint and port can get from CloudFormation stack Outputs

    In my demo, I use the existed secret named quickstart/MyCustomTypeSecret

     # Check the secret
     aws secretsmanager describe-secret --secret-id quickstart/MyCustomTypeSecret --region cn-north-1
    
     # Get the value of master user and password
     masterUser=$(aws secretsmanager get-secret-value --secret-id quickstart/MyCustomTypeSecret --version-stage AWSCURRENT --output json --region cn-north-1 | jq -r .SecretString | jq -r .username )
     echo $masterUser
     masterPassword=$(aws secretsmanager get-secret-value --secret-id quickstart/MyCustomTypeSecret --version-stage AWSCURRENT --output json --region cn-north-1 | jq -r .SecretString | jq -r .password )
     echo $masterPassword
    
     # Get the RDS MySQL endpoint
     dbEndPoint=$(aws cloudformation describe-stacks --region cn-north-1 --stack-name Screte-Mgr-RDS --query 'Stacks[0].Outputs[?OutputKey==`dbEndPoint`].OutputValue' --output json | jq -r '.[0]')
    
     # Connect the RDS MySQL
     mysql -h $dbEndPoint -u $masterUser -p
    

Example: RDS MySQL DB instance using the credentials stored in the Secrets Manager as the master user and password. The Secrets is created by other account

Test cases cfn2

  1. Finish the steps for Access Secrets from other Account
  1. Deploy CloudFormation Template from Dev_Account
  1. Connect the RDS with credentials stored in the Secrets Manager as the master user and password.

    The RDS endpoint and port can get from CloudFormation stack Outputs

    In my demo, I use the existed secret named SecurityAcount/SharedSecrets/RDSMySQL

     sudo yum localinstall -y https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm
     sudo yum install -y mysql-community-client
     sudo yum install -y jq
    
     # Get the value of master user and password
     masterUser=$(aws secretsmanager get-secret-value --secret-id "arn:aws-cn:secretsmanager:cn-north-1:SecurityAcount:secret:SecurityAcount/SharedSecrets/RDSMySQL" --version-stage AWSCURRENT --output json --region cn-north-1 | jq -r .SecretString | jq -r .username )
     echo $masterUser
     masterPassword=$(aws secretsmanager get-secret-value --secret-id "arn:aws-cn:secretsmanager:cn-north-1:SecurityAcount:secret:SecurityAcount/SharedSecrets/RDSMySQL" --version-stage AWSCURRENT --output json --region cn-north-1 | jq -r .SecretString | jq -r .password )
     echo $masterPassword
    
     # Get the RDS MySQL endpoint
     dbEndPoint=$(aws cloudformation describe-stacks --region cn-north-1 --stack-name Screte-Mgr-RDS --query 'Stacks[0].Outputs[?OutputKey==`dbEndPoint`].OutputValue' --output json | jq -r '.[0]')
     echo $dbEndPoint
    
     # Connect the RDS MySQL
     mysql -h $dbEndPoint -u $masterUser -p
    

Cleanup

  1. Delete the CloudFormation Stack
    aws cloudformation delete-stack --stack-name Screte-Mgr-RDS --region cn-north-1
    
  2. Delete your Testing EC2

  3. Delete your secrets