Global Serverless Apps with AWS Lambda & API Gateway

Serverless technologies are lowering the barrier to entry for global deployments with on-demand pricing and scaling. AWS’ serverless offerings are now supported in 16 regions, and with the help of Up Pro’s latest v1.2.0 release we’re going to take a look at setting up a globally distributed app to decrease latency for your customers.

Image for post
Image for post

Before we get started, if you want to follow along first install Up:

$ curl -sf | sh

Then create a new directory with the following file:

Edge-optimized APIs

First let’s take a look at how the “edge” optimized API Gateway endpoint type works, as they’re the default in Up.

Edge-optimized endpoints leverage of the Cloudfront CDN network—when a client sends a request to your domain it is routed to the nearest Cloudfront PoP location, which in turn sends it over the AWS network to your origin location.

Utilizing a CDN in this manner decreases latency when compared to sending traffic directly to a single origin. If we run the endpoint through the Apex Latency tool we can see that physics cannot be beaten here, we still have to transmit the request and response over-seas to the origin in Oregon, so we’re still favoring the west in this case.

Image for post
Image for post

If you have a single origin database & server this can be a good solution, but if you can make your data globally available there’s a better way to go, let’s look at that next.

Regional APIs

The second type of endpoint supported by API Gateway is the “regional” endpoint, these are effectively “bare” endpoints, bypassing Cloudfront. The use of Cloudfront can increase latency if your customers are close to the given region, as it introduces an additional hop and TCP/SSL handshake.

To use regional endpoints in Up all you need to do is change to “regional”. Note that this is an Up Pro feature.

First let’s deploy the application to Oregon:

$ up env set --region us-west-2 MESSAGE="Hello from us-west-2"
$ up --region us-west-2

When curling the app from my place in London, I get a response from , because that’s the only deployment we have.

$ curl
Hello from us-west-2
$ curl
Hello from us-west-2

As shown by the latency check for this setup, we only have a reasonable response latency in the west.

Image for post
Image for post

Let’s deploy an additional endpoint to the region in London:

$ up env set --region eu-west-2 MESSAGE="Hello from eu-west-2"
$ up --region eu-west-2

Up utilizes a Route 53 Latency routing policy to select the quickest endpoint for your customer. If we check the response again we now get back instead of from here in London, so it’s working correctly!

$ curl
Hello from eu-west-2

Checking with the latency tool again you can see that regions close to London are now much more responsive:

Image for post
Image for post

Depending on your situation you may want to deploy all the regions, but without global data it won’t be effective, let’s look into that next!

Global Databases

So we have respectable API latencies now, but what about when our database is introduced?

If you have a single database located in one region, you’re going to have to send requests over-seas to this location, effectively replicating the “edge” endpoint behavior. In order to properly utilize regional endpoints you should have a replicated or globally available database, such as DynamoDB Global Tables.

This is where Up’s environment variables can come in handy, they’re encrypted so you can utilize them to store a region-specific settings, such as database connection string. Here we’re going to tweak to point to a western and eastern RDS instance:

# Set env vars
$ up env set --region us-west-2 DB='postgresql://'
$ up env set --region eu-west-2 DB='postgresql://'
# Deploy new env vars
$ up production --region us-west-2
$ up production --region eu-west-2

After redeploying the changes your European customers will now connect to the RDS instance in London, and Western countries will connect to Oregon.

Head over to Up’s GitHub to view more documentation, and subscribe to Up Pro for features such as:

  • Global deployments
  • Encrypted environment variables
  • Error alerting via Slack, SMS, and email
  • Deployment log with Git integration
  • Instant rollbacks
  • Active warming
  • Priority email support
  • Unlimited use in your team (any # of apps and developers)

Code. Photography. Art.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store