AWS Serverless PoC (API Gateway + Lambda + DynamoDB)
We use the classic AWS serverless architecture to demonstrate how to build a simple chat room service.
Prerequisites
- Install and configure AWS CLI environment: Installation – Installing or updating the latest version of the AWS CLI. Configuration – Configure basic settings that AWS CLI uses to interact with AWS. NOTE: Make sure your IAM User/Role has sufficient permissions.
- Install Node Version Manager: Install NVM – Install NVM and configure your environment according to this document.
- Install Node.js:
nvm install 16.3.0
- Install AWS CDK Toolkit:
npm install -g aws-cdk
- Install Golang: Download and Install – Download and install Go quickly with the steps described here.
- Install Docker: Install Docker Engine – The installation section shows you how to install Docker on a variety of platforms.
- Make sure you also have GNU Make, jq installed.
Deployment
Run the following command to deploy AWS infra and code by CDK Toolkit:
cdk-cli-wrapper-dev.sh deploy
If all goes well, you will see the following output:
Outputs:
CdkGolangExample-ApiGtwLambdaDdb.LambdaRestApiEndpointCCECE4C1 = https://b12gqp2av5.execute-api.ap-northeast-2.amazonaws.com/dev/
Stack ARN:
arn:aws:cloudformation:ap-northeast-2:123456789012:stack/CdkGolangExample-ApiGtwLambdaDdb/225b9050-a414-11ec-b5c2-0ab842e4df54
✨ Total time: 133.05s
You can also clean up the deployment by running command:
cdk-cli-wrapper-dev.sh destroy
Testing
As you see in the output of the Deployment step, the URL is your API Gateway endpoint:
https://b12gqp2av5.execute-api.ap-northeast-2.amazonaws.com/dev/
We have integrated two Lambda functions with the following resource paths:
put-chat-records
get-chat-records
You can POST user comment by following API:
POST https://b12gqp2av5.execute-api.ap-northeast-2.amazonaws.com/dev/put-chat-records
Content-Type: application/json
x-api-key: dI65dhFd3742OmUhbdxYo4CT2eOwfoUT1FCtm8ml
Body:
{
"name" : string,
"comment" : string,
"chatRoom": string
}
Status Code: 201 Created
Or you can QUERY user comments by following API:
GET https://b12gqp2av5.execute-api.ap-northeast-2.amazonaws.com/dev/get-chat-records?chatroom=abc123
x-api-key: dI65dhFd3742OmUhbdxYo4CT2eOwfoUT1FCtm8ml
Status Code: 200 OK
[
{
"name" :string,
"comment":string,
"time" :string
},
...
]
Development
In your day-to-day development work, running Lambda functions locally can improve productivity. All scripting tools related to Lambda functions are in the ‘functions’ directory. Run the following command to build a Docker image and run the container:
./run_local_test.sh
Script for running RIE in local.
Usage:
run_local_test.sh <lambda_function_name>
Examples:
run_local_test.sh put-chat-records
Keep run_local_test.sh running and open another terminal, run the test script:
./do_local_test.sh
Script for testing lambda function in local.
Usage:
do_local_test.sh <json body>
Examples:
do_local_test.sh '{"body":"{\"name\":\"Cow\",\"comment\":\"Sample comment!\",\"chatRoom\":\"101\"}"}'
do_local_test.sh @put-chat-records/sample_body.json
do_local_test.sh '{"queryStringParameters":{"chatroom":"101"}}'
do_local_test.sh @get-chat-records/sample_query_string.json
The first two examples are for put-chat-records function, and the last two examples are for get-chat-records function. When you are done modifying the Lambda function code, you can run the following command again:
cdk-cli-wrapper-dev.sh deploy