Why doesn't the aws cloud formation install the packages that I specify?

18,762

Solution 1

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.

Solution 2

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.

Solution 3

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.

Share:
18,762

Related videos on Youtube

batman
Author by

batman

Updated on September 18, 2022

Comments

  • batman
    batman almost 2 years

    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.

    • mattdm
      mattdm over 11 years
      I assume from your tags that this is an Ubuntu AMI?
    • Edwin
      Edwin over 11 years
      Have you tried to remove the "," after "git":[]? It's not syntactically correct.
  • Erik van Brakel
    Erik van Brakel almost 10 years
    I think this one should be the accepted answer.
  • Pierre.Vriens
    Pierre.Vriens about 8 years
    I don't get it ...