简体   繁体   中英

CloudFormation UserData template to write RDS endpoint in a configuration file of the EC2

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 

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 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM