DEV Community

Cover image for Create your own Image CDN using AWS CloudFormation and AWS Lamda
Ahmed Adel for AWS Community Builders

Posted on • Updated on

Create your own Image CDN using AWS CloudFormation and AWS Lamda

➥ In this article, I will demonstrate how to create your own Image CDN in a few simple steps using AWS CloudFormation and AWS Lamda (Serverless).

➥Before we dig in the tutorial, let's talk briefly about IMAGE CDNs

● What is Image CDN and why to use it ?

➥ Image content delivery networks (CDNs) are used for optimizing images, or in another words, load images with the best possible optimized size in its placeholder in a website.
➥ This approach increases website performance and speed.
➥There is a lot of third party CDNs you can use like ImageKit, Cloudinary, Bunny CDN and much more ...

But today, I'll be talking about creating your own Image CDN:

1- Download Serverless Image Handler CloudFormation Template.
2- Login to your AWS Console, Go To AWS CloudFormation Page.
3- Select Create Stack > with new resource
Create_Stack
you will then be redirected to create stack page.

4- In Prepare template section select Template is ready,
In Specify template select Upload a template file, and then upload the template file you previously downloads then click next

Create_Stack_page1
5- In Specify stack details page, just enter a name for your stack , let's assume we named it My-New-Demo-CDN and then in Image Sources field, here you should enter all the S3 Bucket names that will be the original source of the images will be uploaded (the images with the highest quality that will be resized later by the Lamda function depending in the requested size)

Image description
then hit next ...

6- In Configure stack options, you can enter any tag you want for the resources or leave it empty, no need to get into advanced details at the moment, click next

Image description
7- You will be directed to the Review My-New-Demo-CDN, where you will review all the options you previously selected for the stack like template, parameters, stack options, permissions, policy ...etc.

Image description

  • Check I acknowledge that AWS CloudFormation might create IAM resources and hit Create Stack.

8- Creating process will start and it will take a few minutes to finish ...

Image description

9- Then you will notice that all resources are ready and your CDN create status will be Create Complete.

stack_ready

10- Navigate to Outputs Tab, you will notice that an API End Point has been created for your stack to use, in our example it is https://r3fy67sunclsio8y5.cloudfront.net, this link is the link that will replace your image bucket url.

get_cdn_url

**Now your CDN is created and ready to use ...


How To Use it ?

1- Let's first upload an cute cat image with high quality to one of the buckets that we assigned as an image source for the CDN, don't forget to allow public access on uploading this image to be able to use it from the browser.

Image description

then we can view this image from the browser from its public url

Image description

As we can see, the url consists of 2 parts:
a- Base Bucket URL https://default-bucket-test-image-cdn.s3.eu-west-3.amazonaws.com/
b- image path in the bucket cute-cat-4k-wg.jpg

Also notice that the image is loaded with its full size when you open it in the browser

Image description

So, what if I need to use this image multiple time in my website, once as a thumbnail, other time in 300x300 size and other one with its full size, is it logical to save it in my storage three times with three different sizes , or may be load it each time with the full size ? 🤔

2- Here will come the magic 🧙🏻 of the CDN, let's replace the bucket URL with our previously created CDN URL , and just enter a size for the image so that our link will be
https://r3fy67sunclsio8y5.cloudfront.net/300x300/cute-cat-4k-wg.jpg
and notice what happens

Image description

Our image is now resized to 300x300 and only 21Kb is loaded to the browser instead of 3.1 Mb 🤯🤯.
Try it with other sizes and see the magic yourself ...


Actually, this CDN is not limited only to resizing ....

✅ it also extends the functionality of many of the Thmbor Filters to allow you to set background color, blur, grey scale, image type, image format, quality, crop, rgb, sharpen upscale , setting watermark and other filters ...
check the table below for the list of filters

Filter Name Filter Syntax
Autojpg /filters:autojpg()/
Background color /filters:background_color(color)/
Blur /filters:blur(7)/
Color fill /filters:fill(color)/
Convolution /filters:convolution(1;2;1;2;4;2;1;2;1,3,false)/
Equalize /filters:equalize()/
Grayscale /filters:grayscale()/
Image format (heic, heif, jpeg, png, raw, tiff, webp) /filters:format(image_format)
Image type (jpeg, png, gif) /filters:format(jpeg)/
No upscale /filters:no_upscale()/
Proportion /filters:proportion(0.0-1.0)/
Quality /filters:quality(0-100)/

for example, our link will be like:

  • https://r3fy67sunclsio8y5.cloudfront.net/fit-in/500x500/cute-cat-4k-wg.jpg for resizing
  • https://r3fy67sunclsio8y5.cloudfront.net/fit-in/500x500/filters:blur(7)/cute-cat-4k-wg.jpg for resizing then blurring the image
  • https://r3fy67sunclsio8y5.cloudfront.net/fit-in/500x500/filters:quality(50)/cute-cat-4k-wg.jpg for resizing then reducing quality to 50% and so on for all the filters

Archeticture of this CDN:

Image description

Cost of this CDN:

➥ You are responsible for the cost of the AWS services used while running this solution. As of December 2021, the estimated cost for running the Serverless Image Handler solution with an AWS Lambda processing time of two seconds per image, where each image is approximately 1 MB in size, and default settings in the US East (N. Virginia) Region is $15.29 per month for 100,000 new images, $149.30 per month for 1,000,000 new images, and $744.91 per month for 5,000,000 new images (refer to the following table for the cost breakdown). This includes estimated charges for Amazon API Gateway, AWS Lambda, Amazon CloudFront, Amazon S3 storage, and AWS Secrets Manager.

