| 2 | from constructs import Construct |
| 3 | |
| 4 | class EMRClusterStack(Stack): |
| 5 | def __init__( |
| 6 | self, |
| 7 | scope: Construct, |
| 8 | id: str, |
| 9 | s3_log_bucket: str, |
| 10 | s3_script_bucket: str, |
| 11 | spark_script: str, |
| 12 | **kwargs, |
| 13 | ) -> None: |
| 14 | super().__init__(scope, id, **kwargs) |
| 15 | |
| 16 | # VPC |
| 17 | vpc = ec2.Vpc( |
| 18 | self, |
| 19 | "vpc", |
| 20 | nat_gateways=0, |
| 21 | subnet_configuration=[ |
| 22 | ec2.SubnetConfiguration( |
| 23 | name="public", subnet_type=ec2.SubnetType.PUBLIC |
| 24 | ) |
| 25 | ], |
| 26 | ) |
| 27 | |
| 28 | # enable reading scripts from s3 bucket |
| 29 | read_scripts_policy = iam.PolicyStatement( |
| 30 | effect=iam.Effect.ALLOW, |
| 31 | actions=["s3:GetObject",], |
| 32 | resources=[f"arn:aws:s3:::{s3_script_bucket}/*"], |
| 33 | ) |
| 34 | read_scripts_document = iam.PolicyDocument() |
| 35 | read_scripts_document.add_statements(read_scripts_policy) |
| 36 | |
| 37 | # emr service role |
| 38 | emr_service_role = iam.Role( |
| 39 | self, |
| 40 | "emr_service_role", |
| 41 | assumed_by=iam.ServicePrincipal("elasticmapreduce.amazonaws.com"), |
| 42 | managed_policies=[ |
| 43 | iam.ManagedPolicy.from_aws_managed_policy_name( |
| 44 | "service-role/AmazonElasticMapReduceRole" |
| 45 | ) |
| 46 | ], |
| 47 | inline_policies={ |
| 48 | "read_scripts_document": read_scripts_document |
| 49 | }, |
| 50 | ) |
| 51 | |
| 52 | # emr job flow role |
| 53 | emr_job_flow_role = iam.Role( |
| 54 | self, |
| 55 | "emr_job_flow_role", |
| 56 | assumed_by=iam.ServicePrincipal("ec2.amazonaws.com"), |
| 57 | managed_policies=[ |
| 58 | iam.ManagedPolicy.from_aws_managed_policy_name( |
| 59 | "service-role/AmazonElasticMapReduceforEC2Role" |
| 60 | ) |
| 61 | ], |