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+ }
0 commit comments