-
Notifications
You must be signed in to change notification settings - Fork 5
/
glue_job.tf
298 lines (255 loc) · 8.42 KB
/
glue_job.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
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
resource "aws_glue_catalog_database" "catalog_db" {
name = "controlshift_${var.controlshift_environment}"
}
locals {
signatures_s3_path = "s3://agra-data-exports-${var.controlshift_environment}/${var.controlshift_organization_slug}/full/signatures"
signatures_table_index = index(local.parsed_bulk_data_schemas.tables.*.table.name, "signatures")
signatures_table_columns = local.parsed_bulk_data_schemas.tables[local.signatures_table_index].table.columns
}
resource "aws_glue_crawler" "signatures_crawler" {
database_name = aws_glue_catalog_database.catalog_db.name
name = "${var.controlshift_environment}_full_signatures"
role = aws_iam_role.glue_service_role.arn
s3_target {
path = local.signatures_s3_path
}
}
resource "aws_s3_bucket" "glue_resources" {
bucket = var.glue_scripts_bucket_name
}
resource "aws_s3_bucket_acl" "glue_resources" {
bucket = aws_s3_bucket.glue_resources.id
acl = "private"
}
resource "aws_s3_bucket_server_side_encryption_configuration" "glue_resources" {
bucket = aws_s3_bucket.glue_resources.bucket
rule {
apply_server_side_encryption_by_default {
sse_algorithm = "AES256"
}
}
}
locals {
# Unsupported column types read from CSV files: all of these will be read as 'string'
unsupported_input_column_types = [
"boolean",
"character varying.*",
"decimal.*",
"hstore",
"jsonb",
"numeric.*",
"timestamp"
]
# Unsupported columnt types for Redshift: these will be replaced by the mapped type
unsupported_output_column_types = {
"hstore" = "character varying"
"jsonb" = "character varying"
"numeric\\(3,2\\)" = "decimal(3,2)"
"timestamp without time zone" = "timestamp"
}
signatures_script = templatefile("${path.module}/templates/signatures_job.py.tftpl", {
catalog_database_name = aws_glue_catalog_database.catalog_db.name
unsupported_input_column_types = local.unsupported_input_column_types
unsupported_output_column_types = local.unsupported_output_column_types
redshift_database_name = var.redshift_database_name
redshift_schema = var.redshift_schema
redshift_connection_name = aws_glue_connection.redshift_connection.name
signatures_table_columns = local.signatures_table_columns
})
}
resource "aws_s3_object" "signatures_script" {
bucket = aws_s3_bucket.glue_resources.id
key = "${var.controlshift_environment}/signatures_job.py"
acl = "private"
content = local.signatures_script
}
resource "aws_iam_role" "glue_service_role" {
name = "AWSGlueServiceRole"
description = "Used by the AWS Glue jobs to insert data into redshift"
assume_role_policy = data.aws_iam_policy_document.glue_assume_role.json
}
data "aws_iam_policy_document" "glue_assume_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["glue.amazonaws.com"]
}
}
}
resource "aws_iam_role_policy_attachment" "glue_resources" {
role = aws_iam_role.glue_service_role.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSGlueServiceRole"
}
resource "aws_iam_role_policy" "controlshift_data_export_bucket_access" {
name = "AllowsCrossAccountAccessToControlShiftDataExportBucket"
role = aws_iam_role.glue_service_role.id
policy = data.aws_iam_policy_document.controlshift_data_export_bucket.json
}
resource "aws_iam_role_policy" "controlshift_glue_scripts_bucket_access" {
name = "AllowsAccessToGlueScriptsBucket"
role = aws_iam_role.glue_service_role.id
policy = data.aws_iam_policy_document.controlshift_glue_scripts_bucket.json
}
data "aws_iam_policy_document" "controlshift_data_export_bucket" {
statement {
effect = "Allow"
actions = [
"s3:GetObject",
"s3:GetBucketLocation",
"s3:ListBucket"
]
resources = [
"arn:aws:s3:::agra-data-exports-${var.controlshift_environment}/${var.controlshift_organization_slug}/*"
]
}
}
data "aws_iam_policy_document" "controlshift_glue_scripts_bucket" {
statement {
effect = "Allow"
actions = [
"s3:GetObject",
"s3:GetBucketLocation",
"s3:ListBucket",
"s3:PutObject",
"s3:DeleteObject"
]
resources = [
"arn:aws:s3:::${var.glue_scripts_bucket_name}/*",
"arn:aws:s3:::${var.glue_scripts_bucket_name}"
]
}
}
resource "aws_glue_connection" "redshift_connection" {
name = "controlshift_${var.controlshift_environment}_data_sync"
connection_properties = {
JDBC_CONNECTION_URL = "jdbc:redshift://${data.aws_redshift_cluster.sync_data_target.endpoint}:${data.aws_redshift_cluster.sync_data_target.port}/${var.redshift_database_name}"
PASSWORD = var.redshift_password
USERNAME = var.redshift_username
JDBC_ENFORCE_SSL = false
}
physical_connection_requirements {
availability_zone = var.glue_physical_connection_requirements.availability_zone
security_group_id_list = var.glue_physical_connection_requirements.security_group_id_list
subnet_id = var.glue_physical_connection_requirements.subnet_id
}
}
resource "aws_glue_job" "signatures_full" {
name = "cs-${var.controlshift_environment}-signatures-full"
connections = [ aws_glue_connection.redshift_connection.name ]
glue_version = "3.0"
default_arguments = {
"--TempDir": "s3://${aws_s3_bucket.glue_resources.bucket}/${var.controlshift_environment}/temp",
"--job-bookmark-option": "job-bookmark-disable",
"--job-language": "python"
}
role_arn = aws_iam_role.glue_service_role.arn
command {
script_location = "s3://${aws_s3_bucket.glue_resources.bucket}/${var.controlshift_environment}/signatures_job.py"
python_version = "3"
}
}
resource "aws_sns_topic" "glue_job_success" {
depends_on = [ aws_glue_job.signatures_full ]
name = var.success_topic_name_for_run_glue_job_lambda
policy = data.aws_iam_policy_document.sns_notification_policy_for_successful_run_glue_job.json
}
data "aws_iam_policy_document" "sns_notification_policy_for_successful_run_glue_job" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = [ "events.amazonaws.com" ]
}
actions = [
"SNS:Publish"
]
resources = [
"arn:aws:sns:*:*:${var.success_topic_name_for_run_glue_job_lambda}"
]
}
}
resource "aws_cloudwatch_event_rule" "successful_glue_job_run" {
name = "successful-glue-job-run"
description = "Glue Job finished successfully"
event_pattern = <<PATTERN
{
"source": [
"aws.glue"
],
"detail-type": [
"Glue Job State Change"
],
"detail": {
"state": [
"SUCCEEDED"
]
}
}
PATTERN
}
resource "aws_cloudwatch_event_target" "notify_successful_glue_job" {
rule = aws_cloudwatch_event_rule.successful_glue_job_run.name
target_id = "notify-successful-glue-job-run"
arn = aws_sns_topic.glue_job_success.arn
}
resource "aws_sns_topic" "glue_job_failure" {
depends_on = [ aws_glue_job.signatures_full ]
name = var.failure_topic_name_for_run_glue_job_lambda
policy = data.aws_iam_policy_document.sns_notification_policy_for_failed_run_glue_job.json
}
data "aws_iam_policy_document" "sns_notification_policy_for_failed_run_glue_job" {
statement {
effect = "Allow"
principals {
type = "Service"
identifiers = [ "events.amazonaws.com" ]
}
actions = [
"SNS:Publish"
]
resources = [
"arn:aws:sns:*:*:${var.failure_topic_name_for_run_glue_job_lambda}"
]
}
}
resource "aws_cloudwatch_event_rule" "failed_glue_job_run" {
name = "failed-glue-job-run"
description = "Glue Job finished with failure"
event_pattern = <<PATTERN
{
"source": [
"aws.glue"
],
"detail-type": [
"Glue Job State Change"
],
"detail": {
"state": [
"FAILED", "TIMEOUT"
]
}
}
PATTERN
}
resource "aws_cloudwatch_event_target" "notify_failed_glue_job" {
rule = aws_cloudwatch_event_rule.failed_glue_job_run.name
target_id = "notify-failed-glue-job-run"
arn = aws_sns_topic.glue_job_failure.arn
}
data "aws_vpc" "main" {
id = var.vpc_id
}
data "aws_route_tables" "all_route_tables" {
vpc_id = data.aws_vpc.main.id
}
# Glue jobs require a VPC endpoint for connecting to S3
resource "aws_vpc_endpoint" "s3" {
vpc_id = data.aws_vpc.main.id
service_name = "com.amazonaws.${var.aws_region}.s3"
}
resource "aws_vpc_endpoint_route_table_association" "s3" {
count = length(data.aws_route_tables.all_route_tables.ids)
route_table_id = tolist(data.aws_route_tables.all_route_tables.ids)[count.index]
vpc_endpoint_id = aws_vpc_endpoint.s3.id
}