Module: Enscalator::Plugins::ElasticsearchOpsworks

Includes:
Elb
Defined in:
lib/enscalator/plugins/elasticsearch_opsworks.rb

Overview

Elasticsearch related configuration

Instance Method Summary collapse

Methods included from Elb

#elb_init

Instance Method Details

#elasticsearch_init(app_name, ssh_key:, es_config: {}, os: 'Amazon Linux 2015.09', cookbook: 'https://github.com/en-japan-air/opsworks-elasticsearch-cookbook.git') ⇒ Object

Create Elasticsearch cluster using Opsworks

Parameters:

  • app_name (String)

    application name

  • ssh_key (String)

    name of the ssh key

  • os (String) (defaults to: 'Amazon Linux 2015.09')

    base operating system

  • cookbook (String) (defaults to: 'https://github.com/en-japan-air/opsworks-elasticsearch-cookbook.git')

    chef cookbook



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
# File 'lib/enscalator/plugins/elasticsearch_opsworks.rb', line 13

def elasticsearch_init(app_name,
                       ssh_key:,
                       es_config: {},
                       os: 'Amazon Linux 2015.09',
                       cookbook: 'https://github.com/en-japan-air/opsworks-elasticsearch-cookbook.git')

  parameter "ES#{app_name}ChefCookbook",
            Default: cookbook,
            Description: 'GitURL',
            Type: 'String'

  parameter "ES#{app_name}InstanceDefaultOs",
            Default: os,
            Description: ['The stack\'s default operating system, which is installed',
                          'on every instance unless you specify a different',
                          'operating system when you create the instance.'].join(' '),
            Type: 'String'

  parameter "ES#{app_name}SshKeyName",
            Default: ssh_key,
            Description: 'SSH key name for EC2 instances.',
            Type: 'String'

  resource 'InstanceRole',
           Type: 'AWS::IAM::InstanceProfile',
           Properties: {
             Path: '/',
             Roles: [
               ref('OpsWorksEC2Role')
             ]
           }

  resource 'ServiceRole',
           Type: 'AWS::IAM::Role',
           Properties: {
             AssumeRolePolicyDocument: {
               Statement: [
                 {
                   Effect: 'Allow',
                   Principal: {
                     Service: [
                       'opsworks.amazonaws.com'
                     ]
                   },
                   Action: [
                     'sts:AssumeRole'
                   ]
                 }
               ]
             },
             Path: '/',
             Policies: [
               {
                 PolicyName: "#{app_name}-opsworks-service",
                 PolicyDocument: {
                   Statement: [
                     {
                       Effect: 'Allow',
                       Action: %w( ec2:* iam:PassRole cloudwatch:GetMetricStatistics elasticloadbalancing:* ),
                       Resource: '*'
                     }
                   ]
                 }
               }
             ]
           }

  resource 'OpsWorksEC2Role',
           Type: 'AWS::IAM::Role',
           Properties: {
             AssumeRolePolicyDocument: {
               Statement: [
                 {
                   Effect: 'Allow',
                   Principal: {
                     Service: [
                       'ec2.amazonaws.com'
                     ]
                   },
                   Action: [
                     'sts:AssumeRole'
                   ]
                 }
               ]
             },
             Path: '/',
             Policies: [
               {
                 PolicyName: "#{app_name}-opsworks-ec2-role",
                 PolicyDocument: {
                   Statement: [
                     {
                       Effect: 'Allow',
                       Action: %w(
                         ec2:DescribeInstances
                         ec2:DescribeRegions
                         ec2:DescribeSecurityGroups
                         ec2:DescribeTags
                         cloudwatch:PutMetricData),
                       Resource: '*'
                     }
                   ]
                 }
               }
             ]
           }

  instances_security_group = security_group_vpc("ES#{app_name}", 'so that ES cluster can find other nodes', vpc.id)

  ops_stack_name = "#{app_name}-ES"
  resource 'ESStack',
           Type: 'AWS::OpsWorks::Stack',
           Properties: {
             Name: ops_stack_name,
             VpcId: vpc.id,
             DefaultSubnetId: ref_resource_subnets.first,
             ConfigurationManager: {
               Name: 'Chef',
               Version: '12'
             },
             UseCustomCookbooks: 'true',
             CustomCookbooksSource: {
               Type: 'git',
               Url: ref("ES#{app_name}ChefCookbook")
             },
             DefaultOs: ref("ES#{app_name}InstanceDefaultOs"),
             DefaultRootDeviceType: 'ebs',
             DefaultSshKeyName: ref("ES#{app_name}SshKeyName"),
             CustomJson: {
               java: {
                 jdk_version: '8',
                 oracle: {
                   accept_oracle_download_terms: 'true'
                 },
                 accept_license_agreement: 'true',
                 install_flavor: 'oracle'
               },
               elasticsearch: {
                 plugins: [
                   'analysis-kuromoji',
                   'cloud-aws',
                   { name: 'elasticsearch-head', url: 'mobz/elasticsearch-head' }
                 ],
                 config: {
                   'cluster.name': "#{app_name}-elasticsearch",
                   'path.data': '/mnt/elasticsearch-data',
                   'path.logs': '/mnt/elasticsearch-data/logs/',
                   'network.bind_host': '0.0.0.0',
                   'network.publish_host': '_non_loopback_',
                   'index.routing.allocation.disable_allocation': 'false',
                   'cloud.aws.region': region,
                   discovery: {
                     type: 'ec2',
                     ec2: {
                       groups: [ref(instances_security_group)],
                       tag: {
                         'opsworks:stack': ops_stack_name
                       }
                     }
                   },
                   'cluster.routing.allocation.awareness.attributes': 'rack_id'
                 }.merge(es_config)
               }
             },
             ServiceRoleArn: {
               'Fn::GetAtt': %w(ServiceRole Arn)
             },
             DefaultInstanceProfileArn: {
               'Fn::GetAtt': %w(InstanceRole Arn)
             }
           }

  resource 'ESLayer',
           Type: 'AWS::OpsWorks::Layer',
           Properties: {
             StackId: ref('ESStack'),
             Name: 'Search',
             Type: 'custom',
             Shortname: 'search',
             CustomRecipes: {
               Setup: %w(apt ark java layer-custom::es-opsworks)
             },
             EnableAutoHealing: 'true',
             AutoAssignElasticIps: 'false',
             AutoAssignPublicIps: 'false',
             VolumeConfigurations: [
               {
                 MountPoint: '/mnt/elasticsearch-data',
                 NumberOfDisks: 1,
                 VolumeType: 'gp2',
                 Size: 100
               }
             ],
             CustomSecurityGroupIds: [
               {
                 'Fn::GetAtt': %W(#{instances_security_group} GroupId)
               },
               ref_private_security_group
             ]
           }

  resource 'ESMainInstance',
           Type: 'AWS::OpsWorks::Instance',
           Properties: {
             # EbsOptimized: true,          # Not available for m3.medium
             InstanceType: 'm3.medium',
             LayerIds: [ref('ESLayer')],
             StackId: ref('ESStack')
           }
end