lundi 3 août 2015

Cloudformation init doesn't execute commands but creates files

I am trying to create a couldformation template, and using cfn-init in the same. But I am facing this random issue, where the cfn-init module would run the files and the services configuration perfectly but none of the commands in the commands module is executed. Following is a snippet of my template.

"Metadata": {
    "AWS::CloudFormation::Init": {
      "config": {
        "commands": {
          "cloud": {
            "command" : "whoami >> /home/ubuntu/blah"
          },
          "test" : {
              "command" : "echo \"$MAGIC\" > test.txt",
              "env" : { "MAGIC" : "I come from the environment!" },
              "cwd" : "~",
              "test" : "test ! -e ~/test.txt",
              "ignoreErrors" : "false"
          },
          "test2": {
            "command" : "whoami >> wala",
            "cwd" : "/home/ubuntu"
          }
        },
        "files": {
          "/home/ubuntu/sambhav": {
            "content": {
              "Fn::Join": [
                "",
                [
                  "Ok Cool!!!",
                  "\n"
                ]
              ]
            }
          },
          "/etc/cfn/cfn-hup.conf": {
            "content": {
              "Fn::Join": [
                "",
                [
                  "[main]\n",
                  "stack=",
                  {
                    "Ref":"AWS::StackId"
                  },
                  "\n",
                  "region=",
                  {
                    "Ref":"AWS::Region"
                  },
                  "\n",
                  "interval=2",
                  "\n"
                ]
              ]
            },
            "mode": "000400",
            "owner": "root",
            "group": "root"
          },
          "/etc/cfn/hooks.d/cfn-auto-reloader.conf": {
            "content": {
              "Fn::Join": [
                "",
                [
                  "[cfn-auto-reloader-hook]\n",
                  "triggers=post.update\n",
                  "path=Resources.EC2Instance.Metadata.AWS::CloudFormation::Init\n",
                  "action=/usr/local/bin/cfn-init -s ",
                  {
                  "Ref":"AWS::StackId"
                  },
                  " -r EC2Instance ",
                  " --region ",
                  {
                    "Ref":"AWS::Region" 
                  },
                  "\n",
                  "runas=root\n"
                ]
              ]
            }
          }
        },
        "services" : {
          "sysvinit" : {
            "cfn-hup" : { "enabled" : "true", "ensureRunning" : "true",
                "files" : ["/etc/cfn/cfn-hup.conf", "/etc/cfn/hooks.d/cfn-auto-reloader.conf"]}
          }
        }
      }
    }
  },
  "Properties": {
    "InstanceType": {
      "Ref": "InstanceType"
    },
    "SecurityGroups": ["staging"],
    "KeyName": {
      "Ref": "KeyName"
    },
    "ImageId": "ami-6.....",
    "Tags" : [
        {"Key" : "Name", "Value" : "Staging"}
    ],
    "UserData" : { 
      "Fn::Base64" : { 
        "Fn::Join" : [
          "", 
          [
            "#!/bin/bash \n",
            "# Install cfn bootstraping tools \n",
            "apt-get update \n",
            "apt-get -y install python-setuptools \n",
            "mkdir aws-cfn-bootstrap-latest\n",
            "mkdir -p /home/ubuntu/work/projects\n",
            "sudo curl http://ift.tt/194v5uC | tar xz -C aws-cfn-bootstrap-latest --strip-components 1 \n",
            "sudo easy_install aws-cfn-bootstrap-latest \n",
            "cp /usr/local/bin/cfn-hup /etc/init.d/cfn-hup \n",
            "chmod +x /etc/init.d/cfn-hup \n",
            "update-rc.d cfn-hup defaults \n ",
            "service cfn-hup start \n",
            "/usr/local/bin/cfn-init --stack ", { "Ref":"AWS::StackName" }, 
            " --resource EC2Instance", " --region ", { "Ref": "AWS::Region" },
            "\n"
          ]
        ]
      }
    }
  }




Aucun commentaire:

Enregistrer un commentaire