Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz | 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"
          ]
        ]
      }
    }
  }
like image 459
Sambhav Sharma Avatar asked Aug 03 '15 13:08

Sambhav Sharma


People also ask

How do you call CFN-init?

You can run the cfn-init command if you SSH into the instance too, which makes a quick deploy-try out process. Just right click on the instance and get the user data and copy-paste the cfn-init part. You can inspect the logs at /var/log/cfn-init. log and /var/log/cfn-init-cmd.

How does CFN-init work?

If you use cfn-init to update an existing file, it creates a backup copy of the original file in the same directory with a . bak extension. For example, if you update / path / to / file_name , the action produces two files: / path / to / file_name .

How do you debug a CloudFormation failure?

log or /var/log/cfn-init. log , to help you debug the instance launch. You can retrieve the logs by logging in to your instance, but you must disable rollback on failure or else AWS CloudFormation deletes the instance after your stack fails to create. You can also publish the logs to Amazon CloudWatch.

What is the purpose of the CloudFormation helper CFN-init?

cfn-init: Use to retrieve and interpret resource metadata, install packages, create files, and start services.


1 Answers

The template snippet you've provided appears to be working as expected, with the files being created with the appropriate content:

ubuntu@ip-172-31-9-151:~$ cat /home/ubuntu/*
root
Ok Cool!!!
root
cat: /home/ubuntu/work: Is a directory

Check the cfn-init logs to ensure that the commands are being executed and reveal any errors that are encountered:

ubuntu@ip-172-31-9-151:~$ cat /var/log/cfn-init.log 
2015-10-18 05:37:49,087 [INFO] -----------------------Starting build-----------------------
2015-10-18 05:37:49,300 [INFO] Running configSets: default
2015-10-18 05:37:49,300 [INFO] Running configSet default
2015-10-18 05:37:49,301 [INFO] Running config config
2015-10-18 05:37:49,306 [INFO] Command cloud succeeded
2015-10-18 05:37:49,310 [INFO] Command test succeeded
2015-10-18 05:37:49,313 [INFO] Command test2 succeeded
2015-10-18 05:37:49,323 [INFO] enabled service cfn-hup
2015-10-18 05:37:49,420 [INFO] Restarted cfn-hup successfully
2015-10-18 05:37:49,421 [INFO] ConfigSets completed
2015-10-18 05:37:49,427 [INFO] -----------------------Build complete-----------------------
like image 193
iviarki Avatar answered Nov 15 '22 10:11

iviarki