14

I'm very new to the AWS services. I'm trying to use the AWS cloud formation and I created a template. The template is error free and I am able to create machines using that.

But I have added some config in the template such that it installs tomcat, git and other things during startup. But that doesn't happen for me.

Here is a part of code I used for installing tomcat :

"Resources": {
    "Tomcat": {
        "Type": "AWS::EC2::Instance",
        "Metadata": {
            "AWS::CloudFormation::Init": {
                "config": {
                    "packages": {
                        "apt": {
                            "tomcat6": [],
                            "git": [],
                        }
                    }
                }
            }
        },

But when I log into the machine neither tomcat nor git have been installed!

Thanks in advance.

2
  • I assume from your tags that this is an Ubuntu AMI?
    – mattdm
    Jan 8, 2013 at 21:05
  • Have you tried to remove the "," after "git":[]? It's not syntactically correct.
    – Edwin
    Jan 12, 2013 at 9:01

4 Answers 4

25

I believe your problem here is confusion around that cloud-init is not the same as cfn-init.

  • cloud-init is the tool that started as part of the Ubuntu AWS AMIs that allow the interpretation of the EC2 user-data component of the instance meta-data. Amazon Linux adopted this tool as well and built it into their AMI.

  • cfn-init is part of a different toolset called CloudFormation Helper Scripts created by AWS for Amazon Linux that can read an additional section named Metadata in your CloudFormation template.

So, Ubuntu and Amazon Linux AMIs both have the cloud-init tools preinstalled to access the user-data, but only Amazon Linux has the CloudFormation Helper Scripts preinstalled e.g. cfn-init to access the CloudFormation Metadata.

However, AWS does distribute packages that you can use to read the CloudFormation Metadata. Specifically, look at this template for how you can use a cloud-init user-data script to install python-setuptools, download the CloudFormation Helper Scripts, install them using easy_install and then invoke cfn-init.

Note: There are other distributions or AMIs that might support cloud-init or cfn-init, but I'm just covering the general cases here.

12

You need to call cfn-init (http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-init.html) inside the UserData property of the instance:

{
    "Resources": {
        "Tomcat": {
            "Type": "AWS::EC2::Instance",
            "Metadata": {
                "AWS::CloudFormation::Init": {
                    "config": {
                        "packages": {
                            "apt": {
                                "tomcat6": [],
                                "git": []
                            }
                        }
                    }
                }
            },
            "Properties": {
                "UserData": {
                    "Fn::Base64": {
                        "Fn::Join": ["", [
                            "#!/bin/bash\n",
                            "/opt/aws/bin/cfn-init -s ", {
                                "Ref": "AWS::StackName"
                            },
                            "    -r Tomcat",
                            "    --region ", {
                                "Ref": "AWS::Region"
                            }, "\n"
                        ]]
                    }
                }
            }
        }
    }
}

The UserData property is Base64 encoded, and allows you to specify a script that should be run at instance launch. Here you can call cfn-init which will read the CloudFormation::Init Metadata and setup everything specified there.

Also when creating the CF Stack you might want to go into advanced settings on the second page (after giving the parameters) and make sure Rollback on error is set to "No". That way if the cfn-init script fails for whatever reason you can ssh onto the instance and check the /var/log/cfn-init.log file for more info.

1
  • I think this one should be the accepted answer. Jul 30, 2014 at 12:26
1

Above answer is correct. But you also should make sure that you have sucessfully installed CloudFormation helper scripts. Please follow to http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-helper-scripts-reference.html for installation details.

-3
    "UserData"       : { "Fn::Base64" : { "Fn::Join" : ["", [
      "#!/bin/bash\n",
      "export DEBIAN_FRONTEND=noninteractive\n",
      "apt-get update && apt-get upgrade -y\n",
      "apt-get -y install python-setuptools\n",
      "easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz\n",
      "/usr/local/bin/cfn-init --stack ", { "Ref":"AWS::StackName" }, " --resource <REPLACE_WITH_RESOURCENAME>", " --region ", { "Ref": "AWS::Region" }, "\n",
1

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .