-
Notifications
You must be signed in to change notification settings - Fork 3
/
glue_stack.py
378 lines (357 loc) · 16.3 KB
/
glue_stack.py
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
# Copyright 2021 Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: MIT-0
import aws_cdk as cdk
from constructs import Construct
import aws_cdk.aws_s3 as s3
import aws_cdk.aws_ec2 as ec2
import aws_cdk.aws_glue as glue
import aws_cdk.aws_iam as iam
import aws_cdk.aws_kms as kms
import aws_cdk.aws_s3_deployment as s3_deployment
from .configuration import (
AVAILABILITY_ZONE_1, SUBNET_ID_1,
S3_ACCESS_LOG_BUCKET, S3_KMS_KEY, S3_CONFORMED_BUCKET, S3_PURPOSE_BUILT_BUCKET, SHARED_SECURITY_GROUP_ID,
ACCOUNT_ID, REGION, get_environment_configuration, get_logical_id_prefix, get_resource_name_prefix
)
class GlueStack(cdk.Stack):
def __init__(
self,
scope: Construct,
construct_id: str,
target_environment: str,
**kwargs
) -> None:
"""
CloudFormation stack to create Glue Jobs, Connections,
Script Bucket, Temporary Bucket, and an IAM Role for permissions.
@param scope cdk.Construct: Parent of this stack, usually an App or a Stage, but could be any construct.
@param construct_id str:
The construct ID of this stack. If stackName is not explicitly defined,
this id (and any parent IDs) will be used to determine the physical ID of the stack.
@param target_environment str: The target environment for stacks in the deploy stage
@param kwargs:
"""
super().__init__(scope, construct_id, **kwargs)
self.mappings = get_environment_configuration(target_environment)
logical_id_prefix = get_logical_id_prefix()
resource_name_prefix = get_resource_name_prefix()
existing_access_logs_bucket_name = cdk.Fn.import_value(self.mappings[S3_ACCESS_LOG_BUCKET])
access_logs_bucket = s3.Bucket.from_bucket_attributes(
self,
'ImportedBucket',
bucket_name=existing_access_logs_bucket_name
)
s3_kms_key_parameter = cdk.Fn.import_value(self.mappings[S3_KMS_KEY])
s3_kms_key = kms.Key.from_key_arn(self, 'ImportedKmsKey', s3_kms_key_parameter)
shared_security_group_parameter = cdk.Fn.import_value(self.mappings[SHARED_SECURITY_GROUP_ID])
glue_connection_subnet = cdk.Fn.import_value(self.mappings[SUBNET_ID_1])
glue_connection_availability_zone = cdk.Fn.import_value(self.mappings[AVAILABILITY_ZONE_1])
conformed_bucket_name = cdk.Fn.import_value(self.mappings[S3_CONFORMED_BUCKET])
conformed_bucket = s3.Bucket.from_bucket_name(
self,
id='ImportedConformedBucket',
bucket_name=conformed_bucket_name
)
purposebuilt_bucket_name = cdk.Fn.import_value(self.mappings[S3_PURPOSE_BUILT_BUCKET])
purposebuilt_bucket = s3.Bucket.from_bucket_name(
self,
id='ImportedPurposeBuiltBucket',
bucket_name=purposebuilt_bucket_name
)
shared_security_group = ec2.SecurityGroup.from_security_group_id(
self,
'ImportedSecurityGroup',
shared_security_group_parameter
)
subnet = ec2.Subnet.from_subnet_attributes(
self,
'ImportedSubnet',
subnet_id=glue_connection_subnet,
availability_zone=glue_connection_availability_zone
)
glue_scripts_bucket = self.glue_scripts_bucket(
target_environment,
logical_id_prefix,
resource_name_prefix,
s3_kms_key,
access_logs_bucket
)
glue_scripts_temp_bucket = self.glue_scripts_temporary_bucket(
target_environment,
logical_id_prefix,
resource_name_prefix,
s3_kms_key,
access_logs_bucket
)
glue_role = self.get_role(
target_environment,
logical_id_prefix,
resource_name_prefix,
s3_kms_key,
)
job_connection = glue.CfnConnection(
self,
f'{target_environment}{logical_id_prefix}RawToConformedWorkflowConnection',
catalog_id=self.account,
connection_input=glue.CfnConnection.ConnectionInputProperty(
connection_type="NETWORK",
name=f'{target_environment.lower()}-{resource_name_prefix}-raw-to-conformed-connection-two',
physical_connection_requirements=glue.CfnConnection.PhysicalConnectionRequirementsProperty(
availability_zone=subnet.availability_zone,
security_group_id_list=[shared_security_group.security_group_id],
subnet_id=subnet.subnet_id
)
)
)
self.raw_to_conformed_job = glue.CfnJob(
self,
f'{target_environment}{logical_id_prefix}RawToConformedJob',
name=f'{target_environment.lower()}-{resource_name_prefix}-raw-to-conformed-job',
command=glue.CfnJob.JobCommandProperty(
name='glueetl',
python_version='3',
script_location=f's3://{glue_scripts_bucket.bucket_name}/etl/etl_raw_to_conformed.py'
),
connections=glue.CfnJob.ConnectionsListProperty(
connections=[job_connection.connection_input.name],
),
default_arguments={
'--enable-glue-datacatalog': True,
'--target_database_name': 'lmd_datalake_arg',
'--target_bucket': conformed_bucket.bucket_name,
'--target_table_name': 'lmd_datalake_raw',
'--TempDir': f's3://{glue_scripts_temp_bucket.bucket_name}/etl/raw-to-conformed',
},
execution_property=glue.CfnJob.ExecutionPropertyProperty(
max_concurrent_runs=10,
),
glue_version='3.0',
max_retries=2,
number_of_workers=2,
role=glue_role.role_arn,
# job_run_queuing_enabled=True,
worker_type='Standard',
timeout=60
)
self.conformed_to_purpose_built_job = glue.CfnJob(
self,
f'{target_environment}{logical_id_prefix}ConformedToPurposeBuiltJob',
name=f'{target_environment.lower()}-{resource_name_prefix}-conformed-to-purpose-built-job',
command=glue.CfnJob.JobCommandProperty(
name='glueetl',
python_version='3',
script_location=f's3://{glue_scripts_bucket.bucket_name}/etl/etl_conformed_to_purposebuilt.py'
),
connections=glue.CfnJob.ConnectionsListProperty(
connections=[job_connection.connection_input.name],
),
default_arguments={
'--enable-glue-datacatalog': True,
'--target_database_name': 'lmd_datalake_conformed_arg',
'--target_bucketname': purposebuilt_bucket.bucket_name,
'--target_table_name': 'lmd_datalake_conformed',
'--txn_bucket_name': glue_scripts_bucket.bucket_name,
'--txn_sql_prefix_path': '/etl/transformation-sql/',
'--TempDir': f's3://{glue_scripts_temp_bucket.bucket_name}/etl/conformed-to-purpose-built'
},
execution_property=glue.CfnJob.ExecutionPropertyProperty(
max_concurrent_runs=10,
),
glue_version='3.0',
max_retries=2,
number_of_workers=2,
role=glue_role.role_arn,
# job_run_queuing_enabled=True,
worker_type='Standard',
timeout=60
)
self.conformed_to_redshift_job = glue.CfnJob(
self,
f'{target_environment}{logical_id_prefix}ConformedToRedshiftJob',
name=f'{target_environment.lower()}-{resource_name_prefix}-conformed-to-redshift-job',
description="Glue Job to ingest PARQUET file data from S3 to RedshiftServerless",
role=glue_role.role_arn,
glue_version="3.0",
command=glue.CfnJob.JobCommandProperty(
name="glueetl",
script_location=f"s3://{glue_scripts_bucket.bucket_name}/etl/etl_conformed_to_redshift.py",
python_version="3"
),
default_arguments={
"--enable-metrics": True,
"--enable-continuous-cloudwatch-log": True,
"--job-bookmark-option": "job-bookmark-enable",
'--TempDir': f's3://{glue_scripts_temp_bucket.bucket_name}/etl/conformed-to-redshift',
'--enable-spark-ui': True,
'--enable-job-insights': True,
'--enable-glue-datacatalog': True,
'--job-language': 'python',
'--additional-python-modules': 'botocore>=1.29.147,boto3>=1.26.147',
'--workgroup_name': f"{target_environment}-lmd-v2".lower(),
'--region': self.mappings[REGION],
'--account_id': self.mappings[ACCOUNT_ID]
},
max_retries=0,
worker_type='Standard',
number_of_workers=4,
# job_run_queuing_enabled=True,
execution_property=glue.CfnJob.ExecutionPropertyProperty(
max_concurrent_runs=1),
timeout=60
)
def glue_scripts_bucket(
self,
target_environment,
logical_id_prefix: str,
resource_name_prefix: str,
s3_kms_key: kms.Key,
access_logs_bucket: s3.Bucket
) -> s3.Bucket:
"""
Creates S3 Bucket that contains glue scripts used in Job execution
@param target_environment str: The target environment for stacks in the deploy stage
@param logical_id_prefix str: The logical id prefix to apply to all CloudFormation resources
@param resource_name_prefix str: The prefix applied to all resource names
@param s3_kms_key kms.Key: The KMS Key to use for encryption of data at rest
@param access_logs_bucket s3.Bucket: The access logs target for this bucket
"""
bucket_name = f'{target_environment.lower()}-{resource_name_prefix}-{self.account}-etl-scripts'
bucket = s3.Bucket(
self,
f'{target_environment}{logical_id_prefix}RawGlueScriptsBucket',
bucket_name=bucket_name,
access_control=s3.BucketAccessControl.PRIVATE,
block_public_access=s3.BlockPublicAccess.BLOCK_ALL,
bucket_key_enabled=s3_kms_key is not None,
encryption=s3.BucketEncryption.KMS,
encryption_key=s3_kms_key,
public_read_access=False,
removal_policy=cdk.RemovalPolicy.DESTROY,
versioned=True,
object_ownership=s3.ObjectOwnership.OBJECT_WRITER,
server_access_logs_bucket=access_logs_bucket,
server_access_logs_prefix=bucket_name,
)
# Dynamically upload resources to the script target
s3_deployment.BucketDeployment(
self,
'DeployGlueJobScript',
# This path is relative to the root of the project
sources=[s3_deployment.Source.asset('./lib/glue_scripts')],
destination_bucket=bucket,
destination_key_prefix='etl',
)
return bucket
def glue_scripts_temporary_bucket(
self, target_environment, logical_id_prefix: str, resource_name_prefix: str,
s3_kms_key: kms.Key, access_logs_bucket: s3.Bucket
) -> s3.Bucket:
"""
Creates S3 Bucket used as a temporary file store in Job execution
@param target_environment str: The target environment for stacks in the deploy stage
@param logical_id_prefix str: The logical id prefix to apply to all CloudFormation resources
@param resource_name_prefix str: The prefix applied to all resource names
@param s3_kms_key kms.Key: The KMS Key to use for encryption of data at rest
@param access_logs_bucket s3.Bucket: The access logs target for this bucket
"""
bucket_name = f'{target_environment.lower()}-{resource_name_prefix}-{self.account}-glue-temporary-scripts'
bucket = s3.Bucket(
self,
f'{target_environment}{logical_id_prefix}RawGlueScriptsTemporaryBucket',
bucket_name=bucket_name,
access_control=s3.BucketAccessControl.PRIVATE,
block_public_access=s3.BlockPublicAccess.BLOCK_ALL,
bucket_key_enabled=s3_kms_key is not None,
encryption=s3.BucketEncryption.KMS if s3_kms_key else s3.BucketEncryption.S3_MANAGED,
encryption_key=s3_kms_key if s3_kms_key else None,
public_read_access=False,
removal_policy=cdk.RemovalPolicy.DESTROY,
versioned=True,
object_ownership=s3.ObjectOwnership.OBJECT_WRITER,
server_access_logs_bucket=access_logs_bucket,
server_access_logs_prefix=bucket_name,
)
return bucket
def get_role(
self,
target_environment: str,
logical_id_prefix: str,
resource_name_prefix: str,
s3_kms_key: kms.Key,
) -> iam.Role:
"""
Creates the role used during Glue Job execution
@param target_environment str: The target environment for stacks in the deploy stage
@param logical_id_prefix str: The logical id prefix to apply to all CloudFormation resources
@param resource_name_prefix str: The prefix applied to all resource names
@param s3_kms_key kms.Key: The KMS Key to provide permissions to
@returns iam.Role: The role that was created
"""
return iam.Role(
self,
f'{target_environment}{logical_id_prefix}RawGlueRole',
role_name=f'{target_environment.lower()}-{resource_name_prefix}-raw-glue-role',
assumed_by=iam.ServicePrincipal('glue.amazonaws.com'),
inline_policies={
'AllowS3GetActions': iam.PolicyDocument(
statements=[
iam.PolicyStatement(
effect=iam.Effect.ALLOW,
actions=[
's3:ListBucketVersions',
's3:ListBucket',
's3:GetBucketNotification',
's3:GetBucketLocation',
],
resources=[
'arn:aws:s3:::*'
]
)
]),
'AllowS3PutActions': iam.PolicyDocument(
statements=[
iam.PolicyStatement(
effect=iam.Effect.ALLOW,
actions=[
's3:ReplicationObject',
's3:PutObject',
's3:GetObject',
's3:DeleteObject',
],
resources=[
'arn:aws:s3:::*/*'
]
)
]),
'AllowS3ListAllActions': iam.PolicyDocument(
statements=[
iam.PolicyStatement(
effect=iam.Effect.ALLOW,
actions=[
's3:ListAllMyBuckets',
],
resources=[
'*'
]
)
]),
'AllowKMSActions': iam.PolicyDocument(
statements=[
iam.PolicyStatement(
effect=iam.Effect.ALLOW,
actions=[
'kms:*',
],
resources=[
s3_kms_key.key_arn,
]
)
]),
},
managed_policies=[
iam.ManagedPolicy.from_aws_managed_policy_name('service-role/AWSGlueServiceRole'),
iam.ManagedPolicy.from_aws_managed_policy_name("AmazonRedshiftAllCommandsFullAccess"),
iam.ManagedPolicy.from_aws_managed_policy_name("SecretsManagerReadWrite"),
iam.ManagedPolicy.from_aws_managed_policy_name("AmazonRedshiftFullAccess")
]
)