Building serverless multi-Region WebSocket APIs with Amazon API Gateway, AWS Lambda and Amazon DynamoDB
This repository contains the full source code that is used in the blog post Building serverless multi-Region WebSocket APIs.
- An AWS account
- Installed and authenticated AWS CLI (authenticate with an IAM user or an AWS STS Security Token)
- Installed and setup AWS Cloud Development Kit (AWS CDK)
- Installed Node.js, TypeScript and git
git clone git@github.com:aws-samples/multi-region-websocket-api
2. Open the repository in your preferred IDE and familiarize yourself with the structure of the project.
.
├── cdk CDK code that defines our environment
├── img Architecture image
└── src
└── lambda Handler code of the lambda function used to distribute the Lambda layer
node.js dependencies are declared in a package.json
.
This project contains a package.json
file in two different folder:
cdk
: Dependencies required to deploy your stack with the CDKsrc
: Dependencies required for the Lambda function, i.e. TypeScript types for AWS SDK
Navigate to each of the folders and run npm install
Open cdk/bin/websockets-blog.ts
and adjust the Regions to deploy the application.
// List of Region codes to deploy the application to
const regionsToDeploy = ['us-east-1', 'eu-west-1', 'ap-northeast-1'];
Navigate to the cdk
folder and run the following commands.
cdk synth
will synthesize a CloudFormation template from your CDK code. If you haven't worked with CDK in your account before, you need to bootstrap the required resources for the CDK with cdk bootstrap
, otherwise skip this step. Note that bootstrapping needs to be performed in every Region you plan to deploy to. You can then deploy the template with cdk deploy
.
cdk synth
# replace account id and Region codes
cdk bootstrap 123456789012/us-east-1 123456789012/eu-west-1
cdk deploy --all
Connect to the Websocket API Gateway endpoints in each Region with a Websocket client or command line tool. After connecting, send a message from one of the connected clients. Your message will be send to every other client in the same Region and all clients in other Regions.
When you are done, make sure to clean everything up.
Run the following command to shut down the resources created in this workshop.
cdk destroy --all
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.