This workshop shows you how to build serverless application based on event-driven backend. You will learn how to communicate one Lambda to another in asynchronously using SQS, and how to push server response to client via Websocket API Gateway.
Deploying the CDK project provisions below infrastructure on your AWS account
This application implmented a popular game in Korea, named 13
(a.k.a Baskin Robbins).
It is a counting game where everyone(you and server) can count up to three numbers.
The goal of the game is not to count 13
. If you count 13
, you lose.
You start with one, and can stop counting one, two or three.
Then, the server will count up to three numbers starting where the you left off.
BTW, you can not win against to server by design
- NodeJs 12.x+
- awscli
- AWS Account and locally configured AWS credential
If you run this workshop on Cloud9 IDE, skip Installation section
Install NodeJs, official installation guide
Install awcli
$ pip install awscli
$ aws --version
aws-cli/1.18.181 Python/3.7.4 Darwin/19.6.0 botocore/1.19.21
Configure awscli
$ aws configure
AWS Access Key ID [****************MT6L]:
AWS Secret Access Key [****************yX2F]:
Default region name [ap-northeast-2]:
Default output format [json]:
Install project dependencies
$ cd infra
$ npm i
Install cdk
and esbuild
on global context,
$ npm i -g cdk@1.117.0 esbuild
Run cdk bootstrap
if this is first time to use CDK on your account.
And deploy infrastructure
$ cdk bootstrap
$ cdk deploy "*" --require-approval never
- Connect to websocket and get
connectionID
- Start new game sesison using invoke HTTP API Gateway at
POST /start
- Count your numbers via
POST /game
- Check out server response on websocket
- do 3~4 until you count 13.
Get endpoint url for Websocket API Gateway
$ export WS_ENDPOINT=$(aws cloudformation describe-stacks --stack-name ServerlessWorkshopInfraStack --query "Stacks[0].Outputs[?ExportName=='WsApiUrl'].OutputValue" --output text)
$ echo $WS_ENDPOINT
wss://xxx.execute-api.ap-northeast-2.amazonaws.com/dev
Install wscat
for connecting websocket endpoint
$ npm i -g wscat
Connect to endpoint using wscat.
$ wscat -c $WS_ENDPOINT
Connected (press CTRL+C to quit)
> hi
< {"message": "Forbidden", "connectionId":"XDmENcnwIE0CFEQ=", "requestId":"XDmGEE2RoE0FRdQ="}
Copy this connectionId
, in this case it's XDmENcnwIE0CFEQ=, to your clipboard.
And keep open the terminal to get response from the server on next step.
Open new terminal.
Install httpie, it's human friendly versino of curl.
# On MacOS
$ brew install httpie
# On Linux
$ sudo apt install httpie
# On Amazon Linux2
$ sudo yum install httpie
Get HTTP API Gateway url
$ export HTTP_ENDPOINT=$(aws cloudformation describe-stacks --stack-name ServerlessWorkshopInfraStack --query "Stacks[0].Outputs[?ExportName=='HttpApiUrl'].OutputValue" --output text)
$ echo $HTTP_ENDPOINT
https://xxxx.execute-api.ap-northeast-2.amazonaws.com/dev
Start game session using connectionId and arbitrary accountId.
$ export CONN_ID="CONNECTION_ID_YOU_COPIED" # e.g. XDqRFclbIE0CJPw=
$ http post $HTTP_ENDPOINT/start sessionId=$CONN_ID accountId=1
Count your numbers via POST /game
$ http post $HTTP_ENDPOINT/game sessionId=$CONN_ID accountId=1 userMove=1,2
HTTP/1.1 200 OK
Apigw-Requestid: XDqYKi2WoE0EPdg=
Connection: keep-alive
Content-Length: 45
Content-Type: text/plain; charset=utf-8
Date: Sat, 05 Dec 2020 02:31:30 GMT
{
"lastMove": 2,
"sessionId": "XDqRFclbIE0CJPw="
}
Check out websocket
> hi
< {"message": "Forbidden", "connectionId":"XDqRFclbIE0CJPw=", "requestId":"XDqROEJuoE0FpCQ="}
< {"data":"3,4"}
Continue game to the end, your websocket terminal would goes like this..
> hi
< {"message": "Forbidden", "connectionId":"XEEuXcgPoE0Acmw=", "requestId":"XEFD0E2foE0Fpgg="}
< {"data":"3,4"}
< {"data":"7,8"}
< {"data":"10,11,12"}
< {"data":"You lose.."}
Open index.html and modify wsUrl
and httpUrl
variables.
Run simple http server to serve index.html
$ cd ../public
$ python -m http.server 8080
Open http://localhost:8080
on web browser
Destroy resources from your account
$ cdk destroy "*"