BattleLoot
BattleLoot

Reputation: 57

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

I have some LaunchConfig for AS group

   "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"
              }, 
            ......

It's possible or what the best approach add to index.html some data, like, for example, instance-id from AWS::EC2::Instance using "files" sections?

If I add { "Ref" : "AWS::StackId" } or { "Ref" : "AWS::Region" }, it works fine, but its from Pseudo Parameter.

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

Thanks!

Upvotes: 0

Views: 319

Answers (1)

LiVi
LiVi

Reputation: 329

I don't believe it's possible to do this directly, but you should be able to accomplish this by placing a file, and then afterwards running a command to update it:

(Disclaimer: I haven't explicitly tested this.)

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

Upvotes: 1

Related Questions