-
Notifications
You must be signed in to change notification settings - Fork 0
/
cscb025_cloudformation_template.yml
173 lines (173 loc) · 6.09 KB
/
cscb025_cloudformation_template.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
AWSTemplateFormatVersion: '2010-09-09'
Description: 'AWS Cloudformation template to create LAMP Stack'
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: must be the name of an existing EC2 KeyPair.
DBName:
Default: cscb025
Description: MySQL database name
Type: String
MinLength: '1'
MaxLength: '64'
AllowedPattern: '[a-zA-Z][a-zA-Z0-9]*'
ConstraintDescription: must begin with a letter and contain only alphanumeric
characters.
DBUser:
NoEcho: 'true'
Description: Username for MySQL database access
Default: cscb025
Type: String
MinLength: '1'
MaxLength: '16'
AllowedPattern: '[a-zA-Z][a-zA-Z0-9]*'
ConstraintDescription: must begin with a letter and contain only alphanumeric
characters.
DBPassword:
NoEcho: 'true'
Description: Password for MySQL database access
Default: cscb025
Type: String
MinLength: '1'
MaxLength: '41'
AllowedPattern: '[a-zA-Z0-9]*'
ConstraintDescription: must contain only alphanumeric characters.
DBRootPassword:
NoEcho: 'true'
Description: Root password for MySQL
Default: cscb025
Type: String
MinLength: '1'
MaxLength: '41'
AllowedPattern: '[a-zA-Z0-9]*'
ConstraintDescription: must contain only alphanumeric characters.
InstanceAmi:
Description: AMI of your region Amazon Linux (HVM). Set for eu-west-1 by default.
Type: String
Default: ami-acd005d5
AllowedPattern: (ami-[a-f0-9]+)
ConstraintDescription: must be a valid AMI Image ID
Repository:
Default: 'https://github.com/miglen/CSCB025.git'
Description: Location of the repository to be deployed
Type: String
SSHLocation:
Description: ' The IP address range that can be used to SSH to the EC2 instances'
Type: String
MinLength: '9'
MaxLength: '18'
Default: 0.0.0.0/0
AllowedPattern: (\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})
ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.
Resources:
WebServerInstance:
Type: AWS::EC2::Instance
Metadata:
AWS::CloudFormation::Init:
configSets:
InstallAndRun:
- Install
- Configure
Install:
packages:
yum:
git: []
mysql: []
mysql-server: []
mysql-libs: []
httpd24: []
php71: []
php71-mysqlnd: []
files:
/etc/cfn/cfn-hup.conf:
content: !Sub |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
mode: '000400'
owner: root
group: root
/etc/cfn/hooks.d/cfn-auto-reloader.conf:
content: !Sub |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.WebServerInstance.Metadata.AWS::CloudFormation::Init
action=/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource WebServerInstance --configsets InstallAndRun --region ${AWS::Region}
runas=root
/tmp/setup.sh:
content: !Sub |
#!/bin/bash
# Reset MySQL root password
sleep 5
mysqladmin -u root password '${DBRootPassword}'
# Create user and table
mysql -u root --password='${DBRootPassword}' -e "create database ${DBName};"
mysql -u root --password='${DBRootPassword}' -e "create user ${DBUser};"
mysql -u root --password='${DBRootPassword}' -e "grant all on ${DBName}.* to '${DBUser}'@'localhost' identified by '${DBPassword}';"
# Clone the repository
git clone ${Repository} /var/www/html/
# Import the schema
mysql ${DBName} -u root --password='${DBRootPassword}' < /var/www/html/schema.sql
# Cleanup all files
rm -f /var/www/html/schema.sql
rm -f /tmp/setup.sh
mode: '000400'
owner: root
group: root
services:
sysvinit:
mysqld:
enabled: 'true'
ensureRunning: 'true'
httpd:
enabled: 'true'
ensureRunning: 'true'
cfn-hup:
enabled: 'true'
ensureRunning: 'true'
files:
- /etc/cfn/cfn-hup.conf
- /etc/cfn/hooks.d/cfn-auto-reloader.conf
Configure:
commands:
01_setup:
command: "bash /tmp/setup.sh"
test: "ls /var/www/html/index.php"
Properties:
ImageId: !Ref 'InstanceAmi'
InstanceType: 't2.micro'
SecurityGroups:
- !Ref 'WebServerSecurityGroup'
KeyName: !Ref 'KeyName'
UserData:
Fn::Base64: !Sub |
#!/bin/bash -xe
yum update -y aws-cfn-bootstrap
# Install the files and packages from the metadata
/opt/aws/bin/cfn-init -v --stack ${AWS::StackName} --resource WebServerInstance --configsets InstallAndRun --region ${AWS::Region}
# Signal the status from cfn-init
/opt/aws/bin/cfn-signal -e $? --stack ${AWS::StackName} --resource WebServerInstance --region ${AWS::Region}
CreationPolicy:
ResourceSignal:
Timeout: PT5M
WebServerSecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupDescription: Enable HTTP access via port 80
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '80'
ToPort: '80'
CidrIp: 0.0.0.0/0
- IpProtocol: tcp
FromPort: '22'
ToPort: '22'
CidrIp: !Ref 'SSHLocation'
Outputs:
WebsiteURL:
Description: URL for your web server
Value: !Join ['', ['http://', !GetAtt [WebServerInstance, PublicDnsName]]]
SSHCommand:
Description: SSH Command
Value: !Join ['', ['ssh -i ', !Ref KeyName, '.pem ', 'ec2-user@', !GetAtt [WebServerInstance, PublicIp]]]