paint-brush
5 Tips to SpeedUp Serverless Web Apps in AWSby@ashan.fernando
9,158 reads
9,158 reads

5 Tips to SpeedUp Serverless Web Apps in AWS

by Ashan FernandoApril 6th, 2018
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Have you deployed your Serverless Web Application in AWS using CloudFront, API Gateway, Lambda and still finds it’s not performing up to the expectations?

Company Mentioned

Mention Thumbnail
featured image - 5 Tips to SpeedUp Serverless Web Apps in AWS
Ashan Fernando HackerNoon profile picture

Have you deployed your Serverless Web Application in AWS using CloudFront, API Gateway, Lambda and still finds it’s not performing up to the expectations?

This article focuses on 5 Tips to improve Serverless Web Application Performance in general.

1. Keeping Lambda Functions WARM

AWS Lambda function has a considerable cold start time. Even though it reduces when you increase the memory of individual functions, still it could provide a noticeable effect for your web application.

Implement a mechanism to keep Lambda functions WARM. You can do this using either of the following approaches.

2. Avoid CORS By Accessing Your API and Frontend Using the Same Origin

If you have deployed your frontend (Angular, ReactJS, VueJS App) to S3 and serves the API using API Gateway you will come across CORS (Cross-Origin Resource Sharing). This is not a bad thing, but the problem is when you send headers like Authorization (Or Anything other than the basic headers), modern Browser sends a special request first called Preflight Request before sending the actual request.

This creates a waterfall effect where it adds twice the latency for the full request fulfillment. To avoid this, you can use AWS CloudFront and Plugin API Gateway and S3 both to the same domain and configure the routing there.

When connecting API Gateway to CloudFront, make sure you do the following

  • When creating Origin, set Origin Protocol Policy to HTTPS

  • Make sure you select the API Origin.
  • Set the path pattern appropriately (e.g; /dev-stage/resource/* )
  • When creating the behavior, Do Not Forward the Host Header (WhiteList the rest)
  • Also set TTL value to 0, unless you want to cache API (Even if you plan to cache, create separate behaviors for individual paths).

3. Deploy Your API Gateways as Regional Endpoints

When you deploy your API Gateways, it internally attaches a CloudFront distribution to it. This creates a latency since it adds another hop for request forwarding.

You can set the endpoint type to REGIONAL to deploy them as regional endpoints to avoid this.

  • If you are using Serverless Framework, refer Configuring Endpoint Types.
  • If you are deploying your API Gateway using AWS SAM or AWS CLI add the “value”: “REGIONAL” as necessary.

4. Optimize the Frontend

Even you use AWS CloudFront to serve the Frontend, you can further improve the performance by compressing the files such as JavaScript, CSS using GZIP and Upload to S3. Also make sure you use the correct Content-Encoding: gzip headers. This will reduce the required bandwidth to download the files in several factors.

In addition, enable Compress Objects Automatically in CloudFront Behavior configuration to automatically compress content for web requests that include Accept-Encoding: gzip in the request header. This allows CloudFront to compresses files of certain types for both Amazon S3 and custom origins.

Since CloudFront uses the best effort (Not guaranteed) when severs are not busy at Edge locations to compress the files to GZIP, storing the files compressed can provide better performance.

5. Use Appropriate Memory for Your Lambda Functions

When you are using smaller memory for Lambda (e.g; 128MB) it uses, lower speed CPU also matching the memory. This can have adverse effects on your Lambda performance. Therefore it is important to increase memory (At least 512MB for production applications) appropriately based on the logic handled by the function.