Home > Back-end >  CloudFormation UserData template to write RDS endpoint in a configuration file of the EC2
CloudFormation UserData template to write RDS endpoint in a configuration file of the EC2

Time:11-07

I'm stuck with a part of my CloudFormation script: I have a EC2 with a Docker lamp and Wordpress defined with a Docker-compose file, and a separate RDS database in the VPC.

Each time I create the stack, the RDS endpoint changes. I need to write the new endpoint in the docker compose file to allow communication. I wrote a bash script to change this info by replacing the WORDPRESS_DB_HOST line and it's ok, but I can't pass the endpoint correctly, I think it's a syntax problem and I can't figure the solution.

UserData:
        Fn::Base64: 
          !Sub |
            #!/bin/bash -xe
            sed -i 's/WORDPRESS_DB_HOST.*/WORDPRESS_DB_HOST: {Fn::GetAtt: ["MasterDB", "Endpoint.Address"]}:3306 /' /home/ubuntu/lampconfig/docker-compose.yml 
            docker-compose up -d 

CodePudding user response:

See this question on how you can use Fn:GetAtt inside Sub. You need to use the short syntax:

UserData:
        Fn::Base64: 
          !Sub |
            #!/bin/bash -xe
            sed -i 's/WORDPRESS_DB_HOST.*/WORDPRESS_DB_HOST: ${MasterDB.Endpoint.Address}:3306 /' /home/ubuntu/lampconfig/docker-compose.yml 
            docker-compose up -d 
  • Related