Skip to content

Instantly share code, notes, and snippets.

@piotrekwitkowski
Created July 31, 2024 10:12
Show Gist options
  • Save piotrekwitkowski/6e66ef415e3593075f6f5a3ce339a749 to your computer and use it in GitHub Desktop.
Save piotrekwitkowski/6e66ef415e3593075f6f5a3ce339a749 to your computer and use it in GitHub Desktop.
Lambda function in a CloudFormation template to copy an asset from one bucket to another
S3AssetCopyFunction:
Type: AWS::Lambda::Function
Properties:
FunctionName: S3AssetCopy
Handler: index.handler
Role: !GetAtt LambdaExecutionRole.Arn
MemorySize: 4096
Runtime: python3.12
Timeout: 30
Code:
ZipFile: |
import boto3
import cfnresponse
def handler(event, context):
try:
rp = event['ResourceProperties']
boto3.resource('s3').Object(rp['DESTINATION_BUCKET'], rp['DESTINATION_KEY']).copy_from(CopySource=rp['SOURCE_BUCKET'] + '/' + rp['SOURCE_KEY'])
cfnresponse.send(event, context, cfnresponse.SUCCESS, {})
except Exception as e:
cfnresponse.send(event, context, cfnresponse.FAILED, {})
UploadHTMLSampleStaticWebsite:
Type: Custom::S3AssetCopyFunction
Properties:
ServiceToken: !GetAtt S3AssetCopyFunction.Arn
SOURCE_BUCKET: !Ref AssetsBucketName
SOURCE_KEY: !Sub ${AssetsBucketPrefix}file.html
DESTINATION_BUCKET: !Ref ParticipantAssetsBucket
DESTINATION_KEY: file.html
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment