Customizing Content Delivery with Lambda@Edge
In this workshop you will learn how you can use Lambda@Edge to extend functionality of your web-application or a website.
Backstory: Aliens have arrived. Humanity cannot communicate with them because we do not speak a common language. However, the aliens are curious and willing to learn the language by looking at beautiful images with short text descriptions. Let's build a website to help the aliens learn our language using simple learning cards!
The workshop demonstrates the usage of the following AWS services:
- Amazon S3: the website's static files will be stored in an S3 bucket
- Amazon DynamoDB: the dynamic data will be stored in a DynamoDB table
- Amazon CloudFront: the fast and secure content delivery will be performed by a CloudFront distribution
- AWS Lambda@Edge: the dynamic content generation and content customization will be driven by Lambda@Edge functions
Lab 0 - Launch the Stack
To start the workshop, launch the CloudFormation stack to bootstrap the resources in the us-east-1 (N.Virginia) region.
Click the launch stack button below to kick it off, accept all default values and wait for CloudFormation to complete the creation of the stack.
Need more detailed instructions? Proceed to
Lab 0 - Launch the stack (steps with screenshots)
The CloudFormation stack will create several AWS resources for this workshop, including an S3 bucket, CloudFront distribution, DynamoDB table, and IAM roles to be used as execution roles for your Lambda@Edge functions. You can see all resources created by the CloudFormation stack in AWS CloudFormation Console.
Please note down the DynamoDB table name, like
AlienCards-8d8c0ff0, and a CloudFront distribution domain name, like
d21536tl4oas8g.cloudfront.net. You will need to use them in the upcoming labs. You can see these unique names created in your AWS account in Amazon DynamoDB Console and in Amazon CloudFront Console respectively.
Note that in the labs we sometimes refer to the CloudFront domain name as
d123.cloudfront.net, but you need to use the unique domain name of your CloudFront distribution.
Lab 1 - Security
Security is always the top priority. Learn how to check and improve website security by configuring HTTP to HTTPs redirect and adding standard security headers to enforce HTTPS connection is always used by the client and prevent XSS.
Lab 2 - Content Generation
Learn how to create a Lambda function that dynamically generates HTML content which can be cached by CloudFront and returned back to your viewers.
Lab 3 - Simple API
Learn how you can use Lambda@Edge to implement a simple API that accepts POST requests from the viewers and modifies the web application state in a DynamoDB table.
Lab 4 - Pretty URLs
Use Lambda@Edge to introduce pretty semantic URLs to your web application. Pretty URLs are easy to read and remember, they also help with search engine optimization and allow your viewers to use the descriptive links in social media.
Lab 5 - Customization
Learn how to serve content customized for the device type of a viewer.
Here is a few extra challanges for you if you feel up to it.
Why aliens that landed in, say, Japan are learning English? It would make sense for them to learn Japanese instead, perhaps using some authentic pictures and characters. With Lambda@Edge you can inspect
CloudFront-Viewer-Countryheader and select a different S3 bucket (for example, in the
ap-northeast-1region) for CloudFront to fetch the images from using Content-Based Origin Selection feature. For more information, please refer to some public documentation:
Consider using Amazon DynamoDB Global tables. In the previous labs, we implemented Lambda@Edge functions in a way that they access a DynamoDB table in a single region, thus, introducing extra latency. This can be improved if DynamoDB table is replicated to multiple regions closer to where your viewers are. For more information, please refer to some public documentation:
Sometimes, you may want to introduce a new change in your website to only a fraction of your viewers. You can do it with Lambda@Edge, for example, by rolling a dice and setting a cookie on the client side so that clients get consistent behavior, i.e. either variant A, or variant B, but not a mix of them.
Interested in exploring more use cases that Lambda@Edge supports? Check this out.
Clean your AWS resources created for this workshop.