Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Installing packages using apt-get in CloudFormation file

I've got the following CloudFormation script. The stack is being created and the Ec2 instance launches, and I can SSH in but it's not installing the packages.

I'm not sure where it's failing at. I'm using Ubuntu. I can't find were cfn-init is installed on my instance? Or is it only installed for Amazon Linux AMIs?

How do I go about troubleshooting this?

{
"Parameters" : {
    "ShinyKey": {
        "Description": "Key pair for instance.",
        "Type": "AWS::EC2::KeyPair::KeyName"
    }
},
"Resources": {
    "Ec2Instance" : {
        "Metadata": {
            "AWS::CloudFormation::Init": {
                "config": {
                    "packages": {
                        "apt": {
                            "r-base-dev": [],
                            "libcurl4-openssl-dev": [],
                            "git": []
                        }
                    }
                }
            }
        },
        "Type" : "AWS::EC2::Instance",
        "Properties": {
            "ImageId": "ami-9eaa1cf6",
            "InstanceType": "t2.micro",
            "KeyName": {"Ref": "ShinyKey"},
            "SecurityGroups": [{"Ref": "InstanceSecurityGroup"}],
            "Tags": [{
                "Key": "Name",
                "Value": "R-Shiny-Server"
            }],
            "UserData": {
                "Fn::Base64": {
                    "Fn::Join": [
                        "",
                        [
                            "#!/bin/bash\n",
                            "/usr/local/bin/cfn-init --region ",
                            {
                                "Ref": "AWS::Region"
                            },
                            " -s ",
                            {
                                "Ref": "AWS::StackName"
                            },
                            " -r Ec2Instance\n"
                        ]
                    ]
                }
            }
        }
    },
    "InstanceSecurityGroup" : {
        "Type" : "AWS::EC2::SecurityGroup",
        "Properties": {
            "GroupDescription" : "Enable SSH access via port 22, and ports 3838 and 80 for Shiny",
            "SecurityGroupIngress" : [
                { "IpProtocol" : "tcp", "FromPort" : "22", "ToPort" : "22", "CidrIp" : "0.0.0.0/0" },
                { "IpProtocol" : "tcp", "FromPort" : "80", "ToPort" : "80", "CidrIp" : "0.0.0.0/0" },
                { "IpProtocol" : "tcp", "FromPort" : "3838", "ToPort" : "3838", "CidrIp" : "0.0.0.0/0" }
            ]
        }
    }
}

}

like image 457
James White Avatar asked Jan 23 '15 00:01

James White


1 Answers

The issue with the template above is that cfn-init is not installed in the Ubuntu AMI, so the call to cfn-init in your user-data script will return "command not found" and do nothing.

The cfn-helper utilities are automatically installed only in the latest Amazon Linux AMI, as noted in the documentation. For Ubuntu you need to install them manually, which you can do by adding a line to your existing user-data script, after "#!/bin/bash\n",:

"apt-get update && apt-get install pip && pip install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-latest.tar.gz\n",

After this, the call to /usr/local/bin/cfn-init in the next line should run correctly.

like image 140
wjordan Avatar answered Nov 27 '22 01:11

wjordan