AWS CDK constructs with Serverless Framework - examples of usage
Introduction
In one of the last posts I've described Serverless plugin Lift used to use constructs from AWS CDK inside Serverless Framework. I briefly described 4 available constructs (static-website, storage, queue, and webhook).
Today, we will look at examples of using static-website and storage constructs, some of the most popular use-cases that developers often have.
I have prepared two repositories that will allow you to play with these constructors yourself in example projects. I strongly encourage you to experiment in your free time.
Static website construct
As a reminder, construct static-website allows deployment of both static pages and SPA applications (React and Vue). Construct creates an S3 bucket that holds the website files. Whereas CloudFront CDN serves the website from S3 over HTTPS, with caching at the edge.
The repository contains an application written in NextJS that can be exported to static HTML, and files related to the Serverless framework.
With the above service, we can easily do deployment of NextJS application exported to static HTML to AWS. With Amazon CloudFront we have supports HTTP and HTTPS, and through CDN, cache our websites all over the world.
For all information regarding usage, please refer to the Readme. Be aware though that when you export NextJS project to static html, many features that NextJS features will not work (e.g. Image Optimization).
At this moment there is no construct with server-side-rendering, so we don't have the SSR advantage here for which NextJS is really quite powerful. Probably SSR could be solved via Lambda@Edge, but I have no experience in this aspect. The Lift team is working on those things. In case of React or Vue applications, you will easily be able to do deployment.
Storage construct
For the example from Storage construct, I created the following repository.
This is a simple Serverless application written in Ruby that uses Lift to create an S3 bucket. S3 Event Notifications are then set up through the Serverless Framework. When someone uploads a file with a .png extension (such as a meme image) to the bucket, S3 triggers a Lambda function that creates a record in the DynamoDB database with information about the file.
All information about the service (setup, deployment, logs) can be found in the README.
Summary
As you can see Lift constructs can make our lives easier, and they have applications in various projects. I strongly encourage you to download the repositories created for this article and try for yourself the various options available for these constructs. Please note that in case your project grows beyond the plugin, you can eject from Lift at any time, as the plugin is based on CloudFormation.