Skip to content

Instantly share code, notes, and snippets.

@mschierberl
Created September 19, 2012 04:07
Show Gist options
  • Star 9 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save mschierberl/3747639 to your computer and use it in GitHub Desktop.
Save mschierberl/3747639 to your computer and use it in GitHub Desktop.
Railo CloudFormation example with nginx
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "AWS CloudFormation template to deploy railo4/tomcat7/nginx.",
"Parameters" : {
"KeyName" : {
"Default" : "bacfug",
"Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instances",
"Type" : "String"
},
"InstanceType" : {
"Description" : "EC2 instance type",
"Type" : "String",
"Default" : "t1.micro",
"AllowedValues" : [ "t1.micro","m1.small","m1.medium","m1.large","m1.xlarge","m2.xlarge","m2.2xlarge","m2.4xlarge","c1.medium","c1.xlarge","cc1.4xlarge","cc2.8xlarge","cg1.4xlarge"],
"ConstraintDescription" : "must be a valid EC2 instance type."
}
},
"Mappings" : {
"AWSInstanceType2Arch" : {
"t1.micro" : { "Arch" : "64" },
"m1.small" : { "Arch" : "64" },
"m1.medium" : { "Arch" : "64" },
"m1.large" : { "Arch" : "64" },
"m1.xlarge" : { "Arch" : "64" },
"m2.xlarge" : { "Arch" : "64" },
"m2.2xlarge" : { "Arch" : "64" },
"m2.4xlarge" : { "Arch" : "64" },
"c1.medium" : { "Arch" : "64" },
"c1.xlarge" : { "Arch" : "64" },
"cc1.4xlarge" : { "Arch" : "64HVM" },
"cc2.8xlarge" : { "Arch" : "64HVM" },
"cg1.4xlarge" : { "Arch" : "64HVM" }
},
"AWSRegionArch2AMI" : {
"us-east-1" : { "32" : "ami-31814f58", "64" : "ami-1b814f72", "64HVM" : "ami-0da96764" },
"us-west-2" : { "32" : "ami-38fe7308", "64" : "ami-96ef63a6", "64HVM" : "NOT_YET_SUPPORTED" },
"us-west-1" : { "32" : "ami-11d68a54", "64" : "ami-1bd68a5e", "64HVM" : "NOT_YET_SUPPORTED" },
"eu-west-1" : { "32" : "ami-973b06e3", "64" : "ami-953b06e1", "64HVM" : "NOT_YET_SUPPORTED" },
"ap-southeast-1" : { "32" : "ami-b4b0cae6", "64" : "ami-beb0caec", "64HVM" : "NOT_YET_SUPPORTED" },
"ap-northeast-1" : { "32" : "ami-0644f007", "64" : "ami-0a44f00b", "64HVM" : "NOT_YET_SUPPORTED" },
"sa-east-1" : { "32" : "ami-3e3be423", "64" : "ami-3c3be421", "64HVM" : "NOT_YET_SUPPORTED" }
}
},
"Resources" : {
"CfnUser" : {
"Type" : "AWS::IAM::User",
"Properties" : {
"Path": "/",
"Policies": [{
"PolicyName": "root",
"PolicyDocument": { "Statement":[{
"Effect":"Allow",
"Action" : [
"cloudformation:DescribeStackResource",
"s3:GetObject"
],
"Resource":"*"
}]}
}]
}
},
"CfnKeys" : {
"Type" : "AWS::IAM::AccessKey",
"Properties" : {
"UserName" : {"Ref": "CfnUser"}
}
},
"HostKeys" : {
"Type" : "AWS::IAM::AccessKey",
"Properties" : {
"UserName" : {"Ref": "CfnUser"}
}
},
"WebServer": {
"Type": "AWS::EC2::Instance",
"Metadata" : {
"AWS::CloudFormation::Init" : {
"config" : {
"packages" : {
"apt" : {
"nginx" : [],
"tomcat6" : [],
"git" : []
}
},
"sources": {
"/tmp/railo/": "http://www.getrailo.org/down.cfm?item=/railo/remote/download/4.0.0.013/custom/all/railo-4.0.0.013-jars.tar.gz"
,"/tmp/tomcat_conf/": "https://gist.github.com/gists/3745673/download"
},
"files" : {
"/tmp/index.cfm" : {
"content" : { "Fn::Join" : ["", [
"<cfdump var='#server#'>\n"
]]},
"mode" : "000755",
"owner" : "root",
"group" : "root"
},
"/tmp/nginx/default" : {
"content" : { "Fn::Join" : ["", [
"server {\n",
"# Main Railo proxy handler\n",
"location ~ \\.(cfm|cfml|cfc|jsp|cfr)(.*)$ {\n",
"proxy_pass http://127.0.0.1:8080;\n",
"proxy_redirect off;\n",
"proxy_set_header Host $host;\n",
"proxy_set_header X-Forwarded-Host $host;\n",
"proxy_set_header X-Forwarded-Server $host;\n",
"proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;\n",
"proxy_set_header X-Real-IP $remote_addr;\n",
"}\n",
"root /usr/share/nginx/www;\n",
"index index.cfm index.html index.htm;\n",
"}\n"
]]},
"mode" : "000755",
"owner" : "root",
"group" : "root"
}
}
,"services" : {
"sysvinit" : {
"nginx" : { "enabled" : "true", "ensureRunning" : "true" },
"tomcat6" : { "enabled" : "true", "ensureRunning" : "true" }
}
}
}
}
},
"Properties": {
"ImageId" : { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" },
{ "Fn::FindInMap" : [ "AWSInstanceType2Arch", { "Ref" : "InstanceType" }, "Arch" ] } ] },
"InstanceType" : { "Ref" : "InstanceType" },
"SecurityGroups" : [ {"Ref" : "WebServerSecurityGroup"} ],
"KeyName" : { "Ref" : "KeyName" },
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash\n",
"apt-get update\n",
"# Helper function\n",
"function error_exit\n",
"{\n",
" cfn-signal -e 1 -r \"$1\" '", { "Ref" : "WaitHandle" }, "'\n",
" exit 1\n",
"}\n",
"apt-get -y install python-setuptools\n",
"easy_install https://s3.amazonaws.com/cloudformation-examples/aws-cfn-bootstrap-1.0-6.tar.gz\n",
"cfn-init -v -s ", { "Ref" : "AWS::StackName" }, " -r WebServer ",
" --access-key ", { "Ref" : "HostKeys" },
" --secret-key ", {"Fn::GetAtt": ["HostKeys", "SecretAccessKey"]},
" --region ", { "Ref" : "AWS::Region" }, " || error_exit 'Failed to run cfn-init'\n",
"chown tomcat6 /var/lib/tomcat6/webapps/ROOT\n",
"chown tomcat6 /usr/share/tomcat6/lib\n",
"cp /tmp/index.cfm /var/lib/tomcat6/webapps/ROOT/index.cfm\n",
"cp /tmp/railo/railo-4.0.0.013-jars/* /usr/share/tomcat6/lib/\n",
"cp /tmp/tomcat_conf/gist3745673-f5ade74eed784f31aea0628b90ce3873e0a5103c/web.xml /var/lib/tomcat6/conf/web.xml\n",
"cp /tmp/nginx/default /etc/nginx/sites-enabled/default\n",
"/etc/init.d/tomcat6 restart\n",
"/etc/init.d/nginx restart\n",
"# All is well so signal success\n",
"cfn-signal -e 0 -r \"Railo setup complete\" '", { "Ref" : "WaitHandle" }, "'\n"
]]}}
}
}
,"WaitHandle" : {
"Type" : "AWS::CloudFormation::WaitConditionHandle"
}
,"WaitCondition" : {
"Type" : "AWS::CloudFormation::WaitCondition",
"DependsOn" : "WebServer",
"Properties" : {
"Handle" : {"Ref" : "WaitHandle"},
"Timeout" : "300"
}
},
"WebServerSecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "Enable HTTP access via port 80,8080, and SSH access",
"SecurityGroupIngress" : [
{"IpProtocol" : "tcp", "FromPort" : "80", "ToPort" : "80", "CidrIp" : "0.0.0.0/0"},
{"IpProtocol" : "tcp", "FromPort" : "8080", "ToPort" : "8080", "CidrIp" : "0.0.0.0/0"},
{"IpProtocol" : "tcp", "FromPort" : "22", "ToPort" : "22", "CidrIp" : "0.0.0.0/0"}
]
}
}
},
"Outputs" : {
"WebsiteURL" : {
"Value" : { "Fn::Join" : ["", ["http://", { "Fn::GetAtt" : [ "WebServer", "PublicDnsName" ]}, ""]] },
"Description" : "Railo Installation"
}
}
}
@jeisenlo
Copy link

jeisenlo commented Feb 6, 2013

This is wonderful, thank you for sharing!

Your description says that this installs tomcat7, but the template only references tomcat6...

@mvanleest
Copy link

Nice! Thanks for sharing!

@thoskam
Copy link

thoskam commented Oct 1, 2013

Has anyone gotten this to work with Cloudformation? I just tried it and could not get it to launch. I pretty new to the whole concept so im sure im missing something...

@timmaybrown
Copy link

This is awesome! Same question about tomcat 7 vs tomcat 6 where in this template is that controlled? Is that based on the AMI used? Hopefully this will work...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment