-
Notifications
You must be signed in to change notification settings - Fork 32
/
Copy pathElasticsearchServer.template
381 lines (337 loc) · 16.4 KB
/
ElasticsearchServer.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
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "Elasticsearch Server (Amazon Linux)",
"Parameters": {
"KeyName": {
"Description": "The name of an existing key pair to enable SSH access to Amazon EC2 instances",
"Type": "String",
"MinLength": "1",
"MaxLength": "255",
"AllowedPattern": "[\\x20-\\x7E]*",
"ConstraintDescription": "KeyPair name from 1 to 255 ASCII characters.",
"Default": ""
},
"InstanceType": {
"Description": "Elasticsearch Node Type (EC2 Instance)",
"Type": "String",
"Default": "m3.medium",
"AllowedValues": [
"m3.medium",
"m3.large",
"r3.large"
],
"ConstraintDescription": "Must be a valid Amazon EC2 instance type."
},
"ElasticsearchClusterName" : {
"Description" : "Name of the Elasticsearch replica set e.g. es",
"Type" : "String",
"Default" : "es"
},
"ElasticsearchNumber" : {
"Description" : "Number of server in replica set (used with ElasticsearchClusterName)",
"Type" : "String",
"AllowedValues" : ["1","2","3"],
"Default" : "1"
},
"ElasticsearchVpcId": {
"Description": "The VPC of the Elasticsearch cluster",
"Type" : "AWS::EC2::VPC::Id",
"Default": ""
},
"ElasticsearchSubnetId": {
"Description": "The subnet of the Elasticsearch cluster",
"Type" : "AWS::EC2::Subnet::Id",
"Default": ""
},
"ElasticsearchSecurityGroupId": {
"Description": "The security group for the Elasticsearch cluster",
"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": ""
},
"VolumeSize" : {
"Description" : "Volume size for each EBS volume",
"Type" : "Number",
"Default" : "500"
},
"ElasticsearchVersion": {
"Description": "The version number of Elasticsearch used throughout the application",
"Type": "String",
"Default": "1.4.4",
"AllowedValues": [
"1.4.4"
],
"ConstraintDescription": "Must be a supported version number."
}
},
"Mappings": {
"AWSInstanceType2Arch": {
"m3.medium": {"Arch": "64"},
"m3.large": { "Arch" : "64" },
"r3.large" : { "Arch" : "64HVM"}
},
"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": {
"ElasticsearchServer": {
"Type": "AWS::EC2::Instance",
"Metadata": {
"AWS::CloudFormation::Init": {
"Comment1" : "This creates an instance and installs Elasticseach and Monitoring",
"configSets" : {
"Install" : [ "Update-Route53", "Metric-Uploading-Config", "Install-Elasticsearch"]
},
"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" },
" \"" , { "Ref" : "ElasticsearchClusterName" }, "-", { "Ref" : "ElasticsearchNumber" }, " 300 A $PRIVATE_IP\" --replace --wait ; \n",
"fi\n",
"echo Route53 Update Finished!"
] ] },
"mode" : "000755",
"owner" : "root",
"group" : "root"
}
},
"commands" : {
"01_UpdateRoute53" : {
"command" : "/tmp/update_route53.sh > /tmp/update_route53.log 2>&1"
}
}
},
"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" : "*/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=/ --disk-path=/data \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-Elasticsearch" : {
"packages" : {
"yum" : {
"mdadm" : [],
"sysstat" : []
}
},
"files" : {
"/etc/yum.repos.d/elasticsearch.repo" : {
"content" : { "Fn::Join" : ["", [
"[elasticsearch-1.4]\n",
"name=Elasticsearch repository for 1.4.4 packages\n",
"baseurl=http://packages.elastic.co/elasticsearch/1.4/centos\n",
"gpgcheck=1\n",
"gpgkey=http://packages.elastic.co/GPG-KEY-elasticsearch",
"enabled=1\n"
] ] },
"mode" : "000644",
"owner" : "root",
"group" : "root"
},
"/tmp/config-elasticsearch.sh" : {
"content" : { "Fn::Join" : ["", [
"#!/bin/bash\n\n",
"# Creates elasticsearch config file and injects in zone name.\n\n",
"ZONE_NAME=`aws route53 get-hosted-zone --id ", {"Ref": "Route53HostedZone" }, " | jp 'HostedZone.Name' | sed 's/\"//g' | sed 's/\\.$//' `\n",
"CLUSTER_NAME=", {"Ref" : "ElasticsearchClusterName"}, "\n",
"cat << END > /etc/elasticsearch/elasticsearch.yml\n",
" cluster.name: ", { "Ref" : "ElasticsearchClusterName" }, "\n\n",
" node.name: ", { "Ref" : "ElasticsearchClusterName" }, "-", { "Ref" : "ElasticsearchNumber" }, "\n\n",
" index.number_of_shards: 2\n",
" index.number_of_replicas: 1\n",
" discovery.type: zen\n",
" path.data: /data\n\n",
" discovery.zen.minimum_master_nodes: 2\n",
" discovery.zen.ping.multicast.enabled: false\n",
" discovery.zen.ping.unicast.hosts: ['${CLUSTER_NAME}-1.${ZONE_NAME}', '${CLUSTER_NAME}-2.${ZONE_NAME}']\n" ,
" discovery.zen.ping.timeout: 7\n",
"END\n\n",
"chown elasticsearch:elasticsearch /etc/elasticsearch/elasticsearch.yml\n",
"echo elasticsearch config script finished!\n"
] ] },
"mode" : "000755",
"owner" : "root",
"group" : "root"
}
},
"commands" : {
"01_InstallPublicKey" : {
"command" : "rpm --import https://packages.elastic.co/GPG-KEY-elasticsearch\n"
},
"02_YumUpdateElasticsearch" : {
"command" : "yum update -y\n"
},
"03_InstallElasticsearch" : {
"command" : "yum -y install elasticsearch"
},
"04_InstallHead" : {
"command" : "/usr/share/elasticsearch/bin/plugin -install mobz/elasticsearch-head"
},
"05_config_elasticsearch" : {
"command" : "/tmp/config-elasticsearch.sh > /tmp/config-elasticsearch.log 2>&1"
}
}
}
}
},
"Properties": {
"ImageId": {
"Fn::FindInMap": [
"AWSRegionArch2AMI",
{
"Ref": "AWS::Region"
},
{
"Fn::FindInMap": [
"AWSInstanceType2Arch",
{
"Ref": "InstanceType"
},
"Arch"
]
}
]
},
"InstanceType": {
"Ref": "InstanceType"
},
"SecurityGroupIds": [
{
"Ref" : "ElasticsearchSecurityGroupId"
}
],
"KeyName": {
"Ref": "KeyName"
},
"SubnetId": {
"Ref": "ElasticsearchSubnetId"
},
"IamInstanceProfile": "MonitorRole",
"Monitoring" : "true",
"Tags": [
{
"Key": "Stack",
"Value": "Production"
},
{
"Key": "Name",
"Value": { "Fn::Join" : ["", [
"Elasticsearch-", { "Ref" : "ElasticsearchNumber" }
]]}
}
],
"UserData": { "Fn::Base64" : { "Fn::Join" : ["", [
"#!/bin/bash -xe\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" : "WaitHandleElasticSearch" }, "'\n",
" exit 1\n",
"}\n",
"/opt/aws/bin/cfn-init -v ",
" --stack ", { "Ref" : "AWS::StackName" },
" --resource ElasticsearchServer ",
" --configsets Install ",
" --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/sdh ]; do echo waiting for /dev/sdh1 to attach; sleep 10; done\n",
"mkfs -t ext4 /dev/sdh\n",
"mkdir /data\n",
"mount /dev/sdh /data\n",
"chown -R elasticsearch:elasticsearch /data > /tmp/chown_data.log 2>&1\n",
"chmod 755 /data > /tmp/chmod_data.log 2>&1\n",
"echo \"/dev/sdh /data ext4 defaults,nofail 0 2\" >> /etc/fstab\n",
"## Allow 60% of total mem as heap size for cluster \n",
"freemem=$(free -m | grep Mem | awk '{ printf \"%1.0f\\n\", 0.6*$2; }')",
" && sed -i \"s/#ES_HEAP_SIZE=.*/ES_HEAP_SIZE=$(echo $freemem)m/g\" /etc/sysconfig/elasticsearch",
" || error_exit \"Failed to change heap size for es \" \n",
"## Start Elasticsearch\n",
"/etc/init.d/elasticsearch start > /tmp/elasticsearch-start.log 2>&1\n",
"## Ensure Elasticsearch starts on reboot\n",
"chkconfig elasticsearch on\n",
"## CloudFormation signal that setup is complete\n",
"/opt/aws/bin/cfn-signal ",
" --exit-code 0",
" --reason \"ElasticsearchInstance setup complete\" '", { "Ref" : "WaitHandleElasticSearch" }, "'\n"
]]}}
}
},
"ElasticSearchDataVolume" : {
"Type" : "AWS::EC2::Volume",
"Properties" : {
"Size" : { "Ref" : "VolumeSize" },
"AvailabilityZone" : { "Fn::GetAtt" : [ "ElasticsearchServer", "AvailabilityZone" ]}
}
},
"ElasticSearchDataVolumeMount" : {
"Type" : "AWS::EC2::VolumeAttachment",
"Properties" : {
"InstanceId" : { "Ref" : "ElasticsearchServer" },
"VolumeId" : { "Ref" : "ElasticSearchDataVolume" },
"Device" : "/dev/sdh"
}
},
"WaitHandleElasticSearch" : {
"Type" : "AWS::CloudFormation::WaitConditionHandle",
"Properties" : {}
},
"WaitConditionElasticSearch" : {
"Type" : "AWS::CloudFormation::WaitCondition",
"DependsOn" : "ElasticsearchServer",
"Properties" : {
"Handle" : { "Ref" : "WaitHandleElasticSearch" },
"Timeout" : "1200"
}
}
}
}