-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.tf
234 lines (192 loc) · 6.05 KB
/
main.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
# CREATE S3 BUCKET
resource "aws_s3_bucket" "etl_bucket" {
bucket = var.bucket_name
force_destroy = true
tags = {
Name = "etl_bucket"
}
}
# ADD ACL TO THE S3 BUCKET
resource "aws_s3_bucket_acl" "awsbucketeer_bucket_acl" {
bucket = aws_s3_bucket.etl_bucket.bucket
acl = "public-read-write"
}
# CREATE AWS IAM ROLE FOR EC2_S3_ACCESS
resource "aws_iam_role" "ec2_s3_role" {
name = "ec2_s3_role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "ec2.amazonaws.com"
}
},
]
})
}
# CREATE AWS IAM ROLE FOR REDSHIFT
resource "aws_iam_role" "redshift_role" {
name = "redshift_role"
assume_role_policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = "sts:AssumeRole"
Effect = "Allow"
Sid = ""
Principal = {
Service = "redshift.amazonaws.com"
}
},
]
})
}
# ATTACH REQUIRED POLICIES TO THE EC2 IAM ROLE
resource "aws_iam_role_policy_attachment" "attach_policy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonS3FullAccess"
role = aws_iam_role.ec2_s3_role.name
}
resource "aws_iam_role_policy_attachment" "attach_policy_2" {
policy_arn = "arn:aws:iam::aws:policy/AmazonRedshiftAllCommandsFullAccess"
role = aws_iam_role.ec2_s3_role.name
}
resource "aws_iam_role_policy_attachment" "attach_policy_3" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEMRFullAccessPolicy_v2"
role = aws_iam_role.ec2_s3_role.name
}
# CREATE INSTANCE PROFILE AND ATTACH IAM ROLE TO IT
resource "aws_iam_instance_profile" "ec2_profile" {
name = "ec2_profile"
role = aws_iam_role.ec2_s3_role.name
}
# ATTACH REQUIRED POLICIES TO THE REDSHIFT IAM ROLE
resource "aws_iam_role_policy_attachment" "attach_policy_4" {
policy_arn = "arn:aws:iam::aws:policy/AmazonS3ReadOnlyAccess"
role = aws_iam_role.redshift_role.name
}
resource "aws_iam_role_policy_attachment" "attach_policy_5" {
policy_arn = "arn:aws:iam::aws:policy/AWSGlueConsoleFullAccess"
role = aws_iam_role.redshift_role.name
}
# CREATE KEY PAIR
resource "aws_key_pair" "mtc_auth" {
key_name = "mtc_key"
public_key = file("~/.ssh/mtckey.pub")
}
# CREATE SECURITY GROUP
resource "aws_security_group" "mtc_sg" {
name = "dev_sg"
description = "dev security group"
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["${chomp(data.http.my_local_ip.body)}/32"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
#CREATE REDSHIFT SECURITY GROUP
resource "aws_security_group" "redshift_security_group" {
name = "redshift_sg"
description = "redshift security group"
ingress {
from_port = 5439
to_port = 5439
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
# CREATE AWS INSTANCE
resource "aws_instance" "dev_node" {
instance_type = "t2.medium"
iam_instance_profile = aws_iam_instance_profile.ec2_profile.name
ami = data.aws_ami.server_ami.id
vpc_security_group_ids = [aws_security_group.mtc_sg.id]
key_name = aws_key_pair.mtc_auth.id
user_data = file("user-data.tpl")
root_block_device {
volume_size = 20
}
tags = {
Name = "dev-node"
}
}
# CREATE TIME_SLEEP RESOURCE TO LET THE EC2 INSTANCE FINISH INITIALIZING
resource "time_sleep" "wait_180_seconds" {
depends_on = [aws_instance.dev_node]
create_duration = "180s"
}
# CREATE NULL RESOURCE TO EXECUTE BASH SCRIPT
resource "null_resource" "provisioner_resource" {
depends_on = [time_sleep.wait_180_seconds]
provisioner "local-exec" {
command = templatefile("${var.host_os}-ssh-config.tpl", {
hostname = aws_instance.dev_node.public_ip,
user = "ubuntu",
identityfile = "~/.ssh/mtckey"
ec2_dns = aws_instance.dev_node.public_dns
account_id = data.aws_caller_identity.current.account_id
iam_role_name = var.redshift_role_name
bucket_name = var.bucket_name
})
interpreter = var.host_os == "bash" ? ["bash", "-c"] : ["powershell", "-command"]
}
}
# CREATE REDSHIFT NULL RESOURCE
resource "null_resource" "redshift_provisioner_resource" {
provisioner "local-exec" {
command = templatefile("redshift-ssh-config.tpl", {
identityfile = "~/.ssh/mtckey"
ec2_dns = aws_instance.dev_node.public_dns
redshift_user = var.rs_master_username
redshift_password = var.rs_master_pass
redshift_host = aws_redshift_cluster.redshift_cluster.endpoint
redshift_dns = aws_redshift_cluster.redshift_cluster.dns_name
redshift_database = var.rs_database_name
redshift_port = var.rs_port
bucket_name = var.bucket_name
aws_region = var.aws_region
emr_cluster_id = aws_emr_cluster.emr_cluster.id
depends_on = [
aws_redshift_cluster.redshift_cluster
]
})
interpreter = var.host_os == "bash" ? ["bash", "-c"] : ["powershell", "-command"]
}
}
# CREATE REDSHIFT CLUSTER RESOURCE
resource "aws_redshift_cluster" "redshift_cluster" {
cluster_identifier = var.rs_cluster_identifier
master_username = var.rs_master_username
master_password = var.rs_master_pass
node_type = var.rs_nodetype
cluster_type = var.rs_cluster_type
port = var.rs_port
database_name = var.rs_database_name
skip_final_snapshot = true
publicly_accessible = true
iam_roles = ["${aws_iam_role.redshift_role.arn}"]
vpc_security_group_ids = [aws_security_group.redshift_security_group.id]
depends_on = [
aws_security_group.redshift_security_group,
aws_iam_role.redshift_role,
aws_instance.dev_node,
null_resource.provisioner_resource,
aws_emr_cluster.emr_cluster
]
}