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.
s3:ObjectCreated:*)Open the template.yaml file in the fcaj-book-shop folder.
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

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

This function differs from the Python Lambda functions we created earlier:
PackageType: Zip)s3:ObjectCreated:*) rather than being called directly via APIFinally, 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

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.
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
Run the following commands to validate and build the SAM template:
sam validate
sam build

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 notificationEnter y to confirm the deployment.

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

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

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.

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

Click the Configuration tab, then select Triggers from the left menu. Click Details to expand the trigger information. Verify:
arn:aws:s3:::book-image-shop-by-tranvixs3:ObjectCreated:*s3.amazonaws.com
Select Environment variables from the left menu. Verify the 3 environment variables:
DES_BUCKET = book-image-resize-shop-by-tranvixHEIGHT = 280pxWIDTH = 200px
Select Permissions from the left menu. Click on the execution role name (e.g., fcaj-book-shop-ImageResizeRole-...).

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

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.
