简体   繁体   English

CloudFormation UserData 模板,用于在 EC2 的配置文件中写入 RDS 端点

[英]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.我被我的 CloudFormation 脚本的一部分困住了:我有一个带有 Docker 灯的 EC2 和用 Docker-compose 文件定义的 Wordpress,以及 VPC 中的一个单独的 RDS 数据库。

Each time I create the stack, the RDS endpoint changes.每次我创建堆栈时,RDS 端点都会发生变化。 I need to write the new endpoint in the docker compose file to allow communication.我需要在 docker compose 文件中编写新端点以允许通信。 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.我编写了一个 bash 脚本来通过替换 WORDPRESS_DB_HOST 行来更改此信息,这没关系,但是我无法正确传递端点,我认为这是一个语法问题,我无法找到解决方案。

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.这个问题上,你可以如何使用Fn:GetAtt里子。 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 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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