forked from adoreboard/aws-cloudformation-templates
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMongoDbConfigServer.template
324 lines (288 loc) · 13.1 KB
/
MongoDbConfigServer.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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "MongoDB Config Server (Amazon Linux)",
"Parameters": {
"KeyName": {
"Description": "The name of an existing key pair to enable SSH access to Amazon EC2 instances",
"Type": "String",
"Default": ""
},
"InstanceType": {
"Description": "MongoDB Node Type (EC2 Instance)",
"Type": "String",
"Default": "t2.micro",
"AllowedValues": [
"t2.micro",
"t2.small",
"t2.medium"
],
"ConstraintDescription": "Must be a valid Amazon EC2 instance type."
},
"AccessKeyId" : {
"Description" : "Access Key ID",
"Type" : "String",
"Default" : ""
},
"SecretAccessKey" : {
"Description" : "Secret Access Key for the specified Access Key ID",
"Type" : "String",
"Default" : ""
},
"ConfigServerNumber" : {
"Description" : "Number of Mongo config server",
"Type" : "String",
"AllowedValues" : ["1","2","3"],
"Default" : "1"
},
"MongoConfigVpcId": {
"Description": "The VPC of the MongoDB Config Server",
"Type" : "AWS::EC2::VPC::Id",
"Default": ""
},
"MongoConfigSubnetId": {
"Description": "The subnet of the MongoDB Config Server",
"Type" : "AWS::EC2::Subnet::Id",
"Default": ""
},
"MongoConfigGroupId": {
"Description": "The security group for the MongoDB Config Server",
"Type": "AWS::EC2::SecurityGroup::Id",
"Default": ""
},
"UpdateRoute53": {
"Description": "Add record set to AWS Route53 service. Note: don't select this option if you want to update the Route53 table separately",
"Type": "String",
"Default": "True",
"AllowedValues": [ "True", "False" ]
},
"Route53HostedZone": {
"Description": "Route 53 hosted zone for updating internal DNS (Only applicable if the parameter [ UpdateRoute53 ] = \"true\"",
"Type": "AWS::Route53::HostedZone::Id",
"Default": ""
}
},
"Mappings": {
"AWSInstanceType2Arch": {
"t2.micro" : { "Arch": "64HVM" },
"t2.small" : { "Arch": "64" },
"t2.medium" : { "Arch": "64" }
},
"AWSRegionArch2AMI": {
"eu-west-1" : { "64" : "ami-2918e35e", "64HVM" : "ami-4b18e33c" },
"us-east-1" : { "64" : "ami-fb8e9292", "64HVM" : "ami-978d91fe" },
"us-west-1" : { "64" : "ami-7aba833f", "64HVM" : "ami-5aba831f" }
}
},
"Resources": {
"MongoDbConfigServer": {
"Type": "AWS::EC2::Instance",
"Metadata": {
"AWS::CloudFormation::Init": {
"Comment1" : "This creates an instance (generally a micro) and installs a MongoDB config server",
"configSets" : {
"Install" : [ "Metric-Uploading-Config", "Install-MongoDB", "Update-Route53" ]
},
"Metric-Uploading-Config" : {
"packages" : {
"yum" : {
"perl-Sys-Syslog" : [],
"perl-Switch" : [],
"perl-URI" : [],
"perl-IO-Compress" : [],
"perl-Bundle-LWP" : []
}
},
"sources" : {
"/home/ec2-user" : "http://ec2-downloads.s3.amazonaws.com/cloudwatch-samples/CloudWatchMonitoringScripts-v1.1.0.zip"
},
"files" : {
"/home/ec2-user/crontab": {
"content" : { "Fn::Join" : ["", [
"*/1 * * * * ~/aws-scripts-mon/mon-put-instance-data.pl --aws-iam-role=MonitorRole --mem-used --memory-units=megabytes --mem-util --disk-space-util --disk-space-used --disk-space-avail --disk-path=/","\n"
]]},
"mode" : "000600",
"owner" : "ec2-user",
"group" : "ec2-user"
}
},
"commands" : {
"01_YumUpdateMonitoring" : {
"command" : "yum update -y"
},
"02_MonitoringCron" : {
"command" : "chmod +x /home/ec2-user/aws-scripts-mon/*.pl && crontab -u ec2-user /home/ec2-user/crontab && rm /home/ec2-user/crontab"
}
}
},
"Install-MongoDB" : {
"packages" : {
"yum" : {
"mdadm" : [],
"sysstat" : []
}
},
"files" : {
"/etc/yum.repos.d/mongodb-org-3.0.repo" : {
"content" : { "Fn::Join" : ["", [
"[mongodb-org-3.0]\n",
"name=MongoDB Repository\n",
"baseurl=https://repo.mongodb.org/yum/amazon/2013.03/mongodb-org/3.0/x86_64/\n",
"gpgcheck=0\n",
"enabled=1\n"
] ] },
"mode" : "000644",
"owner" : "root",
"group" : "root"
},
"/etc/mongod.conf" : {
"content" : { "Fn::Join" : ["", [
"net:\n",
" bindIp: 0.0.0.0\n",
" port: 27017\n",
" http:\n",
" enabled: true\n",
" RESTInterfaceEnabled: true\n\n",
"systemLog:\n",
" destination: file\n",
" path: /var/log/mongodb/mongod.log\n",
" logAppend: true\n\n",
"storage:\n",
" dbPath: /var/lib/mongo\n",
" journal:\n",
" enabled: true\n\n",
"processManagement:\n",
" fork: true\n",
" pidFilePath: /var/run/mongodb/mongod.pid\n\n",
"sharding:\n",
" clusterRole: configsvr\n"
] ] },
"mode" : "000644",
"owner" : "root",
"group" : "root"
}
},
"commands" : {
"01_YumUpdateMongo" : {
"command" : "yum update -y\n"
},
"02_InstallMongoDBServerAndClient" : {
"command" : "yum install -y mongodb-org-server mongodb-org-shell > /tmp/yum-mongo.log 2>&1\n"
}
},
"services" : {
"sysvinit" : {
"mongod" : { "enabled" : "true", "ensureRunning" : "true" }
}
}
},
"Update-Route53" : {
"files" : {
"/usr/local/bin/cli53" : {
"source" : "https://github.com/barnybug/cli53/releases/download/0.6.3/cli53-linux-amd64",
"mode" : "000755",
"owner" : "root",
"group" : "root"
},
"/tmp/update_route53.sh" : {
"content" : { "Fn::Join" : ["", [
"#!/bin/bash\n\n",
"PRIVATE_IP=`curl http://169.254.169.254/latest/meta-data/local-ipv4/`\n",
"UPDATE_ROUTE_53=", {"Ref": "UpdateRoute53" }, "\n",
"if [ $UPDATE_ROUTE_53 == 'True' ] ; then \n",
" /usr/local/bin/cli53 rrcreate ", {"Ref": "Route53HostedZone" },
" \"mongo-cfg", { "Ref" : "ConfigServerNumber" }, " 300 A $PRIVATE_IP\" --replace ; \n",
"fi\n",
"echo Route53 Update Finished!"
] ] },
"mode" : "000755",
"owner" : "root",
"group" : "root"
}
},
"commands" : {
"01_UpdateRoute53" : {
"command" : { "Fn::Join" : ["", [
"/tmp/update_route53.sh > /tmp/update_route53.log 2>&1"
]]}
}
}
}
}
},
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"AWSRegionArch2AMI",
{ "Ref": "AWS::Region" },
{ "Fn::FindInMap": [
"AWSInstanceType2Arch",
{ "Ref": "InstanceType" },
"Arch"
]}
]
},
"InstanceType": {
"Ref": "InstanceType"
},
"SecurityGroupIds": [
{
"Ref" : "MongoConfigGroupId"
}
],
"KeyName": {
"Ref": "KeyName"
},
"SubnetId": {
"Ref": "MongoConfigSubnetId"
},
"IamInstanceProfile": "MonitorRole",
"Monitoring" : "true",
"Tags": [
{
"Key": "Stack",
"Value": "Production"
},
{
"Key": "Name",
"Value": { "Fn::Join" : ["", [
"Mongo-cfg", { "Ref" : "ConfigServerNumber" }
]]}
}
],
"UserData": { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash\n",
"yum update -y aws-cfn-bootstrap\n",
"## Error reporting helper function\n",
"function error_exit\n",
"{\n",
" /opt/aws/bin/cfn-signal --exit-code 1 --reason \"$1\" '", { "Ref" : "WaitHandleConfigServer" }, "'\n",
" exit 1\n",
"}\n",
"## Initialize CloudFormati\n",
"/opt/aws/bin/cfn-init -v ",
" --stack ", { "Ref" : "AWS::StackName" },
" --resource MongoDbConfigServer ",
" --configsets Install ",
" --access-key ", { "Ref" : "AccessKeyId" },
" --secret-key ", {"Ref": "SecretAccessKey" },
" --region ", { "Ref" : "AWS::Region" },
" > /tmp/cfn-init.log 2>&1 || error_exit $(</tmp/cfn-init.log)\n",
"## CloudFormation signal that setup is complete\n",
"/opt/aws/bin/cfn-signal --exit-code 0 --reason \"MongoDB Config Server instance setup complete\" '", { "Ref" : "WaitHandleConfigServer" }, "'\n"
]]}}
}
},
"WaitHandleConfigServer" : {
"Type" : "AWS::CloudFormation::WaitConditionHandle",
"Properties" : {}
},
"WaitConditionConfigServer" : {
"Type" : "AWS::CloudFormation::WaitCondition",
"DependsOn" : "MongoDbConfigServer",
"Properties" : {
"Handle" : { "Ref" : "WaitHandleConfigServer" },
"Timeout" : "600"
}
}
}
}