-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMySQL_Single_Instance.template
188 lines (171 loc) · 6.13 KB
/
MySQL_Single_Instance.template
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "A Database instance running a local MySQL server",
"Parameters" : {
"KeyName" : {
"Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instances",
"Type" : "String"
},
"InstanceType" : {
"Description" : "Database server EC2 instance type",
"Type" : "String",
"AllowedValues" : [ "m1.tiny", "m1.small", "m1.medium", "m1.large", "m1.xlarge" ],
"ConstraintDescription" : "must be a valid EC2 instance type."
},
"DBName": {
"Description" : "The 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."
},
"DBUsername": {
"NoEcho": "true",
"Description" : "The database admin account username",
"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" : "The database admin account password",
"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",
"Type": "String",
"MinLength": "1",
"MaxLength": "41",
"AllowedPattern" : "[a-zA-Z0-9]*",
"ConstraintDescription" : "must contain only alphanumeric characters."
},
"LinuxDistribution": {
"Description" : "Distribution of choice",
"Type": "String",
"AllowedValues" : [ "F18", "F17", "U10", "RHEL-6.1", "RHEL-6.2", "RHEL-6.3" ],
"Default": "F17"
}
},
"Mappings" : {
"AWSInstanceType2Arch" : {
"m1.tiny" : { "Arch" : "32" },
"m1.small" : { "Arch" : "64" },
"m1.medium" : { "Arch" : "64" },
"m1.large" : { "Arch" : "64" },
"m1.xlarge" : { "Arch" : "64" }
},
"DistroArch2AMI": {
"F18" : { "32" : "F18-i386-cfntools", "64" : "F18-x86_64-cfntools" },
"F17" : { "32" : "F17-i386-cfntools", "64" : "F17-x86_64-cfntools" },
"U10" : { "32" : "U10-i386-cfntools", "64" : "U10-x86_64-cfntools" },
"RHEL-6.1" : { "32" : "rhel61-i386-cfntools", "64" : "rhel61-x86_64-cfntools" },
"RHEL-6.2" : { "32" : "rhel62-i386-cfntools", "64" : "rhel62-x86_64-cfntools" },
"RHEL-6.3" : { "32" : "rhel63-i386-cfntools", "64" : "rhel63-x86_64-cfntools" }
}
},
"Resources" : {
"MySqlDatabaseServer": {
"Type": "AWS::EC2::Instance",
"Metadata" : {
"AWS::CloudFormation::Init" : {
"configSets" : {
"proxy" : [ "config1" , "config2" ]
},
"config1": {
"files": {
"/etc/yum.conf": {
"content": { "Fn::Join": ["", [
"[main]\n",
"cachedir=/var/cache/yum/$basearch/$releasever\n",
"keepcache=0\n",
"debuglevel=2\n",
"logfile=/var/log/yum.log\n",
"exactarch=1\n",
"obsoletes=1\n",
"gpgcheck=1\n",
"plugins=1\n",
"installonly_limit=3\n",
"# The proxy server - proxy server:port number\n",
"proxy=http://173.36.196.10:80\n",
"\n"
]]},
"mode": "000644",
"owner": "root",
"group": "root"
}
}
},
"config2" : {
"packages" : {
"yum" : {
"mysql" : [],
"mysql-server" : []
}
},
"services" : {
"systemd" : {
"mysqld" : { "enabled" : "true", "ensureRunning" : "true" }
}
}
}
}
},
"Properties": {
"ImageId" : { "Fn::FindInMap" : [ "DistroArch2AMI", { "Ref" : "LinuxDistribution" },
{ "Fn::FindInMap" : [ "AWSInstanceType2Arch", { "Ref" : "InstanceType" }, "Arch" ] } ] },
"InstanceType" : { "Ref" : "InstanceType" },
"KeyName" : { "Ref" : "KeyName" },
"UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash -v\n",
"# Helper function\n",
"function error_exit\n",
"{\n",
" /opt/aws/bin/cfn-signal -e 1 -r \"$1\" '", { "Ref" : "MySqlWaitHandle" }, "'\n",
" exit 1\n",
"}\n",
"/opt/aws/bin/cfn-init -c proxy -s ", { "Ref" : "AWS::StackName" },
" -r MySqlDatabaseServer ",
" --region ", { "Ref" : "AWS::Region" },
" || error_exit 'Failed to run cfn-init - RP'\n",
"# Setup MySQL root password and create a user\n",
"mysqladmin -u root password '", { "Ref" : "DBRootPassword" }, "'\n",
"cat << EOF | mysql -u root --password='", { "Ref" : "DBRootPassword" }, "'\n",
"CREATE DATABASE ", { "Ref" : "DBName" }, ";\n",
"GRANT ALL PRIVILEGES ON ", { "Ref" : "DBName" }, ".* TO \"", { "Ref" : "DBUsername" }, "\"@\"%\"\n",
"IDENTIFIED BY \"", { "Ref" : "DBPassword" }, "\";\n",
"FLUSH PRIVILEGES;\n",
"EXIT\n",
"EOF\n",
"# All is well so signal success\n",
"/opt/aws/bin/cfn-signal -e 0 -r \"MySQL Database setup complete\" '",
{ "Ref" : "MySqlWaitHandle" }, "'\n"
]]}}
}
},
"MySqlWaitHandle" : {
"Type" : "AWS::CloudFormation::WaitConditionHandle"
},
"MySqlWaitCondition" : {
"Type" : "AWS::CloudFormation::WaitCondition",
"DependsOn" : "MySqlDatabaseServer",
"Properties" : {
"Handle" : {"Ref" : "MySqlWaitHandle"},
"Timeout" : "6000"
}
}
},
"Outputs" : {
"PublicIp": {
"Value": { "Fn::GetAtt" : [ "MySqlDatabaseServer", "PublicIp" ] },
"Description": "Database server IP"
}
}
}