Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Parameters" : {
"KeyName" : {
"Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance",
"Type" : "String"
},
"RecipeURL" : {
"Description" : "The location of the recipe tarball",
"Type": "String"
},
"JsonURL" : {
"Description" : "The location of the node.json file",
"Type": "String"
},
"EC2InstanceType": {
"Type" : "String",
"Default" : "t1.micro",
"AllowedValues" : ["t1.micro", "m1.small", "m1.large"],
"Description" : "Enter t1.micro, m1.small, or m1.large. Default is t1.micro."
}
},
"Mappings" : {
"RegionMap" : {
"us-east-1" : { "AMI" : "ami-35792c5c" },
"us-west-1" : { "AMI" : "ami-687b4f2d" },
"us-west-2" : { "AMI" : "ami-d03ea1e0" },
"eu-west-1" : { "AMI" : "ami-149f7863" },
"sa-east-1" : { "AMI" : "ami-9f6ec982" },
"ap-southeast-1" : { "AMI" : "ami-14f2b946" },
"ap-southeast-2" : { "AMI" : "ami-a148d59b" },
"ap-northeast-1" : { "AMI" : "ami-3561fe34" }
}
},
"Resources" : {
"PowerUserRole" : {
"Type" : "AWS::IAM::Role",
"Properties" : {
"AssumeRolePolicyDocument" : {
"Statement": [ {
"Effect": "Allow",
"Principal": {
"Service": [ "ec2.amazonaws.com" ]
},
"Action": [ "sts:AssumeRole" ]
} ]
},
"Path" : "/",
"Policies" :[ {
"PolicyName" : "PowerUserPolicy",
"PolicyDocument" : {
"Statement": [ {
"Sid": "PowerUserStmt",
"Effect": "Allow",
"NotAction": "iam:*",
"Resource": "*"
}]
}
}]
}
},
"PowerUserProfile" : {
"Type" : "AWS::IAM::InstanceProfile",
"Properties" : {
"Roles" : [ { "Ref" : "PowerUserRole" } ]
}
},
"EC2Servers" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "Enable SSH access via port 22",
"SecurityGroupIngress": [{
"IpProtocol" : "tcp",
"CidrIp" : "0.0.0.0/0",
"FromPort" : "22",
"ToPort" : "22"
}]
}
},
"Ec2Instance" : {
"Type" : "AWS::EC2::Instance",
"Metadata" : {
"AWS::CloudFormation::Init" : {
"configSets" : {
"default" : [ "config1" , "config2", "config3" ]
},
"config1" : {
"packages" : {
"yum" : {
"gcc-c++" : [],
"ruby19" : [],
"ruby19-devel" : [],
"rubygems" : []
}
}
},
"config2" : {
"commands" : {
"alternatives" : {
"command" : "alternatives --set ruby /usr/bin/ruby1.9"
},
"install-chef" : {
"command" : "gem install --no-ri --no-rdoc chef"
},
"mkdir-chef" : {
"command" : "mkdir /var/chef-solo"
}
}
},
"config3" : {
"files" : {
"/etc/chef/solo.rb" : {
"content" : { "Fn::Join" : ["", [
"log_level :info\n",
"log_location \"/var/chef-solo/result.log\"\n",
"file_cache_path \"/var/chef-solo\"\n",
"cookbook_path \"/var/chef-solo/cookbooks\"\n",
"json_attribs \"", { "Ref" : "JsonURL" }, "\"\n",
"recipe_url \"", { "Ref" : "RecipeURL" }, "\"\n"
]] },
"mode" : "000644",
"owner" : "root",
"group" : "root"
}
}
}
}
},
"Properties" : {
"KeyName" : { "Ref" : "KeyName" },
"ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]},
"InstanceType" : { "Ref": "EC2InstanceType" },
"SecurityGroupIds" : [
{ "Ref" : "EC2Servers" }
],
"IamInstanceProfile": { "Ref" : "PowerUserProfile" },
"Tags": [
{ "Key" : "Name", "Value" : "host1" }
],
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
"#! /bin/bash -v\n",
"yum update -y\n",
"function error_exit\n",
"{\n",
" /opt/aws/bin/cfn-signal -e 1 -r \"$1\" '", { "Ref" : "WaitHandle" }, "'\n",
" exit 1\n",
"}\n",
"/opt/aws/bin/cfn-init -s ", { "Ref" : "AWS::StackId" }, " -r Ec2Instance ",
" --region ", { "Ref" : "AWS::Region" }, " || error_exit 'Failed to run cfn-init'\n",
"/usr/local/bin/chef-solo\n",
"/opt/aws/bin/cfn-signal -e $? '", { "Ref" : "WaitHandle" }, "'\n"
]]}}
}
},
"WaitHandle" : {
"Type" : "AWS::CloudFormation::WaitConditionHandle"
},
"WaitCondition" : {
"Type" : "AWS::CloudFormation::WaitCondition",
"DependsOn" : "Ec2Instance",
"Properties" : {
"Handle" : { "Ref" : "WaitHandle" },
"Timeout" : "900"
}
}
},
"Outputs" : {
"InstanceId" : {
"Description" : "InstanceId of the newly created EC2 instance",
"Value" : { "Ref" : "Ec2Instance" }
},
"AZ" : {
"Description" : "Availability Zone of the newly created EC2 instance",
"Value" : { "Fn::GetAtt" : [ "Ec2Instance", "AvailabilityZone" ] }
},
"PublicIP" : {
"Description" : "Public IP address of the newly created EC2 instance",
"Value" : { "Fn::GetAtt" : [ "Ec2Instance", "PublicIp" ] }
},
"PrivateIP" : {
"Description" : "Private IP address of the newly created EC2 instance",
"Value" : { "Fn::GetAtt" : [ "Ec2Instance", "PrivateIp" ] }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment