DEV Community

Arnob
Arnob

Posted on

Images Resizing From S3 Using Lambda Function and CloudFront

Change the image size on the fly from S3 Object, also using CDN for serving the image.

Image From Medium

Story: Images are store in S3. The recruitment is view any image any size on the fly and send to the response using recommend size. The resizing image will serve at CDN using CloudFront.

Lambda Function Deployment

For lambda function i use Serverless Apps. 1st need to create a node package.

npm init
Enter fullscreen mode Exit fullscreen mode

Output

{
  "name": "image-resizer-function",
  "version": "1.0.0",
  "description": "Image Resizer Function",
  "main": "index.js",
  "keywords": [],
  "author": "",
  "license": "ISC",
}
Enter fullscreen mode Exit fullscreen mode

Now install serverless

npm install serverless -g
Enter fullscreen mode Exit fullscreen mode

After install the serverless. Create a Serverless app, At terminal

serverless
Enter fullscreen mode Exit fullscreen mode


captionless image

Select HTTP API.

captionless image

Project created.

Now adding the code to that index.js

At Lambda Function Preview

captionless image

Here added 2 packages. So need to add 2 layer at Lambda Function.

If you deploy this project that project will get Internal Server Error. Now we can deploy this function. Then add the layer to that function.

Lambda Function Deployment

Here the Serverless yaml config

service: aws-node-http-api-project
frameworkVersion: '3'
provider:
  name: aws
  runtime: nodejs18.x
  region: us-east-1
functions:
  api:
    handler: index.handler
    events:
      - httpApi:
          path: /{pathA}
          method: get
      - httpApi:
          path: /{pathA}/{pathB}
          method: get
      - httpApi:
          path: /{pathA}/{pathB}/{pathC}
          method: get
Enter fullscreen mode Exit fullscreen mode

Need to run this at terminal

serverless deploy 
Enter fullscreen mode Exit fullscreen mode

Output

captionless image

The AWS Lambda Console

captionless image

Here a Serverless add a API Gateway. Now adding a S3 Bucket with Lambda Function.

S3 Bucket Creating and Manage

Here the bucket name is img-resizer-v1

The Object Ownership ACLs Enabled*.*

Allow Public Access.

captionless image

Creating Done.

Now edit the bucket policy

Bucket > img-resizer-v1 > Edit Bucket Policy

{
    "Version": "2012-10-17",
    "Statement": [        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:*",
            "Resource": "arn:aws:s3:::img-resizer-v1/*"
        }
    ]
}
Enter fullscreen mode Exit fullscreen mode

Now uploading some images at Buckets.

captionless image

Connecting Lambda function with S3

Click Add Trigger

captionless image

Select S3

captionless image

Select the Selected bucket. Then Click Add

captionless image

Add Layer

Now Add Layer at the function. At the bottom get the layer section.

captionless image

Click Add Layer. At Choose a layer select Custom Layers.

captionless image

Note: Now you add the layer i will share later.

Add aws-sdk and sharp with this layer.

captionless image

At Function 2 Layer added.

Testing

Now test the Lambda Function. At the Configuration Tab. Select Triggers. You will see the API Gateway. There you will find a API Endpoint

captionless image

For testing view the image from API Gateway link.

captionless image

For testing the image resizing. Given the image size width is 500px

https://u4kqo8jrjd.execute-api.us-east-1.amazonaws.com/image.png?w=500
Enter fullscreen mode Exit fullscreen mode


captionless image

Working 😁. I think it will return some error πŸ˜….

Now Applying CloudFront with Lambda Function

Applying Cloudfront CDN

Creating a Distributions.

captionless image

Select Origin Domain, which is given URL by API Gateway

u4kqo8jrjd.execute-api.us-east-1.amazonaws.com
Enter fullscreen mode Exit fullscreen mode

Protocol is HTTPS only.

At Default cache behavior, Viewer protocol policy is HTTPS Only.

At Cache key and origin requests, Cache policy is Caching Optimized

For creating image resizing parameter need to create a cache policy and origin request policy.

Goto Policy Tab, Creating cache policy and origin request policy

captionless image

Then click Origin Request Tab. Create origin request policy

captionless image

Click Create Button.

At CloudFront page click refresh button, newly created policy will show there,

captionless image

At Web Application Firewall (WAF) Select Do Not Enable Security Protections

At Settings, if you wanted to add domain, you need to create a Certificate, then add the sub-domain with CloudFront.

Goto AWS Certificate Manager > Certificates

captionless image

Give the Domain Name

image-resizer.arn-ob.xyz
Enter fullscreen mode Exit fullscreen mode

Click Request.

captionless image

Click Certificate ID

captionless image

Add the DNS to CNAME name and value to Cloudflare

captionless image

Click Save. It will take some time to validate the Domain. Now it status is Pending Validation.

captionless image

After 2–3 min. It will issued.

captionless image

Now add the sub-domain to CloudFront

captionless image

All Done. Click Create Distribution. Now it will take 5–10 min for deploying CDN.

captionless image

Now connect the sub-domain with CloudFront.

captionless image

Domain mapping done with CloudFront CDN.

Tesitng

Original Image

captionless image

Resizing Image

captionless image

Image Resizing Done. 😁

So here we use CloudFront CDN, Now testing the image return response time.

captionless image

Here CloudFront CDN working Good. 😁

The project repository:
https://github.com/arn-ob/aws-img-resizer-s3-lambda-cdn

Thank you for Reading…

Top comments (0)