【问题标题】:Use AWS::EC2::Instance in AWS::CloudFormation::Init在 AWS::CloudFormation::Init 中使用 AWS::EC2::Instance
【发布时间】:2017-04-08 02:11:29
【问题描述】:

我有一些 AS 组的 LaunchConfig

   "LaunchConfig": {
     "Type" : "AWS::AutoScaling::LaunchConfiguration",

     "Metadata" : {
        "AWS::CloudFormation::Init" : {
          "configSets" : {
            "InstallAndRun" : [ "Install" ]
          },

          "Install" : {

            "files" : {

              "/var/www/html/index.html" : {
                "content" : { "Fn::Join" : ["", [
                "<html\n",
                "<h1>Apache HTTP Server</h1>\n",
                "</html>\n"
              ]]},
              "mode"    : "000644",
              "owner"   : "apache",
              "group"   : "apache"
              }, 
            ......

有可能或最好的方法是向 index.html 添加一些数据,例如,来自 AWS::EC2::Instance 的实例 ID 使用“文件”部分?

如果我添加 { "Ref" : "AWS::StackId" } 或 { "Ref" : "AWS::Region" },它可以正常工作,但它来自 Pseudo Parameter。

              "/var/www/html/index.html" : {
                "content" : { "Fn::Join" : ["", [
                "<html\n",
                "<h1>Apache HTTP Server</h1>\n",
                { "Ref" : "AWS::StackId" },
                "</html>\n"
              ]]},

谢谢!

【问题讨论】:

  • 我建议在外部添加文件。即在 S3 上安装它并在 EC2 启动期间拉取它。主要是因为您可以根据需要灵活地向其中添加尽可能多的内容 - 模板不会显得凌乱。然后使用 EC2 启动脚本中的 sed 命令向其注入任何额外数据。

标签: amazon-web-services amazon-ec2 amazon-cloudformation


【解决方案1】:

我不相信可以直接执行此操作,但您应该可以通过放置一个文件,然后运行命令来更新它来完成此操作:

(免责声明:我没有明确测试过。)

{
    "AWS::CloudFormation::Init": {
        "configSets": {
            "InstallAndRun": [
                "Install",
                "UpdateIndexHtml"
            ]
        },
        "Install": {
            "files": {
                "/var/www/html/index.html": {
                    "content": {
                        "Fn::Join": [
                            "",
                            [
                                "<html\n",
                                "<h1>Apache HTTP Server</h1>\n",
                                "---INSTANCE_ID---\n",
                                "</html>\n"
                            ]
                        ]
                    },
                    "mode": "000644",
                    "owner": "apache",
                    "group": "apache"
                }
            }
        },
        "UpdateIndexHtml": {
            "commands": {
                "UpdateIndexHtml": {
                    "command": "sed -i \"s|---INSTANCE_ID---|$(curl -s http://169.254.169.254/latest/meta-data/instance-id)|\" /var/www/html/index.html"
                }
            }
        }
    }
}

【讨论】:

    猜你喜欢
    • 2013-03-01
    • 2014-01-01
    • 2017-04-24
    • 1970-01-01
    • 2019-10-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多