- Infrastructure as code framework used: AWS CDK
- AWS Services used: AWS DynamoDB, Amazon Route53, AWS API Gateway
Multi-region serverless application, failover to a different region based on latency.
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.
- 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)
-
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. -
Deploy the project to the cloud, this will deploy the app in 2 regions - Ireland and Virginia.
cdk synth
cdk deploy --all
After deploying this application.
- 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"
}
- 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"
}
}
}
To delete the app in all the regions. You might need to delete manually the Replicated Global Table:
cdk destroy --all
npm run build
compile typescript to jsnpm run watch
watch for changes and compilenpm run test
perform the jest unit testscdk deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk synth
emits the synthesized CloudFormation template
Learn more about building multi-region serverless applications with AWS