Skip to content

Commit 7987049

Browse files
committed
Initial cloud formation templates working.
1 parent 9de61da commit 7987049

File tree

3 files changed

+368
-0
lines changed

3 files changed

+368
-0
lines changed

cloud-formation/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Cloud Formation
2+
This directory just has a few mess-around templates for testing AWS Cloud Formation during POC, although this was not
3+
at all a focus during the POC.
4+
5+
Useful, although incomplete, resource:
6+
- http://callistaenterprise.se/blogg/teknik/2013/01/17/set-up-a-tomcat-server-on-aws-using-cloudformation-2/
7+
8+
In testing setting up a basic EC2 instance, this took just short of two minutes - nice.
Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
{
2+
"AWSTemplateFormatVersion" : "2010-09-09",
3+
4+
"Description" : "Simple template to create EC2 instances with Apache and Tomcat installed.",
5+
6+
"Parameters" :
7+
{
8+
9+
"KeyName" :
10+
{
11+
"Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instances",
12+
"Type" : "AWS::EC2::KeyPair::KeyName",
13+
"ConstraintDescription" : "must be the name of an existing EC2 KeyPair."
14+
},
15+
16+
"InstanceType" :
17+
{
18+
"Description" : "FormEngine EC2 instance type",
19+
"Type" : "String",
20+
"Default" : "t2.micro"
21+
}
22+
23+
},
24+
25+
"Mappings" :
26+
{
27+
"AWSInstanceType2Arch" :
28+
{
29+
"t2.micro" : { "Arch" : "64" },
30+
"t2.small" : { "Arch" : "64" },
31+
"t2.medium" : { "Arch" : "64" },
32+
"t2.large" : { "Arch" : "64" }
33+
},
34+
"AWSRegionArch2AMI" : {
35+
"eu-west-1" : { "64" : "ami-d41d58a7" }
36+
}
37+
},
38+
"Resources" :
39+
{
40+
"WebServerGroup" :
41+
{
42+
"Type" : "AWS::EC2::SecurityGroup",
43+
"Properties" :
44+
{
45+
"GroupDescription" : "Enable SSH and HTTP access",
46+
"SecurityGroupIngress" :
47+
[
48+
{ "IpProtocol" : "tcp", "FromPort" : "22", "ToPort" : "22", "CidrIp" : "0.0.0.0/0" },
49+
{ "IpProtocol" : "tcp", "FromPort" : "80", "ToPort" : "80", "CidrIp" : "0.0.0.0/0" }
50+
]
51+
}
52+
},
53+
54+
"WebServer":
55+
{
56+
"Type" : "AWS::EC2::Instance",
57+
"Metadata" :
58+
{
59+
"AWS::CloudFormation::Init":
60+
{
61+
"config" :
62+
{
63+
"packages" :
64+
{
65+
"yum" :
66+
{
67+
"java-1.7.0-openjdk": [],
68+
"tomcat7": [],
69+
"httpd": []
70+
}
71+
}
72+
}
73+
}
74+
},
75+
76+
"Properties": {
77+
78+
"ImageId": {
79+
"Fn::FindInMap": [
80+
"AWSRegionArch2AMI",
81+
{"Ref": "AWS::Region"},
82+
{
83+
"Fn::FindInMap": [
84+
"AWSInstanceType2Arch",
85+
{"Ref": "InstanceType"},
86+
"Arch"
87+
]
88+
}
89+
]
90+
},
91+
92+
"InstanceType": {"Ref": "InstanceType"},
93+
"SecurityGroups": [{"Ref": "WebServerGroup"}],
94+
"KeyName": {"Ref": "KeyName"},
95+
"Tags": [
96+
{
97+
"Key": "Name",
98+
"Value": "WebServer"
99+
}
100+
],
101+
102+
103+
"UserData" : {
104+
"Fn::Base64" : {
105+
"Fn::Join" : ["", [
106+
"#!/bin/bash -v\n",
107+
"date > /home/ec2-user/starttime\n",
108+
"yum update -y aws-cfn-bootstrap\n",
109+
110+
"## Error reporting helper function\n",
111+
"function error_exit\n",
112+
"{\n",
113+
" /opt/aws/bin/cfn-signal -e 1 -r \"$1\" '", { "Ref" : "WaitHandle" }, "'\n",
114+
" exit 1\n",
115+
"}\n",
116+
117+
"## Initialize CloudFormation bits - also installs packages\n",
118+
"/opt/aws/bin/cfn-init -v ",
119+
" --stack ", { "Ref" : "AWS::StackId" },
120+
" --resource WebServer",
121+
" --region ", { "Ref" : "AWS::Region" },
122+
" > /tmp/cfn-init.log 2>&1 || error_exit $(</tmp/cfn-init.log)",
123+
"\n",
124+
125+
"# Add Tomcat user to sudoers and disable tty\n",
126+
"echo \"tomcat ALL=(ALL) NOPASSWD:ALL\" >> /etc/sudoers\n",
127+
"echo \"Defaults:%tomcat !requiretty\" >> /etc/sudoers\n",
128+
"echo \"Defaults:tomcat !requiretty\" >> /etc/sudoers\n",
129+
130+
"# Set JVM settings\n",
131+
"echo \"JAVA_OPTS='${JAVA_OPTS} -Xms512m -Xmx512m -XX:PermSize=256m -XX:MaxPermSize=512m'\" >> /etc/tomcat7/tomcat7.conf\n",
132+
133+
"# Tomcat Setup\n",
134+
"chown -R tomcat:tomcat /usr/share/tomcat7/\n",
135+
"chkconfig tomcat7 on\n",
136+
"chkconfig --level 345 tomcat7 on\n",
137+
138+
"# Configure Apache HTTPD\n",
139+
"chkconfig httpd on\n",
140+
"chkconfig --level 345 httpd on\n",
141+
142+
"# Proxy all requests to Tomcat\n",
143+
"echo \"ProxyPass / ajp://localhost:8009/\" >> /etc/httpd/conf/httpd.conf\n",
144+
145+
"# Start servers\n",
146+
"service tomcat7 start\n",
147+
"/etc/init.d/httpd start\n",
148+
149+
"# Send signal to WaitHandle that the setup is completed\n",
150+
"/opt/aws/bin/cfn-signal", " -e 0", " '", { "Ref" : "WaitHandle" }, "'","\n",
151+
152+
"date > /home/ec2-user/stoptime"
153+
]]
154+
}
155+
}
156+
157+
}
158+
159+
},
160+
161+
"IPAddress" :
162+
{
163+
"Type" : "AWS::EC2::EIP"
164+
},
165+
166+
"IPAssoc" :
167+
{
168+
"Type" : "AWS::EC2::EIPAssociation",
169+
"Properties" :
170+
{
171+
"InstanceId" : { "Ref" : "WebServer" },
172+
"EIP" : { "Ref" : "IPAddress" }
173+
}
174+
},
175+
176+
"WaitHandle" : {
177+
"Type" : "AWS::CloudFormation::WaitConditionHandle"
178+
},
179+
180+
"WaitCondition" : {
181+
"Type" : "AWS::CloudFormation::WaitCondition",
182+
"DependsOn" : "WebServer",
183+
"Properties" : {
184+
"Handle" : { "Ref" : "WaitHandle" },
185+
"Timeout" : "1200"
186+
}
187+
}
188+
189+
},
190+
191+
192+
"Outputs" : {
193+
"InstanceIPAddress" : {
194+
"Value" : { "Ref" : "IPAddress" },
195+
"Description" : "public IP address of the new WebServer"
196+
},
197+
"InstanceName" : {
198+
"Value" : { "Fn::GetAtt" : [ "WebServer", "PublicDnsName" ]},
199+
"Description" : "public DNS name of the new WebServer"
200+
},
201+
"WebsiteURL" : {
202+
"Description" : "URL for website",
203+
"Value" : { "Fn::Join" : ["", ["http://", { "Fn::GetAtt" : [ "WebServer", "PublicDnsName" ]}]] }
204+
},
205+
"SSH" : {
206+
"Description" : "Command to quickly SSH into box",
207+
"Value" : { "Fn::Join" : ["", ["ssh ec2-user@", { "Fn::GetAtt" : [ "WebServer", "PublicDnsName" ]}, " -i NGPP.pem" ]] }
208+
}
209+
}
210+
211+
}

