-
Notifications
You must be signed in to change notification settings - Fork 11
/
MongoDB_ReplicaSetStack.template
398 lines (351 loc) · 16.8 KB
/
MongoDB_ReplicaSetStack.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "MongoDB replica set stack",
"Parameters" : {
"KeyName" : {
"Description" : "Name of an existing EC2 KeyPair to enable SSH access",
"Type" : "String"
},
"InstanceType" : {
"Type" : "String",
"Default" : "m1.large",
"AllowedValues" : [ "m1.large", "m1.xlarge", "m2.xlarge", "m2.2xlarge", "m2.4xlarge", "c1.xlarge", "cc1.4xlarge" ],
"Description" : "EC2 instance type (e.g. m1.large, m1.xlarge, m2.xlarge)"
},
"VolumeSize" : {
"Description" : "Volume size for each EBS volume",
"Type" : "Number",
"Default" : "10"
},
"ReplicaSetName" : {
"Description" : "Name for the MongoDB replica set",
"Type" : "String",
"Default" : "MyReplicaSet"
}
},
"Mappings" : {
"InstanceTypeArch" : {
"m1.small" : { "Arch" : "64" },
"m1.medium" : { "Arch" : "64" },
"m1.large" : { "Arch" : "64" },
"m1.xlarge" : { "Arch" : "64" },
"m2.xlarge" : { "Arch" : "64" },
"m2.2xlarge" : { "Arch" : "64" },
"m2.4xlarge" : { "Arch" : "64" },
"c1.medium" : { "Arch" : "64" },
"c1.xlarge" : { "Arch" : "64" },
"cc1.4xlarge" : { "Arch" : "64HVM" }
},
"RegionImageZone" : {
"us-east-1" : { "64" : "ami-e565ba8c", "64HVM" : "ami-e965ba80" },
"us-west-2" : { "64" : "ami-3ac64a0a", "64HVM" : "NOT_YET_SUPPORTED" },
"us-west-1" : { "64" : "ami-e78cd4a2", "64HVM" : "NOT_YET_SUPPORTED" },
"eu-west-1" : { "64" : "ami-f9231b8d", "64HVM" : "NOT_YET_SUPPORTED" },
"ap-southeast-1": { "64" : "ami-be3374ec", "64HVM" : "NOT_YET_SUPPORTED" },
"ap-northeast-1": { "64" : "ami-e47acbe5", "64HVM" : "NOT_YET_SUPPORTED" },
"sa-east-1" : { "64" : "ami-a6855bbb", "64HVM" : "NOT_YET_SUPPORTED" }
},
"RegionZone" : {
"us-east-1" : {"AZ1" : "us-east-1a", "AZ2" : "us-east-1b", "AZ3" : "us-east-1b", "AZ4" : "us-east-1d", "AZ5" : "us-east-1e"},
"us-west-1" : {"AZ1" : "us-west-1a", "AZ2" : "us-west-1b", "AZ3" : "us-west-1c"},
"us-west-2" : {"AZ1" : "us-west-2a", "AZ2" : "us-west-2b"},
"eu-west-1" : {"AZ1" : "eu-west-1a", "AZ2" : "eu-west-1b", "AZ3" : "eu-west-1c"},
"ap-northeast-1": {"AZ1" : "ap-northeast-1a", "AZ2" : "ap-northeast-1b"},
"ap-southeast-1": {"AZ1" : "ap-southeast-1a", "AZ2" : "ap-southeast-1b"},
"sa-east-1" : {"AZ1" : "sa-east-1a", "AZ2" : "sa-east-1b"}
}
},
"Resources" : {
"MongoSecurityGroup" : {
"Type" : "AWS::EC2::SecurityGroup",
"Properties" : {
"GroupDescription" : "ReplicaSet security group",
"SecurityGroupIngress" : [
{
"IpProtocol" : "tcp",
"FromPort" : "22",
"ToPort" : "22",
"CidrIp" : "0.0.0.0/0"
}
]
}
},
"MongoSecurityGroupIngress" : {
"Type" : "AWS::EC2::SecurityGroupIngress",
"Properties" : {
"GroupName" : { "Ref" : "MongoSecurityGroup" },
"IpProtocol" : "tcp",
"FromPort" : "27017",
"ToPort" : "27017",
"SourceSecurityGroupName" : { "Ref" : "MongoSecurityGroup" }
}
},
"StatusIngress" : {
"Type" : "AWS::EC2::SecurityGroupIngress",
"Properties" : {
"GroupName" : { "Ref" : "MongoSecurityGroup" },
"IpProtocol" : "tcp",
"FromPort" : "28017",
"ToPort" : "28017",
"SourceSecurityGroupName" : { "Ref" : "MongoSecurityGroup" }
}
},
"CfnUser" : {
"Type" : "AWS::IAM::User",
"Properties" : {
"Path": "/",
"Policies": [ {
"PolicyName": "root",
"PolicyDocument": { "Statement": [ {
"Effect":"Allow",
"Action":"cloudformation:DescribeStackResource",
"Resource":"*"
} ] }
} ]
}
},
"AccessKey" : {
"Type" : "AWS::IAM::AccessKey",
"Properties" : {
"UserName" : { "Ref" : "CfnUser" }
}
},
"ReplicaSetMember1" : {
"Type" : "AWS::CloudFormation::Stack",
"Properties" : {
"TemplateURL" : "http://S3URL/MongoDB_ReplicaSetMember.template",
"Parameters" : {
"KeyName" : { "Ref" : "KeyName" },
"InstanceType" : { "Ref" : "InstanceType" },
"VolumeSize" : { "Ref" : "VolumeSize" },
"AccessKeyId" : { "Ref" : "AccessKey" },
"SecretAccessKey" : {"Fn::GetAtt": ["AccessKey", "SecretAccessKey"]},
"ReplicaSetName" : { "Ref" : "ReplicaSetName" },
"SecurityGroupName" : { "Ref" : "MongoSecurityGroup" },
"InstanceZone" : { "Fn::FindInMap" : ["RegionZone", { "Ref" : "AWS::Region"}, "AZ2"] }
}
}
},
"ReplicaSetMember2" : {
"Type" : "AWS::CloudFormation::Stack",
"DependsOn" : "ReplicaSetMember1",
"Properties" : {
"TemplateURL" : "http://S3URL/MongoDB_ReplicaSetMember.template",
"Parameters" : {
"KeyName" : { "Ref" : "KeyName" },
"InstanceType" : { "Ref" : "InstanceType" },
"VolumeSize" : { "Ref" : "VolumeSize" },
"AccessKeyId" : { "Ref" : "AccessKey" },
"SecretAccessKey" : {"Fn::GetAtt": ["AccessKey", "SecretAccessKey"]},
"ReplicaSetName" : { "Ref" : "ReplicaSetName" },
"SecurityGroupName" : { "Ref" : "MongoSecurityGroup" },
"InstanceZone" : { "Fn::FindInMap" : ["RegionZone", { "Ref" : "AWS::Region"}, "AZ3"] }
}
}
},
"MongoInstanceP1" : {
"Type" : "AWS::EC2::Instance",
"Metadata" : {
"AWS::CloudFormation::Init" : {
"config" : {
"packages" : {
"yum" : {
"mdadm" : [],
"sysstat" : []
}
},
"files" : {
"/etc/yum.repos.d/10gen.repo" : {
"content" : { "Fn::Join" : ["", [
"[10gen]\n",
"name=10gen Repository\n",
"baseurl=http://downloads-distro.mongodb.org/repo/redhat/os/x86_64\n",
"gpgcheck=0\n"
] ] },
"mode" : "000644",
"owner" : "root",
"group" : "root"
}
}
}
}
},
"DependsOn" : "ReplicaSetMember2",
"Properties" : {
"InstanceType" : { "Ref" : "InstanceType" },
"ImageId" : { "Fn::FindInMap" : [ "RegionImageZone", { "Ref" : "AWS::Region" },
{ "Fn::FindInMap" : [ "InstanceTypeArch", { "Ref" : "InstanceType" }, "Arch" ] } ] },
"SecurityGroups" : [ { "Ref" : "MongoSecurityGroup" } ],
"KeyName" : { "Ref" : "KeyName" },
"AvailabilityZone" : { "Fn::FindInMap" : ["RegionZone", { "Ref" : "AWS::Region"}, "AZ1"] },
"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 -e 1 -r \"$1\" '", { "Ref" : "WaitHandleP1" }, "'\n",
" exit 1\n",
"}\n",
"## Initialize CloudFormation bits\n",
"/opt/aws/bin/cfn-init -v -s ", { "Ref" : "AWS::StackName" }, " -r MongoInstanceP1",
" --access-key ", { "Ref" : "AccessKey" },
" --secret-key ", {"Fn::GetAtt": ["AccessKey", "SecretAccessKey"]},
" --region ", { "Ref" : "AWS::Region" }, " > /tmp/cfn-init.log 2>&1 || error_exit $(</tmp/cfn-init.log)\n",
"## Waiting for EBS mounts to become available\n",
"while [ ! -e /dev/sdh1 ]; do echo waiting for /dev/sdh1 to attach; sleep 10; done\n",
"while [ ! -e /dev/sdh2 ]; do echo waiting for /dev/sdh2 to attach; sleep 10; done\n",
"while [ ! -e /dev/sdh3 ]; do echo waiting for /dev/sdh3 to attach; sleep 10; done\n",
"while [ ! -e /dev/sdh4 ]; do echo waiting for /dev/sdh4 to attach; sleep 10; done\n",
"yum -y install mongo-10gen-server > /tmp/yum-mongo.log 2>&1\n",
"## Create RAID10 and persist configuration\n",
"mdadm --verbose --create /dev/md0 --level=10 --chunk=256 --raid-devices=4 /dev/sdh1 /dev/sdh2 /dev/sdh3 /dev/sdh4 > /tmp/mdadm.log 2>&1\n",
"echo '`mdadm --detail --scan`' | tee -a /etc/mdadm.conf\n",
"## Set read-ahead on each device\n",
"blockdev --setra 128 /dev/md0\n",
"blockdev --setra 128 /dev/sdh1\n",
"blockdev --setra 128 /dev/sdh2\n",
"blockdev --setra 128 /dev/sdh3\n",
"blockdev --setra 128 /dev/sdh4\n",
"## Create physical and logical volumes\n",
"dd if=/dev/zero of=/dev/md0 bs=512 count=1\n",
"pvcreate /dev/md0\n",
"vgcreate vg0 /dev/md0\n",
"lvcreate -l 90%vg -n data vg0\n",
"lvcreate -l 5%vg -n log vg0\n",
"lvcreate -l 5%vg -n journal vg0\n",
"## Create filesystems and mount point info\n",
"mke2fs -t ext4 -F /dev/vg0/data > /tmp/mke2fs1.log 2>&1\n",
"mke2fs -t ext4 -F /dev/vg0/log > /tmp/mke2fs2.log 2>&1\n",
"mke2fs -t ext4 -F /dev/vg0/journal > /tmp/mke2fs3.log 2>&1\n",
"mkdir /data\n",
"mkdir /log\n",
"mkdir /journal\n",
"echo '/dev/vg0/data /data ext4 defaults,auto,noatime,noexec 0 0' | tee -a /etc/fstab\n",
"echo '/dev/vg0/log /log ext4 defaults,auto,noatime,noexec 0 0' | tee -a /etc/fstab\n",
"echo '/dev/vg0/journal /journal ext4 defaults,auto,noatime,noexec 0 0' | tee -a /etc/fstab\n",
"mount /data > /tmp/mount1.log 2>&1\n",
"mount /log > /tmp/mount2.log 2>&1\n",
"mount /journal > /tmp/mount3.log 2>&1\n",
"ln -s /journal /data/journal\n",
"chown -R mongod:mongod /data > /tmp/chown1.log 2>&1\n",
"chown -R mongod:mongod /log > /tmp/chown2.log 2>&1\n",
"chown -R mongod:mongod /journal > /tmp/chown3.log 2>&1\n",
"## Update mongod configuration\n",
"cat <<EOF > /etc/mongod.conf\n",
"logpath=/log/mongod.log\n",
"logappend=true\n",
"fork=true\n",
"dbpath=/data\n",
"rest=true\n",
"replSet=", { "Ref" : "ReplicaSetName" } ,"\n",
"EOF\n",
"## Start mongod\n",
"/etc/init.d/mongod start > /tmp/mongod-start.log 2>&1\n",
"## Waiting until mongod has started\n",
"sleep 120\n",
"## Create replica set configuration\n",
"## The following is only needed on the PRIMARY\n",
"HOSTNAME=`/usr/bin/curl -s http://169.254.169.254/latest/meta-data/public-hostname`\n",
"cat <<EOF > /tmp/replicaSetConfigInit.js\n",
"config = {_id: \"", { "Ref" : "ReplicaSetName" } ,"\", members : [",
"{_id : 0, host:\"$HOSTNAME:27017\"},",
"{_id : 1, host:\"", { "Fn::GetAtt" : ["ReplicaSetMember1", "Outputs.ReplicaSetMemberName"] },":27017\"},",
"{_id : 2, host:\"", { "Fn::GetAtt" : ["ReplicaSetMember2", "Outputs.ReplicaSetMemberName"] },":27017\"}",
"]};\n",
"rs.initiate(config);\n",
"EOF\n",
"/usr/bin/mongo /tmp/replicaSetConfigInit.js > /tmp/replica-setup.log 2>&1\n",
"## CloudFormation signal that setup is complete\n",
"/opt/aws/bin/cfn-signal -e 0 -r \"MongoInstanceP1 setup complete\" '", { "Ref" : "WaitHandleP1" }, "'\n"
] ] } }
}
},
"MongoVolume1P1" : {
"Type" : "AWS::EC2::Volume",
"Properties" : {
"Size" : { "Ref" : "VolumeSize" },
"AvailabilityZone" : { "Fn::GetAtt" : [ "MongoInstanceP1", "AvailabilityZone" ]}
}
},
"MongoVolume2P1" : {
"Type" : "AWS::EC2::Volume",
"Properties" : {
"Size" : { "Ref" : "VolumeSize" },
"AvailabilityZone" : { "Fn::GetAtt" : [ "MongoInstanceP1", "AvailabilityZone" ]}
}
},
"MongoVolume3P1" : {
"Type" : "AWS::EC2::Volume",
"Properties" : {
"Size" : { "Ref" : "VolumeSize" },
"AvailabilityZone" : { "Fn::GetAtt" : [ "MongoInstanceP1", "AvailabilityZone" ]}
}
},
"MongoVolume4P1" : {
"Type" : "AWS::EC2::Volume",
"Properties" : {
"Size" : { "Ref" : "VolumeSize" },
"AvailabilityZone" : { "Fn::GetAtt" : [ "MongoInstanceP1", "AvailabilityZone" ]}
}
},
"MongoVolumeMount1P1" : {
"Type" : "AWS::EC2::VolumeAttachment",
"Properties" : {
"InstanceId" : { "Ref" : "MongoInstanceP1" },
"VolumeId" : { "Ref" : "MongoVolume1P1" },
"Device" : "/dev/sdh1"
}
},
"MongoVolumeMount2P1" : {
"Type" : "AWS::EC2::VolumeAttachment",
"Properties" : {
"InstanceId" : { "Ref" : "MongoInstanceP1" },
"VolumeId" : { "Ref" : "MongoVolume2P1" },
"Device" : "/dev/sdh2"
}
},
"MongoVolumeMount3P1" : {
"Type" : "AWS::EC2::VolumeAttachment",
"Properties" : {
"InstanceId" : { "Ref" : "MongoInstanceP1" },
"VolumeId" : { "Ref" : "MongoVolume3P1" },
"Device" : "/dev/sdh3"
}
},
"MongoVolumeMount4P1" : {
"Type" : "AWS::EC2::VolumeAttachment",
"Properties" : {
"InstanceId" : { "Ref" : "MongoInstanceP1" },
"VolumeId" : { "Ref" : "MongoVolume4P1" },
"Device" : "/dev/sdh4"
}
},
"WaitHandleP1" : {
"Type" : "AWS::CloudFormation::WaitConditionHandle",
"Properties" : {}
},
"WaitConditionP1" : {
"Type" : "AWS::CloudFormation::WaitCondition",
"DependsOn" : "MongoInstanceP1",
"Properties" : {
"Handle" : { "Ref" : "WaitHandleP1" },
"Timeout" : "600"
}
}
},
"Outputs" : {
"ReplicaSetMemberName1" : {
"Value" : { "Fn::GetAtt" : [ "MongoInstanceP1", "PublicDnsName" ] },
"Description" : "public DNS name of the MongoDB replica set member 1"
},
"ReplicaSetMemberName2" : {
"Value" : { "Fn::GetAtt" : [ "ReplicaSetMember1", "Outputs.ReplicaSetMemberName" ] },
"Description" : "public DNS name of the MongoDB replica set member 2"
},
"ReplicaSetMemberName3" : {
"Value" : { "Fn::GetAtt" : [ "ReplicaSetMember2", "Outputs.ReplicaSetMemberName" ] },
"Description" : "public DNS name of the MongoDB replica set member 3"
}
}
}