Skip to content

Multi-region serverless application, failover to a different region based on latency.

License

Notifications You must be signed in to change notification settings

mavi888/cdk-failover-simple-sls-app

Repository files navigation

Latency based routing for Serverless Application

  • Infrastructure as code framework used: AWS CDK
  • AWS Services used: AWS DynamoDB, Amazon Route53, AWS API Gateway

Summary of the demo

Multi-region serverless application, failover to a different region based on latency.

Architecture diagram

In this demo you will see:

  • How to configure a global table using DynamoDB
  • How to create a API Gateway that integrates directly to DynamoDB
  • How to send traffic to the right stack depending on the latency.

This demo is part of a video posted in FooBar Serverless channel. You can check the video to see the whole demo.

Important: this application uses various AWS services and there are costs associated with these services after the Free Tier usage - please see the AWS Pricing page for details. You are responsible for any AWS costs incurred. No warranty is implied in this example.

Requirements

  • AWS CLI already configured with Administrator permission
  • AWS CDK - v2
  • NodeJS 16.x installed
  • CDK bootstrapped in your account (in the regions where you are going to deploy this)

Deploy this demo

  1. Configure this project by changing the config.json file to match your project. For running this demo you need to have a domain in the account that you are going to deploy this app.

  2. Deploy the project to the cloud, this will deploy the app in 2 regions - Ireland and Virginia.

cdk synth
cdk deploy --all

How to test this demo

After deploying this application.

  1. Open a REST client and do
POST https://yourdomain.com/data/

And pass a JSON as a body.

That returns a requestId, save that to see the result.

{
  "requestId": "8f618771-523c-47c2-8ce8-c1df35b01ac6"
}
  1. In a REST client do
GET https://yourdomain.com/data/<requestId>

That will return the Item that was stored in the database. You can see what region it was stored from. The region will be the closest to you. You can change your location by connecting to a VPN and moving closer to the other region.

{
  "Item": {
    "region": {
      "S": "eu-west-1"
    },
    "pk": {
      "S": "8f618771-523c-47c2-8ce8-c1df35b01ac6"
    },
    "name": {
      "S": "marcia"
    }
  }
}

Delete the app

To delete the app in all the regions. You might need to delete manually the Replicated Global Table:

cdk destroy --all

AWS CDK useful commands

  • npm run build compile typescript to js
  • npm run watch watch for changes and compile
  • npm run test perform the jest unit tests
  • cdk deploy deploy this stack to your default AWS account/region
  • cdk diff compare deployed stack with current state
  • cdk synth emits the synthesized CloudFormation template

Links related to this code

Learn more about building multi-region serverless applications with AWS

About

Multi-region serverless application, failover to a different region based on latency.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published