cloud-formation/really-basic.json

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
{
2+
"AWSTemplateFormatVersion" : "2010-09-09",
3+
4+
"Description" : "Simple template to create EC2 instances with Apache and Tomcat installed.",
5+
6+
"Parameters" :
7+
{
8+
9+
"KeyName" :
10+
{
11+
"Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instances",
12+
"Type" : "AWS::EC2::KeyPair::KeyName",
13+
"ConstraintDescription" : "must be the name of an existing EC2 KeyPair."
14+
},
15+
16+
"InstanceType" :
17+
{
18+
"Description" : "FormEngine EC2 instance type",
19+
"Type" : "String",
20+
"Default" : "t2.micro"
21+
}
22+
23+
},
24+
25+
"Mappings" :
26+
{
27+
"AWSInstanceType2Arch" :
28+
{
29+
"t2.micro" : { "Arch" : "64" },
30+
"t2.small" : { "Arch" : "64" },
31+
"t2.medium" : { "Arch" : "64" },
32+
"t2.large" : { "Arch" : "64" }
33+
},
34+
"AWSRegionArch2AMI" : {
35+
"eu-west-1" : { "64" : "ami-d41d58a7" }
36+
}
37+
},
38+
39+
"Resources" :
40+
{
41+
"WebServerGroup" :
42+
{
43+
"Type" : "AWS::EC2::SecurityGroup",
44+
"Properties" :
45+
{
46+
"GroupDescription" : "Enable SSH and HTTP access",
47+
"SecurityGroupIngress" :
48+
[
49+
{ "IpProtocol" : "tcp", "FromPort" : "22", "ToPort" : "22", "CidrIp" : "0.0.0.0/0" },
50+
{ "IpProtocol" : "tcp", "FromPort" : "80", "ToPort" : "80", "CidrIp" : "0.0.0.0/0" }
51+
]
52+
}
53+
},
54+
55+
"WebServer":
56+
{
57+
"Type" : "AWS::EC2::Instance",
58+
"Metadata" :
59+
{
60+
"AWS::CloudFormation::Init":
61+
{
62+
"config" :
63+
{
64+
"packages" :
65+
{
66+
"yum" :
67+
{
68+
"java-1.6.0-openjdk": [],
69+
"tomcat6": [],
70+
"httpd": []
71+
}
72+
}
73+
}
74+
}
75+
},
76+
77+
"Properties": {
78+
79+
"ImageId": {
80+
"Fn::FindInMap": [
81+
"AWSRegionArch2AMI",
82+
{"Ref": "AWS::Region"},
83+
{
84+
"Fn::FindInMap": [
85+
"AWSInstanceType2Arch",
86+
{"Ref": "InstanceType"},
87+
"Arch"
88+
]
89+
}
90+
]
91+
},
92+
93+
"InstanceType": {"Ref": "InstanceType"},
94+
"SecurityGroups": [{"Ref": "WebServerGroup"}],
95+
"KeyName": {"Ref": "KeyName"},
96+
"Tags": [
97+
{
98+
"Key": "Name",
99+
"Value": "WebServer"
100+
}
101+
]
102+
103+
}
104+
105+
},
106+
107+
"IPAddress" :
108+
{
109+
"Type" : "AWS::EC2::EIP"
110+
},
111+
112+
"IPAssoc" :
113+
{
114+
"Type" : "AWS::EC2::EIPAssociation",
115+
"Properties" :
116+
{
117+
"InstanceId" : { "Ref" : "WebServer" },
118+
"EIP" : { "Ref" : "IPAddress" }
119+
}
120+
},
121+
122+
"WaitHandle" : {
123+
"Type" : "AWS::CloudFormation::WaitConditionHandle"
124+
},
125+
126+
"WaitCondition" : {
127+
"Type" : "AWS::CloudFormation::WaitCondition",
128+
"DependsOn" : "WebServer",
129+
"Properties" : {
130+
"Handle" : { "Ref" : "WaitHandle" },
131+
"Timeout" : "1200"
132+
}
133+
}
134+
135+
},
136+
137+
138+
"Outputs" : {
139+
"InstanceIPAddress" : {
140+
"Value" : { "Ref" : "IPAddress" },
141+
"Description" : "public IP address of the new WebServer"
142+
},
143+
"InstanceName" : {
144+
"Value" : { "Fn::GetAtt" : [ "WebServer", "PublicDnsName" ]},
145+
"Description" : "public DNS name of the new WebServer"
146+
}
147+
}
148+
149+
}

0 commit comments

Comments
 (0)