|
1 | | -# Friend Microservices Sample |
| 1 | +# Serverless Microservices with Amazon API Gateway, DynamoDB, SQS, and Lambda |
2 | 2 |
|
3 | | -This CDK project is a sample solution for friend microservices in games.\ |
4 | | -The sample solution contains AWS Lambda Functions, Amazon DynamoDB Tables, Amazon SQS Queues and Amazon API Gateway. |
| 3 | +| Key | Value | |
| 4 | +| ------------ | ---------------------------------------------------------------------------------------------------- | |
| 5 | +| Environment | <img src="https://img.shields.io/badge/LocalStack-deploys-4D29B4.svg?logo=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAEAAAABACAYAAACqaXHeAAAABHNCSVQICAgIfAhkiAAAAAlwSFlzAAAKgAAACoABZrFArwAAABl0RVh0U29mdHdhcmUAd3d3Lmlua3NjYXBlLm9yZ5vuPBoAAALbSURBVHic7ZpNaxNRFIafczNTGIq0G2M7pXWRlRv3Lusf8AMFEQT3guDWhX9BcC/uFAr1B4igLgSF4EYDtsuQ3M5GYrTaj3Tmui2SpMnM3PlK3m1uzjnPw8xw50MoaNrttl+r1e4CNRv1jTG/+v3+c8dG8TSilHoAPLZVX0RYWlraUbYaJI2IuLZ7KKUWCisgq8wF5D1A3rF+EQyCYPHo6Ghh3BrP8wb1en3f9izDYlVAp9O5EkXRB8dxxl7QBoNBpLW+7fv+a5vzDIvVU0BELhpjJrmaK2NMw+YsIxunUaTZbLrdbveZ1vpmGvWyTOJToNlsuqurq1vAdWPMeSDzwzhJEh0Bp+FTmifzxBZQBXiIKaAq8BBDQJXgYUoBVYOHKQRUER4mFFBVeJhAQJXh4QwBVYeHMQJmAR5GCJgVeBgiYJbg4T8BswYPp+4GW63WwvLy8hZwLcd5TudvBj3+OFBIeA4PD596nvc1iiIrD21qtdr+ysrKR8cY42itCwUP0Gg0+sC27T5qb2/vMunB/0ipTmZxfN//orW+BCwmrGV6vd63BP9P2j9WxGbxbrd7B3g14fLfwFsROUlzBmNM33XdR6Meuxfp5eg54IYxJvXCx8fHL4F3w36blTdDI4/0WREwMnMBeQ+Qd+YC8h4g78wF5D1A3rEqwBiT6q4ubpRSI+ewuhP0PO/NwcHBExHJZZ8PICI/e73ep7z6zzNPwWP1djhuOp3OfRG5kLROFEXv19fXP49bU6TbYQDa7XZDRF6kUUtEtoFb49YUbh/gOM7YbwqnyG4URQ/PWlQ4ASllNwzDzY2NDX3WwioKmBgeqidgKnioloCp4aE6AmLBQzUExIaH8gtIBA/lFrCTFB7KK2AnDMOrSeGhnAJSg4fyCUgVHsolIHV4KI8AK/BQDgHW4KH4AqzCQwEfiIRheKKUAvjuuu7m2tpakPdMmcYYI1rre0EQ1LPo9w82qyNziMdZ3AAAAABJRU5ErkJggg=="> <img src="https://img.shields.io/badge/AWS-deploys-F29100.svg?logo=amazon"> | |
| 6 | +| Services | API Gateway, DynamoDB, SQS, Lambda | |
| 7 | +| Integrations | CDK | |
| 8 | +| Categories | Serverless; Microservices | |
| 9 | +| Level | Beginner | |
| 10 | +| GitHub | [Repository link](https://github.com/localstack/microservices-apigateway-lambda-dynamodb-sqs-sample) | |
5 | 11 |
|
6 | | -## Key features |
| 12 | +## Introduction |
7 | 13 |
|
8 | | -- the solution is constructed with only serverless services |
9 | | -- the solution handles friend state management asynchronously |
10 | | -- the solution utilizes DynamoDB Streams with Event Source Mapping Filters to reduce number of transactional writes |
11 | | -- the solution has an SQS queue in front, so multiple backend services can send friend actions |
12 | | -- the solution is decoupled from player management, it simply serves friend state management |
13 | | -- the solution handles duplicated messages from SQS using Event Source Mapping Filters |
| 14 | +The Serverless microservices application sample demonstrates how you can build and deploy a solution for friend microservices in gaming applications using API Gateway, Lambda, DynamoDB, and SQS. This application sample allows you to handle friend state management asynchronously and utilizes DynamoDB Streams with Event Source Mapping Filters to reduce the number of transactional writes. With an SQS queue, multiple backend services can send friend actions and handle duplicated messages using Event Source Mapping Filters. An API Gateway and a Lambda Function have been implemented to read the data and DynamoDB for data persistence. The sample is decoupled from player management and only serves friend state management. Users can deploy the infrastructure with AWS Cloud Development Kit, and we will demonstrate how you use LocalStack to deploy the infrastructure on your developer machine and your CI environment. |
14 | 15 |
|
15 | | -## Assumptions |
| 16 | +## Architecture diagram |
16 | 17 |
|
17 | | -- another service, such as a notification service, has bidirectional connection with clients |
18 | | -- player metadata is stored separately with all player IDs matched |
| 18 | + |
19 | 19 |
|
20 | | -## Architecture |
| 20 | +We are using the following AWS services and their features to build our infrastructure: |
21 | 21 |
|
22 | | -As Shown in the diagram below, there are three Lambda functions (though State Handlers are made of four lambda functions), a single SQS queues in front and a DynamoDB table.\ |
23 | | -`Front Queue` intakes all friend actions from game backend services\ |
24 | | -Each `State Handler` handles different state, and triggered though different Event Source Mapping Filters\ |
25 | | -There also is an API Gateway and a Lambda Function for reading data\ |
26 | | - |
| 22 | +- [Lambda](https://docs.localstack.cloud/user-guide/aws/lambda/) to create the serverless functions for the Create, State, and Read handlers. |
| 23 | +- [SQS](https://docs.localstack.cloud/user-guide/aws/sqs/) as a distributed message queuing service to intakes all friend actions from game backend services. |
| 24 | +- [DynamoDB](https://docs.localstack.cloud/user-guide/aws/dynamodb/) as a key-value and document database to persist data with Event Source Mapping Filters to reduce number of transactional writes. |
| 25 | +- [API Gateway](https://docs.localstack.cloud/user-guide/aws/apigatewayv2/) to expose and allow the Lambda functions to read data through HTTP APIs. |
27 | 26 |
|
28 | | -## DynamoDB Tables |
| 27 | +## Prerequisites |
29 | 28 |
|
30 | | -### Friend Table |
| 29 | +- LocalStack Pro |
| 30 | +- [AWS CLI](https://docs.localstack.cloud/user-guide/integrations/aws-cli/) with the [`awslocal`](https://github.com/localstack/awscli-local) wrapper. |
| 31 | +- [CDK](https://docs.localstack.cloud/user-guide/integrations/aws-cdk/) with the [`cdklocal`](https://github.com/localstack/aws-cdk-local) wrapper. |
| 32 | +- [NodeJS v18.0.0](https://nodejs.org/en/download/) with `npm` package manager. |
31 | 33 |
|
32 | | -Each entry represents friend state from the perspective of a player with the player ID against a friend with the friend ID. |
| 34 | +Start LocalStack Pro by setting your `LOCALSTACK_API_KEY` to activate the Pro features. |
33 | 35 |
|
34 | | -| PK: player_id | SK: friend_id | state | last_updated | |
35 | | -| ----------------- | ----------------- | -------------------- | ------------ | |
36 | | -| string: player ID | string: friend ID | string: friend state | time stamp | |
37 | | - |
38 | | -## Friend States |
| 36 | +```shell |
| 37 | +export LOCALSTACK_API_KEY=<your-api-key> |
| 38 | +EXTRA_CORS_ALLOWED_ORIGINS=* localstack start -d |
| 39 | +``` |
39 | 40 |
|
40 | | -- Requested |
41 | | - - State that the requester sees on his/her entry after sending a friend request |
42 | | -- Pending |
43 | | - - State that the receiver sees on his/her entry after receiving a friend request |
44 | | -- Friends |
45 | | - - State that both players see on their entries once they become friends |
| 41 | +## Instructions |
46 | 42 |
|
47 | | -## Friend Actions |
| 43 | +You can build and deploy the sample application on LocalStack by running our `Makefile` commands. To deploy the infrastructure, you can run `make deploy` after installing the application dependencies. Here are instructions to deploy and test it manually step-by-step. |
48 | 44 |
|
49 | | -- Request |
50 | | - - initial action to send a friend request to another player |
51 | | -- Accept |
52 | | - - an action to accept a pending friend request from another player |
53 | | - - this can only be triggered by the receiver |
54 | | -- Reject |
55 | | - - an action to reject a pending friend request from another player |
56 | | - - this can only be triggered by the receiver |
57 | | -- Unfriend |
58 | | - - an action to break off a completed friend relationship between two players |
59 | | - - this can be triggered by both players |
| 45 | +### Creating the infrastructure |
60 | 46 |
|
61 | | -## Friend Action Message |
| 47 | +To create the AWS infrastructure locally, you can use CDK and our `cdklocal` wrapper. Before you can deploy the infrastructure, you need to install the application dependencies: |
62 | 48 |
|
63 | | -``` |
64 | | -{ |
65 | | - "player_id": string, |
66 | | - "friend_id": string, |
67 | | - "friend_action": Friend Action, |
68 | | -} |
| 49 | +```shell |
| 50 | +yarn |
69 | 51 | ``` |
70 | 52 |
|
71 | | -### Prerequisites |
| 53 | +To deploy the infrastructure, you can run the following command: |
72 | 54 |
|
73 | | -- An AWS account |
74 | | -- Nodejs LTS installed, such as 14.x |
75 | | -- Install Docker Engine |
| 55 | +```shell |
| 56 | +cdklocal bootstrap aws://000000000000/us-east-1 |
| 57 | +cdklocal deploy |
| 58 | +``` |
76 | 59 |
|
77 | | -## Usage |
| 60 | +As an output of the last command, you will see the API Gateway endpoint URL. You can use this URL to test the API. |
78 | 61 |
|
79 | | -### Deployment |
| 62 | +### Testing the microservice |
80 | 63 |
|
81 | | -To deploy the example stack to your default AWS account/region, under project root folder, run: |
| 64 | +To test the microservice, we will send Friend Action Events to the front SQS queue. We will use the AWS CLI to send the events to the queue. To get the Queue URL, you can run the following command: |
82 | 65 |
|
83 | | -1. `yarn install` to install all the dependencies |
84 | | -2. `cdk deploy` to deploy this stack to your default AWS account/region |
| 66 | +```shell |
| 67 | +awslocal sqs list-queues |
| 68 | +``` |
85 | 69 |
|
86 | | -## Test |
| 70 | +Get the URL of the Front Queue and use the following commands to send a friend request event: |
87 | 71 |
|
88 | | -### Send Test Friend Action Events |
| 72 | +```shell |
| 73 | +awslocal sqs send-message-batch --queue-url <QUEUE_URL> --entries file://test/testMessagesFirst.json |
| 74 | +awslocal sqs send-message-batch --queue-url <QUEUE_URL> --entries file://test/testMessagesSecond.json |
| 75 | +awslocal sqs send-message-batch --queue-url <QUEUE_URL> --entries file://test/testMessagesThird.json |
| 76 | +``` |
89 | 77 |
|
90 | | -Since front SQS is Standard queue, you need to send test messages three times separately, to get to the final state. |
91 | | -With AWS CLI: |
| 78 | +To test corner cases, you can send the following messages to the queue: |
92 | 79 |
|
93 | | -``` |
94 | | -aws sqs send-message-batch --queue-url <QUEUE URL> \ |
95 | | - --entries file://test/testMessages<First|Second|Third>.json |
| 80 | +```shell |
| 81 | +awslocal sqs send-message-batch --queue-url <QUEUE_URL> --entries file://test/cornerCase1.json |
| 82 | +awslocal sqs send-message-batch --queue-url <QUEUE_URL> --entries file://test/cornerCase2.json |
96 | 83 | ``` |
97 | 84 |
|
98 | | -If you want to test corner cases, try this: |
| 85 | +To test the microservice now, send the following command using `cURL`: |
99 | 86 |
|
| 87 | +```shell |
| 88 | +curl -X GET 'https://<LOCAL_APIGATEWAY_ENDPOINT>/friends/player1' |
| 89 | +curl -X GET 'https://<LOCAL_APIGATEWAY_ENDPOINT>/friends/player2' |
| 90 | +curl -X GET 'https://<LOCAL_APIGATEWAY_ENDPOINT>/friends/player3' |
100 | 91 | ``` |
101 | | -aws sqs send-message-batch --queue-url <QUEUE URL> \ |
102 | | - --entries file://test/cornerCase<1|2>.json |
103 | | -``` |
104 | | - |
105 | | -### Get Test Friend Data |
106 | 92 |
|
107 | | -With `curl`: |
| 93 | +To run the unit tests, you can run the following command: |
108 | 94 |
|
| 95 | +```shell |
| 96 | +yarn test |
109 | 97 | ``` |
110 | | -$ curl -X GET 'https://<YOUR ENDPOINT>/friends/player1' |
111 | | -``` |
112 | 98 |
|
113 | | -## License |
| 99 | +## GitHub Action |
| 100 | + |
| 101 | +This application sample hosts an example GitHub Action workflow that starts up LocalStack, deploys the infrastructure, and checks the created resources using `awslocal`. You can find the workflow in the `.github/workflows/main.yml` file. To run the workflow, you can fork this repository and push a commit to the `main` branch. |
114 | 102 |
|
115 | | -This solution is licensed under the MIT-0 License. See the LICENSE file. |
| 103 | +Users can adapt this example workflow to run in their own CI environment. LocalStack supports various CI environments, including GitHub Actions, CircleCI, Jenkins, Travis CI, and more. You can find more information about the CI integration in the [LocalStack documentation](https://docs.localstack.cloud/user-guide/ci/). |
116 | 104 |
|
117 | | -Also, this application uses below open source project, |
| 105 | +## Learn more |
118 | 106 |
|
119 | | -- [aigle](https://www.npmjs.com/package/aigle) |
| 107 | +The sample application is based on a [public AWS sample app](https://github.com/aws-samples/friend-microservices) that deploys a friend microservice for gaming applications. |
0 commit comments