-
Notifications
You must be signed in to change notification settings - Fork 1
/
emr.tf
266 lines (226 loc) · 6.55 KB
/
emr.tf
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
#CREATE VPC
resource "aws_vpc" "emr_vpc" {
cidr_block = "10.123.0.0/16"
enable_dns_hostnames = true
enable_dns_support = true
tags = {
Name = "emr_vpc"
}
}
# CREATE A PUBLIC SUBNET IN THE VPC
resource "aws_subnet" "public_subnet" {
vpc_id = aws_vpc.emr_vpc.id
cidr_block = "10.123.1.0/24"
availability_zone = "us-east-1a"
map_public_ip_on_launch = true
tags = {
Name = "emr_public_subnet"
}
}
#CREATE AN INTERNET GATEWAY
resource "aws_internet_gateway" "my_gateway" {
vpc_id = aws_vpc.emr_vpc.id
tags = {
Name = "my_gateway"
}
}
# CREATE A ROUTE TABLE
resource "aws_route_table" "public_routing_table" {
vpc_id = aws_vpc.emr_vpc.id
tags = {
Name = "public_routing_table"
}
}
# CREATE A ROUTE
resource "aws_route" "default_route" {
route_table_id = aws_route_table.public_routing_table.id
destination_cidr_block = "0.0.0.0/0"
gateway_id = aws_internet_gateway.my_gateway.id
}
#CREATE ROUTE TABLE ASSOCIATION
resource "aws_route_table_association" "public_assoc" {
subnet_id = aws_subnet.public_subnet.id
route_table_id = aws_route_table.public_routing_table.id
}
#CREATE AN IAM ROLE FOR THE CLUSTER
resource "aws_iam_role" "emr_service_role" {
name = "emr_service_role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "elasticmapreduce.amazonaws.com"
}
},
]
})
}
#CREATE IAM ROLE FOR THE EMR PROFILE
resource "aws_iam_role" "emr_profile_role" {
name = "emr_profile_role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "ec2.amazonaws.com"
}
},
]
})
}
#ATTACH THE POLICY TO THE EMR CLUSTER/EMR SERVICE ROLE
resource "aws_iam_role_policy_attachment" "attach_emr_policy" {
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceRole"
role = aws_iam_role.emr_service_role.name
}
#ATTACH THE POLICY TO THE EMR CLUSTER/EMR PROFILE ROLE
resource "aws_iam_role_policy_attachment" "attach_emr_profile_policy" {
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonElasticMapReduceforEC2Role"
role = aws_iam_role.emr_profile_role.name
}
# CREATE THE INSTANCE PROFILE TO PASS THE ROLE DETAILS TO THE EC2 INSTANCES
resource "aws_iam_instance_profile" "emr_profile" {
name = "spark_cluster_emr_profile"
role = aws_iam_role.emr_profile_role.name
}
#CREATE SECURITY GROUP FOR MASTER NODE
resource "aws_security_group" "master_security_group" {
name = "master_security_group"
description = "Allow inbound traffic from VPN"
vpc_id = aws_vpc.emr_vpc.id
# Avoid circular dependencies stopping the destruction of the cluster
revoke_rules_on_delete = true
# Allow communication between nodes in the VPC
ingress {
from_port = "0"
to_port = "0"
protocol = "-1"
self = true
}
ingress {
from_port = "8443"
to_port = "8443"
protocol = "TCP"
}
egress {
from_port = "0"
to_port = "0"
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
# Allow SSH traffic from VPN
ingress {
from_port = 22
to_port = 22
protocol = "TCP"
cidr_blocks = ["${chomp(data.http.my_local_ip.body)}/32"]
}
#### Expose web interfaces to VPN
# Spark History
ingress {
from_port = 18080
to_port = 18080
protocol = "TCP"
cidr_blocks = ["123.123.0.0/16"]
}
# Spark UI
ingress {
from_port = 4040
to_port = 4040
protocol = "TCP"
cidr_blocks = ["123.123.0.0/16"]
}
tags = {
name = "emr_master_security_group"
}
}
# CREATE SECURITY GROUP FOR CORE NODE
resource "aws_security_group" "slave_security_group" {
name = "slave_security_group"
description = "Allow all internal traffic"
vpc_id = aws_vpc.emr_vpc.id
revoke_rules_on_delete = true
# Allow communication between nodes in the VPC
ingress {
from_port = "0"
to_port = "0"
protocol = "-1"
self = true
}
ingress {
from_port = "8443"
to_port = "8443"
protocol = "TCP"
}
egress {
from_port = "0"
to_port = "0"
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
# Allow SSH traffic from VPN
ingress {
from_port = 22
to_port = 22
protocol = "TCP"
cidr_blocks = ["${chomp(data.http.my_local_ip.body)}/32"]
}
tags = {
name = "emr_slave_security_group"
}
}
#CREATE THE EMR CLUSTER
resource "aws_emr_cluster" "emr_cluster" {
name = var.emr_name
release_label = var.release_label
applications = var.applications
termination_protection = false
service_role = aws_iam_role.emr_service_role.arn
ec2_attributes {
key_name = aws_key_pair.mtc_auth.id
subnet_id = aws_subnet.public_subnet.id
emr_managed_master_security_group = aws_security_group.master_security_group.id
emr_managed_slave_security_group = aws_security_group.slave_security_group.id
instance_profile = aws_iam_instance_profile.emr_profile.arn
}
master_instance_group {
name = var.master_instance_group_name
instance_type = var.master_instance_group_instance_type
instance_count = var.master_instance_group_instance_count
ebs_config {
size = var.master_instance_group_ebs_size
type = var.master_instance_group_ebs_type
volumes_per_instance = var.master_instance_group_ebs_volumes_per_instance
}
}
core_instance_group {
name = var.core_instance_group_name
instance_type = var.core_instance_group_instance_type
instance_count = var.core_instance_group_instance_count
ebs_config {
size = var.core_instance_group_ebs_size
type = var.core_instance_group_ebs_type
volumes_per_instance = var.core_instance_group_ebs_volumes_per_instance
}
}
tags = {
Name = var.emr_name
Project = "emr_spark_job"
Environment = "dev"
role = "EMR_DefaultRole"
}
depends_on = [
aws_iam_role_policy_attachment.attach_emr_profile_policy,
aws_iam_role_policy_attachment.attach_emr_policy,
aws_iam_instance_profile.emr_profile,
null_resource.provisioner_resource
]
}