Create an AWS Lambda function that resizes images uploaded to Amazon S3.
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Image resizing service (uksb-1tthgi812) (tag:s3-lambda-resizing-python)
Parameters:
SourceBucketName:
Type: String
# Pass your bucket name dynamically or change it here
Default: my-source-bucket-name
DestinationBucketName:
Type: String
# Pass your bucket name dynamically or change it here
Default: my-destination-bucket-name
Resources:
## S3 bucket
SourceBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref SourceBucketName
DestinationBucket:
Type: AWS::S3::Bucket
Properties:
BucketName: !Ref DestinationBucketName
## Lambda function
ResizerFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/
Handler: app.lambda_handler
Runtime: python3.12
MemorySize: 2048
Layers:
- !Sub 'arn:aws:lambda:${AWS::Region}:175033217214:layer:graphicsmagick:2'
Policies:
- S3ReadPolicy:
BucketName: !Ref SourceBucketName
- S3CrudPolicy:
BucketName: !Ref DestinationBucketName
Environment:
Variables:
DESTINATION_BUCKETNAME: !Ref DestinationBucketName
Events:
FileUpload:
Type: S3
Properties:
Bucket: !Ref SourceBucket
Events: s3:ObjectCreated:*
Filter:
S3Key:
Rules:
- Name: suffix
Value: '.jpeg'
Outputs:
SourceBucketName:
Value: !Ref SourceBucketName
Description: S3 Bucket for object storage
DestinationBucketName:
Value: !Ref DestinationBucketName
Description: S3 destination Bucket for object storage
FunctionArn:
Value: !Ref ResizerFunction
Description: ResizerFunction function Arn
Visit the GitHub repo for this pattern.
git clone https://github.com/aws-samples/serverless-patterns/ cd serverless-patterns/s3-lambda-resizing-python
sam deploy --guided
sam delete
.