Select your cookie preferences

We use cookies and similar tools to enhance your experience, provide our services, deliver relevant advertising, and make improvements. Approved third parties also use these tools to help us deliver advertising and provide certain site features.

Resizing images uploaded to Amazon S3 with AWS Lambda (Python)

Created with SnapAmazon S3AWS LambdaAmazon S3Object Created

Create an AWS Lambda function that resizes images uploaded to Amazon S3.

A Lambda function consumes events from an Amazon S3 bucket. The Lambda code checks the uploaded file is an image, creates a thumbnail version of the image, then uploads the thumbnail to the destination bucket.
This pattern deploys a Lambda function and two S3 buckets.

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

< Back to all patterns


GitHub icon Visit the GitHub repo for this pattern.

Download

git clone https://github.com/aws-samples/serverless-patterns/ cd serverless-patterns/s3-lambda-resizing-python

Deploy

sam deploy --guided


Testing

See the GitHub repo for detailed testing instructions.

Cleanup

Delete the application: sam delete.

Additional resources

Seshu Brahma

Presented by Seshu Brahma

AWS Lambda Software Dev Engineer

Follow on LinkedIn