Resizing image Lambda function

In this step, we will create a Lambda function that automatically resizes images after they are uploaded to S3. When a user uploads a book image via the book_create function, this ImageResize function is triggered by an S3 event, resizes the image to the specified dimensions, and stores the result in the BookImageResizeShop bucket for serving to the frontend.

How it works

  1. User uploads an image → stored in BookImageShop bucket
  2. S3 triggers the resize_image Lambda function (s3:ObjectCreated:*)
  3. The function reads the original image, resizes it to 200×280px, and saves the resized version to BookImageResizeShop bucket
  4. The frontend reads book images from the resize bucket

Edit SAM Template

  1. Open the template.yaml file in the fcaj-book-shop folder.

  2. First, add the width and height parameters under the Parameters section. These define the target dimensions for the resized images:

    width:
      Type: String
      Default: 200px
    
    height:
      Type: String
      Default: 280px
    

    Add width and height parameters

  3. Next, add the ImageResize function resource under the Resources section:

    ImageResize:
      Type: AWS::Serverless::Function
      Properties:
        CodeUri: fcaj-book-shop/resize_image/function.zip
        PackageType: Zip
        Handler: index.handler
        Runtime: nodejs22.x
        FunctionName: resize_image
        Architectures:
          - x86_64
        Policies:
          - Statement:
              - Sid: ResizeUploadImage
                Effect: Allow
                Action:
                  - s3:GetObject
                  - s3:PutObject
                  - s3:DeleteObject
                Resource:
                  - !Sub "arn:aws:s3:::${bookImageShopBucketName}/*"
                  - !Sub "arn:aws:s3:::${bookImageResizeShopBucketName}/*"
        Events:
          ResizeImage:
            Type: S3
            Properties:
              Bucket: !Ref BookImageShop
              Events: s3:ObjectCreated:*
        Environment:
          Variables:
            WIDTH: !Ref width
            HEIGHT: !Ref height
            DES_BUCKET: !Ref BookImageResizeShop
    

    ImageResize function resource

This function differs from the Python Lambda functions we created earlier:

  • It uses Node.js 22.x runtime (not Python)
  • The code is deployed as a pre-packaged ZIP file (PackageType: Zip)
  • It is triggered by an S3 event (s3:ObjectCreated:*) rather than being called directly via API
  1. Finally, add the ImageResizeInvokePermission resource to grant the BookImageShop S3 bucket permission to invoke this Lambda function:

    ImageResizeInvokePermission:
      Type: "AWS::Lambda::Permission"
      Properties:
        FunctionName: !Ref ImageResize
        Action: "lambda:InvokeFunction"
        Principal: "s3.amazonaws.com"
        SourceAccount: !Sub ${AWS::AccountId}
        SourceArn: !GetAtt BookImageShop.Arn
    

    ImageResizeInvokePermission resource

If you created S3 bucket names different from the ones in this workshop, make sure to check and update the Policies > Resource ARNs and Environment Variables of the ImageResize function to match your bucket names.

Prepare the Function Code

  1. The directory structure should look like this:

    fcaj-book-shop
    ├── fcaj-book-shop
    │   ├── books_list
    │   │   └── books_list.py
    │   ├── book_create
    │   │   ├── book_create.py
    │   │   └── requirements.txt
    │   ├── book_delete
    │   │   └── book_delete.py
    │   └── resize_image
    │       └── function.zip
    └── template.yaml
    
    • Create the resize_image folder in the fcaj-book-shop/fcaj-book-shop/ directory.

    • Download the function.zip file below and place it in the resize_image folder:

    • Download the function.zip file here: function.zip

Build and Deploy

  1. Run the following commands to validate and build the SAM template:

    sam validate
    sam build
    

    SAM Validate and Build

  2. Deploy the updated stack:

    sam deploy
    

    The changeset will show the following new resources:

    • ImageResizeRole (AWS::IAM::Role)
    • ImageResize (AWS::Lambda::Function)
    • ImageResizeInvokePermission (AWS::Lambda::Permission)
    • ImageResizeResizeImagePermission (AWS::Lambda::Permission)
    • BookImageShop (AWS::S3::Bucket) - Modify to add S3 event notification

    Enter y to confirm the deployment.

    SAM Deploy - Changeset

  3. Wait for the deployment to complete. You should see Successfully created/updated stack.

    Deploy Complete

Verify on AWS Console

  1. Open the Lambda console. You should now see four functions: books_list, book_create, book_delete, and resize_image.

    Lambda Functions

  2. Click on the resize_image function. Review the Code source section — you should see the index.js file with the image resizing logic using the sharp library.

    Code Source

  3. Review the Function overview section. Notice the S3 trigger on the left side — this confirms that the function is automatically triggered by S3 events.

    Function Overview with S3 trigger

  4. Click the Configuration tab, then select Triggers from the left menu. Click Details to expand the trigger information. Verify:

    • Bucket arn: arn:aws:s3:::book-image-shop-by-tranvix
    • Event types: s3:ObjectCreated:*
    • Service principal: s3.amazonaws.com

    S3 Trigger Details

  5. Select Environment variables from the left menu. Verify the 3 environment variables:

    • DES_BUCKET = book-image-resize-shop-by-tranvix
    • HEIGHT = 280px
    • WIDTH = 200px

    Environment Variables

  6. Select Permissions from the left menu. Click on the execution role name (e.g., fcaj-book-shop-ImageResizeRole-...).

    Permissions - Execution Role

  7. At the IAM Role page, check the Permissions policies section. You should see:

    • AWSLambdaBasicExecutionRole (AWS managed) - for CloudWatch Logs
    • ImageResizeRolePolicy0 (Customer inline) - the custom policy we defined

    IAM Role Permissions

  8. Click on ImageResizeRolePolicy0 to expand it. Review the policy JSON that grants s3:GetObject, s3:PutObject, and s3:DeleteObject permissions on both the image shop and resize shop buckets.

    ImageResizeRolePolicy0 JSON