Cost to process # of new images per month
AWS service 100,000 1,000,000 5,000,000
Amazon API Gateway $0.35 $3.50 $17.50
AWS Lambda (2 seconds processing time per image) $3.35 $33.53 $167.67
Amazon CloudFront (process 1 MB images per request) $8.40 $84.01 $420.04
Amazon S3 (store 1 MB images) $2.29 $22.86 $114.30
AWS Secrets Manager* $0.90 $5.40 $25.40
Total monthly cost: $15.29 $149.30 $744.91

For a full documentation of this solution, please visit the official documentation

Top comments (11)

Collapse
 
yongchanghe profile image
Yongchang He

Thank you for sharing such detailed information about the picture processing tool in AWS.

Collapse
 
ahmedadel profile image
Ahmed Adel

You are welcome :)

Collapse
 
vadorequest profile image
Vadorequest

For reference, here is the source code:
github.com/aws-solutions/serverles...

Collapse
 
alex_dim profile image
aljosa dimitrijevic

Hi, i created this stuck and i got message from AWS after some time.
Does this message means something important that i need to take action for ?
Message itself:

"AWS CDK v1 is now in maintenance mode"
"You are receiving this notification because you currently have at least one CloudFormation Stack deployed using version 1 of the AWS Cloud Development Kit (CDK). AWS CDK v1 is now in maintenance mode. During maintenance mode, AWS CDK v1 releases will contain new and updated resource level "L1" constructs, critical bug fixes, and security updates only. CDK v1 will not receive higher-level "L2" construct updates for new or existing services while in maintenance mode. Maintenance mode has a duration of 12 months.

After June 1, 2022, the AWS CDK team is focusing all efforts on adding new features and bug fixes to CDK v2 only. To continue to receive API updates, new features and bug fixes, migrate to AWS CDK v2 today. Check out the migration guide [1] to get started.

On June 1, 2023, CDK v1 will reach end-of-support. After this date, the AWS CDK v1 will no longer receive updates or releases. Previously published releases will continue to be available via public package managers and the code will remain on GitHub. Use of a CDK version which has reached end-of-support is done at your own discretion. We recommend you upgrade to the new major version.

For more information, see this AWS Blog Post from December 12, 2021 [2], and this GitHub issue from April 8, 2022 [3].

If you have any issues, or need support in migrating, please open a discussion on GitHub [4], or an issue in the aws-cdk GitHub repository [5].

If you require further assistance, reach out to AWS Support [6].

[1] docs.aws.amazon.com/cdk/v2/guide/m...
[2] aws.amazon.com/blogs/developer/how...
[3] github.com/aws/aws-cdk/issues/19836
[4] github.com/aws/aws-cdk/discussions...
[5] github.com/aws/aws-cdk/issues/new/...
[6] aws.amazon.com/support
"

Collapse
 
duyquocnguyen profile image
duyquocnguyen

Thank you and I have a question, I have upload a video with .mp4 and I can't into. Maybe this is error with api gateway. But I don't know modyfi them.

{"status":400,"code":"RequestTypeError","message":"The type of request you are making could not be processed. Please ensure that your original image is of a supported file type (jpg, png, tiff, webp, svg) and that your image request is provided in the correct syntax. Refer to the documentation for additional guidance on forming image requests."}

Collapse
 
abeskazali profile image
Ali Beşkazalı • Edited

If you want change video resize, file type or etc. on AWS, you should use AWS Elemental MediaConvert service. You can check this link for CloudFormation template.
aws.amazon.com/tr/solutions/implem...

Collapse
 
ahmedadel profile image
Ahmed Adel • Edited

This CDN is for resizing images only, it does not support video files.

Collapse
 
gnprojects profile image
gn-projects

I have some issue, didn't yet tried with aws object storage, but i keep getting 429 error which is too many requests are done per seconds.

Will this "own" sort of can handle such? Or does it read from the object storage on each get request?

Did you face such error?

Collapse
 
nikola8 profile image
Nikola

Hi Ahmed,

First of all, thanks a lot for your great article! 🙏🏼 The way you write is just great. 🥇 As you said, it’s pure magic using only one image for all sizes. I’m already testing ImageKit, but as I’m handling massive amounts of images the bandwidth limit just does not fit.

May I ask you to help me clarify following scenario:

I’ve created a WordPress based real estate portal. The amount of images grows exponentially, specially because of the auto-created resizings. Do you know how to make WordPress use the CDN created resizing? In other words, the theme auto-creates about 9 resized versions of the source image and all images are uploaded to the AWS S3 bucket. Afterwards, images are served optimized via CDN. But, how will the WordPress theme know that instead of using previously created resized versions, it is actually going to use the resized versions served by the CDN?

Best regards,
Nikola.

Collapse
 
antimoron profile image
AntiMoron

Following this guide, aws just created 122 resouces which really made me feel nervous about the fee.

Collapse
 
ahmedadel profile image
Ahmed Adel

Image description

This is the whole architecture of the solution, and a pricing model is attached in the article above, I'm using this myself btw, you can try it for a short amount of time or estimate the cost using CloudFormation.
Also you can easily drop all the resources whenever you want using CloudFormation