A static website contains web pages with fixed content and these individual web pages on the website include static content which might also contain client-side scripts.
Amazon Web Services (AWS) S3 Static Website Hosting is cheap, scalable, and performant. When combined with CloudFront it’s even better. Setting up an S3 bucket to function as a website is nice and simple and will support HTTPS out of the box, however, you’ll be restricted to using the bucket URL as the endpoint.To work around this, we’re able to use CloudFront to host our website from Amazon’s edge locations with a custom SSL certificate setup for our domains.
Amazon S3 is an acronym for Amazon Simple Storage Service. It is a typical web service that allows us to store and retrieve data via an API reachable over HTTPS. The service offers unlimited storage space and stores your data in a highly available, durable and secure way.
Amazon CloudFront is a content delivery network (CDN). It caches the content on Amazon’s globally distributed network of proxy servers and then intercepts user’s requests in order to route them to their closest proxy server.
AWS Certificate Manager handles the complexity of creating and managing public SSL/TLS certificates for your Amazon Web Services (AWS) based websites and applications. You can use public certificates provided by ACM (ACM certificates) or certificates that you import into ACM. ACM certificates can secure multiple domain names and multiple names within a domain.
Lambda@Edge allows running Lambda functions to customize the content that CloudFront delivers and executing the functions in AWS locations closer to the viewer. The functions run in response to CloudFront events without provisioning or managing servers. With Lambda@Edge we can add, delete, and modify headers and rewrite the URL to direct users to different objects in the cache.We can use Lambda functions to change CloudFront requests and responses at the following points:
Thumb rule for Lambda@Edge
To explain the concept better, we will explain the process for one of our websites – Polly.
We start by getting our static website uploaded to Amazon’s S3 service and configuring the bucket for static website hosting.
{“Version”: “2012–10–17”,“Statement”: [{“Sid”: “PublicReadGetObject”,“Effect”: “Allow”,“Principal”: “*”,“Action”: “s3:GetObject”,“Resource”: “arn:aws:s3:::{BUCKET}/*”}]}
The certificate allows the user’s browser to verify that they are on a real website. It also enables the encryption of data transferred between the user and the website. We can either request the certificate in ACM or we can import the certificate in ACM.We use the Certbot CLI Tool to create an SSL Certificate and import that in ACM. Domain registration can be done in GoDaddy, Route 53 or any other domain provider of your choice.For importing a certificate go to the “Import a Certificate” option and the below window should be displayed.
CloudFront intercepts requests and responses at CloudFront edge locations. It means you may add “intelligence” in the CDN, without having to forward the request to the backend and losing benefits of content caching and geographical proximity with the client.
The complete set of examples on how we can manipulate requests and responses at the edge location can be viewed here.Let’s take a look at how we suffice our use cases with the help of Lambda@Edge.Polly has a login page for new users/logged out users and a dashboard for existing and logged in users.In the below code we are checking whether the cookie has a token or not. If not then it should redirect to Login Page, else it should redirect to the Dashboard Page.exports.handler = (event, context, callback) => {
let path, request, headers, request_url;
request = event.Records[0].cf.request;
headers = request.headers;
if (headers.cookie) {
for (let i = 0; i < headers.cookie.length; i++) {
// Add logic to fetch cookie and redirect the URLs based on cookies
}}
callback(null, request);
};After writing the code above, save and publish a version of Lambda.Now add the Lambda to CloudFront distribution inside the behavior section. Select the behavior and edit it.
Select the trigger and add the Lambda ARN in front of it. Lastly, click “Yes Edit” and let the CloudFront be deployed and your work is done.Lambda ARN will be displayed in the Lambda Management console at the top right side just after you publish the version.That’s it. You did it!
Still having trouble de-coding? or want to know more? Book a demo now!