Leverage AWS CloudFront for Serverless Web Applications using Go

Nowadays serverless architecture become more and more popular. The main idea of it is the absence of the servers. One most important benefit of serverless is that it drastically reduces operation costs. You pay only when you get paid from your customers .

Of course, servers exists in the serverless world. But their role has changed. Serverless servers are behind the scene and provide a generic execution environment for your application.

Single Page Applications (SPA’s) and serverless architecture

Few applications can be truly serverless. There has to be some sort of backend logic. Single Page Applications is a good candidate for that. Usually SPAs is a set of static assets — minified source code and resources such as graphic images, multimedia assets. In AWS environment, application logic works entirely through API Gateway, Lambda, Step Functions or even AWS IoT for WebSocket capabilities.

One of the solutions to host your static web application might be an Amazon S3 Static Website Hosting service. To configure a S3 bucket for static website hosting, you add a website configuration to your bucket. Basic configuration includes index document, error document and permissions.

Good designed SPA should have proper cache-control, content-type and many other headers set up on their assets

Despite of the fact that S3 is really great for hosting static files there are some disadvantages preventing using it for enterprise-grade applications. There are some of them:

  • Can’t use SSL certificate and TLS
  • Does not support region based hosting
  • Advanced conditional redirects rules are limited
  • Lack of HTTP/2 protocol support
  • Limited access restrictions capabilities

Advantages of AWS CloudFront

AWS CloudFront in tandem with S3 helps to mitigate all of these issues. Static S3 website is used as an origin server which servers content only when edge locations has no up to date content on it. Moreover AWS edge locations located closer to all of your global customers which makes your apps really fast.

According to cloudping.info an IP packet delay between Asia Pacific (Singapore) region and North America might be up to 400ms which is unacceptable for most applications

Sometimes initial configuration of S3 and CloudFront might be a bit complex. You need to deal with permissions, custom HTTP headers, CloudFront invalidations. For one of my previous projects I’ve developed a helper utility written with GO which makes SPA developer life a bit easier.

Deploying SPA applications with CloudfrontDeploy

A go-cloudfront-deploy is a small utility written with GO. It allow developers quickly deploy their web sites to to S3 and automatically set up CloudFront distribution. go-cloudfront-deploy set proper HTTP headers for S3 resources. This is really important for browsers in order to process resources correctly and employ HTTP caching.

If you don’t have Go installed, please refer this page. Please make sure that you have AWS CLI installed.

In order to install go-cloudfront-deploy open your terminal and run

go install github.com/yury-sannikov/go-cloudfront-deploy

Now we are ready to deploy your static assets straight to the AWS!

go-cloudfront-deploy clean s3 cloudfront -b yourdomain.com -r us-west-1 -p static/assets/path/

This command will do the following:

  • Clean S3 bucket to delete old resources. Modern tools such Webpack2 has long term caching strategy based on chunk hashes. Leaving old files might lead to improper application functioning.
  • Upload files to S3 bucket, set proper access rights, set proper HTTP headers.
  • Create new CloudFront distribution. This may takes a while. Your S3 bucket content will be transferred to CloudFront Edge Locations to speed up delivery. If you already have distribution, go-cloudfront-deploy runs invalidation so you would not need to wait until Cache-Control header expires your content.

Final Thoughts

While it’s pretty easy to use EC2 or Heroku to distribute your content, costs and downtime associated with errors, breaches and maintenance might be really sensitive. Simplifying system, reducing moving parts and point of failures gives you great benefits in a long run.