|
| 1 | +AWSTemplateFormatVersion: 2010-09-09 |
| 2 | +Description: Copy S3 object to local S3 bucket |
| 3 | + |
| 4 | +Parameters: |
| 5 | + |
| 6 | + S3BucketSources: |
| 7 | + Type: String |
| 8 | + Description: S3 bucket with source files |
| 9 | + MaxLength: 63 |
| 10 | + MinLength: 3 |
| 11 | + Default: aws-bigdata-blog |
| 12 | + AllowedValues: ["aws-bigdata-blog"] |
| 13 | + S3SourcesPrefix: |
| 14 | + Type: String |
| 15 | + Description: S3 prefix with sources WITH ending slash |
| 16 | + MaxLength: 63 |
| 17 | + MinLength: 3 |
| 18 | + Default: artifacts/aws-blog-emr-ranger |
| 19 | + AllowedValues: ["artifacts/aws-blog-emr-ranger"] |
| 20 | + ProjectVersion: |
| 21 | + Default: 3.0 |
| 22 | + Description: Project version |
| 23 | + Type: String |
| 24 | + AllowedValues: |
| 25 | + - 3.0 |
| 26 | + - beta |
| 27 | + S3Objects: |
| 28 | + Type: CommaDelimitedList |
| 29 | + Description: S3 Object to be copied |
| 30 | + Default: launch-cluster.zip, scripts/download-scripts.sh, scripts/remove-yum-package-name-validator.sh, scripts/configure_ranger_glue_support_with_bootstrap.sh, scripts/enable-glue-catalog-support.sh, scripts/create-hdfs-home-ba.sh, scripts/setup-trino-redshift-connector.sh |
| 31 | + |
| 32 | +Resources: |
| 33 | + |
| 34 | + S3BucketRegionSources: |
| 35 | + Type: AWS::S3::Bucket |
| 36 | + Properties: |
| 37 | + BucketEncryption: |
| 38 | + ServerSideEncryptionConfiguration: |
| 39 | + - ServerSideEncryptionByDefault: |
| 40 | + SSEAlgorithm: AES256 |
| 41 | + DeletionPolicy: Delete |
| 42 | + |
| 43 | + CopyZips: |
| 44 | + Type: AWS::CloudFormation::CustomResource |
| 45 | + DependsOn: |
| 46 | + - S3BucketRegionSources |
| 47 | + Properties: |
| 48 | + ServiceToken: !GetAtt 'CopyZipsFunction.Arn' |
| 49 | + DestBucket: !Ref 'S3BucketRegionSources' |
| 50 | + SourceBucket: !Ref 'S3BucketSources' |
| 51 | + SourcePrefix: !Ref 'S3SourcesPrefix' |
| 52 | + ProjectVersion: !Ref 'ProjectVersion' |
| 53 | + Counter: "1" |
| 54 | + Objects: !Ref S3Objects |
| 55 | + |
| 56 | + CopyZipsRole: |
| 57 | + Type: AWS::IAM::Role |
| 58 | + Properties: |
| 59 | + AssumeRolePolicyDocument: |
| 60 | + Version: '2012-10-17' |
| 61 | + Statement: |
| 62 | + - Effect: Allow |
| 63 | + Principal: |
| 64 | + Service: lambda.amazonaws.com |
| 65 | + Action: sts:AssumeRole |
| 66 | + ManagedPolicyArns: |
| 67 | + - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole |
| 68 | + Path: / |
| 69 | + Policies: |
| 70 | + - PolicyName: lambda-copier |
| 71 | + PolicyDocument: |
| 72 | + Version: '2012-10-17' |
| 73 | + Statement: |
| 74 | + - Effect: Allow |
| 75 | + Action: |
| 76 | + - s3:GetObject |
| 77 | + - s3:GetObjectTagging |
| 78 | + Resource: |
| 79 | + - !Sub 'arn:aws:s3:::${S3BucketSources}/*' |
| 80 | + - Effect: Allow |
| 81 | + Action: |
| 82 | + - s3:ListBucket |
| 83 | + Resource: |
| 84 | + - !Sub 'arn:aws:s3:::${S3BucketSources}' |
| 85 | + - Effect: Allow |
| 86 | + Action: |
| 87 | + - s3:ListBucket |
| 88 | + Resource: |
| 89 | + - !Sub 'arn:aws:s3:::${S3BucketRegionSources}' |
| 90 | + - Effect: Allow |
| 91 | + Action: |
| 92 | + - s3:PutObject |
| 93 | + - s3:DeleteObject |
| 94 | + - s3:PutObjectTagging |
| 95 | + Resource: |
| 96 | + - !Sub 'arn:aws:s3:::${S3BucketRegionSources}/*' |
| 97 | + |
| 98 | + CopyZipsFunction: |
| 99 | + Type: AWS::Lambda::Function |
| 100 | + Properties: |
| 101 | + FunctionName: "CopyRangerArtifacts" |
| 102 | + Description: Copies objects from a source S3 bucket to a destination |
| 103 | + Handler: index.handler |
| 104 | + Runtime: python3.9 |
| 105 | + Role: !GetAtt 'CopyZipsRole.Arn' |
| 106 | + Timeout: 240 |
| 107 | + Code: |
| 108 | + ZipFile: | |
| 109 | + import json |
| 110 | + import logging |
| 111 | + import threading |
| 112 | + import boto3 |
| 113 | + import cfnresponse |
| 114 | + |
| 115 | + def copy_objects(source_bucket, dest_bucket, objects, prefix, project_version): |
| 116 | + s3 = boto3.client('s3') |
| 117 | + for o in objects: |
| 118 | + key = prefix + '/' + project_version + '/' + o |
| 119 | + copy_source = { |
| 120 | + 'Bucket': source_bucket, |
| 121 | + 'Key': key |
| 122 | + } |
| 123 | + print('copy source_bucket:' + source_bucket + ' destination_bucket: '+ dest_bucket + ' key: ' + key) |
| 124 | + s3.copy_object(CopySource=copy_source, Bucket=dest_bucket, Key=key) |
| 125 | + |
| 126 | + def delete_objects(bucket, objects, prefix, project_version): |
| 127 | + s3 = boto3.client('s3') |
| 128 | + objects = {'Objects': [{'Key': prefix + '/' + project_version + '/' + o} for o in objects]} |
| 129 | + s3.delete_objects(Bucket=bucket, Delete=objects) |
| 130 | + s3_list_response = s3.list_objects_v2(Bucket=bucket, Prefix=prefix + '/' + project_version + '/emr-tls/') |
| 131 | + if 'Contents' in s3_list_response: |
| 132 | + for object in s3_list_response['Contents']: |
| 133 | + print('Deleting', object['Key']) |
| 134 | + s3.delete_object(Bucket=bucket, Key=object['Key']) |
| 135 | + |
| 136 | + def timeout(event, context): |
| 137 | + logging.error('Execution is about to time out, sending failure response to CloudFormation') |
| 138 | + cfnresponse.send(event, context, cfnresponse.FAILED, {}, None) |
| 139 | + |
| 140 | + def handler(event, context): |
| 141 | + # make sure we send a failure to CloudFormation if the function is going to timeout |
| 142 | + timer = threading.Timer((context.get_remaining_time_in_millis() / 1000.00) - 0.5, timeout, args=[event, context]) |
| 143 | + timer.start() |
| 144 | + |
| 145 | + print('Received event: %s' % json.dumps(event)) |
| 146 | + status = cfnresponse.SUCCESS |
| 147 | + try: |
| 148 | + source_bucket = event['ResourceProperties']['SourceBucket'] |
| 149 | + source_prefix = event['ResourceProperties']['SourcePrefix'] |
| 150 | + project_version = event['ResourceProperties']['ProjectVersion'] |
| 151 | + dest_bucket = event['ResourceProperties']['DestBucket'] |
| 152 | + if source_bucket == dest_bucket: |
| 153 | + return |
| 154 | + objects = event['ResourceProperties']['Objects'] |
| 155 | + if event['RequestType'] == 'Delete': |
| 156 | + delete_objects(dest_bucket, objects, source_prefix, project_version) |
| 157 | + else: |
| 158 | + copy_objects(source_bucket, dest_bucket, objects, source_prefix, project_version) |
| 159 | + except Exception as e: |
| 160 | + logging.error('Exception: %s' % e, exc_info=True) |
| 161 | + status = cfnresponse.FAILED |
| 162 | + finally: |
| 163 | + timer.cancel() |
| 164 | + cfnresponse.send(event, context, status, {}, None) |
| 165 | + |
| 166 | +Outputs: |
| 167 | + |
| 168 | + RegionalS3Bucket: |
| 169 | + Description: Regional S3 bucket with artifacts required by the EMR cluster. This bucket can be reused as the 'S3Bucket' value for future EMR cluster stacks |
| 170 | + Value: !Ref S3BucketRegionSources |
0 commit comments