在 AWS::CloudFormation::Init 中使用 AWS::EC2::Instance

Use AWS::EC2::Instance in AWS::CloudFormation::Init

我有一些 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"
              }, 
            ......

使用 "files" 部分添加到 index.html 某些数据(例如来自 AWS::EC2::Instance 的实例 ID)是可能的,或者最好的方法是什么?

如果我添加 { "Ref" : "AWS::StackId" } 或 { "Ref" : "AWS::Region" },它工作正常,但它来自伪参数。

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

谢谢!

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

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

{
    "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"
                }
            }
        }
    }
}