This repository describes how to implement basic patterns of AWS Serverless using AWS CDK. AWS CDK is a great tool for serverless applications because it helps you manage multiple serverless resources in one place.
Korean practice guide and demo is provided through the following video: AWS DevAxConnect - AWS Serverless service development with AWS CDK.
This project was implemented based on AWS CDK Project Template for DevOps for more fluent DevOps application.
Other "Using AWS CDK" series can be found in:
- AWS ECS DevOps Using AWS CDK
- Amazon SageMaker Model Serving using AWS CDK
- AWS IoT Greengrass Ver2 using AWS CDK
Because this repository is basically a CDK-Project which is based on typescript, the project structure follows the basic CDK-Project form. This porject provide one stack and 3 lambdas. Before depoy this project, config/app-config.json should be filled in according to your AWS Account.
This repository introduces the common patterns of AWS Serverless.
- pattern 1: Amazon SNS -> Amazon Lambda -> Amazon DynamoDB
- pattern 2: Amazon S3 -> Amazon Lambda -> Amazon DynamoDB
- pattern 3: Amazon API Gateway -> Amazon Lambda -> Amazon DynamoDB
Specifically, it is implemented assuming that we are developing a book catalog service for easy understanding.
- flow 1: Async Single Request(Request to save one book)
- flow 2: Async Batch Request(Request to save a large of books)
- flow 3: Sync Single Request(Request for a list of books)
AWS services used are as follows:
- Amazon Lambda: a serverless computing which run code without thinking about servers
- Amazon Simple Notification Service(SNS): a fully managed messaging service for both application-to-application (A2A) and application-to-person (A2P) communication
- Amazon Simple Storage Service(S3): object storage built to store and retrieve any amount of data from anywhere
- Amazon API Gateway: a fully managed service that makes it easy for developers to create, publish, maintain, monitor, and secure APIs at any scale
- Amazon DynamoDB: a fast and flexible NoSQL database service for any scale
To efficiently define and provision serverless resources, AWS Cloud Development Kit(CDK) which is an open source software development framework to define your cloud application resources using familiar programming languages is utilized .
Because this solusion is implemented in CDK, we can deploy these cloud resources using CDK CLI. Among the various languages supported, this solution used typescript. Because the types of typescript are very strict, with the help of auto-completion, typescrip offers a very nice combination with AWS CDK.
Caution: This solution contains not-free tier AWS services. So be careful about the possible costs. Fortunately, serverless services minimize cost if not used.
First of all, AWS Account and IAM User is required. And then the following must be installed.
- AWS CLI: aws configure --profile [profile name]
- Node.js: node --version
- AWS CDK: cdk --version
- jq: jq --version
Please refer to the kind guide in CDK Workshop.
First of all, enter your project basic configuration in the follwoing document: config/app-config.json. Fill in your project's "Name", "Stage", "Account", "Region", "Profile(AWS CLI Credentials)" in "Project" according to your environments.
{
"Project": {
"Name": "ServerlessCdk",
"Stage": "Demo",
"Account": "75157*******",
"Region": "us-east-2",
"Profile": "cdk-demo"
}
}
If you don't know AWS Account/Region, execute the following commands to catch your AWS-Account.
aws sts get-caller-identity --profile [your-profile-name]
...
...
{
"Account": "[account-number]",
"UserId": "[account-id]",
"Arn": "arn:aws:iam::75157*******:user/[iam-user-id]"
}
And then execute the following commands to set up CDK-Project. For details, please check setup_initial.sh file.
sh ./script/setup_initial.sh
Let's check stack included in this CDK-Project before provisining. Execute the following command. The prefix "ServerlessCdkDemo" can be different according to your setting(Project Name/Stage).
cdk list
...
...
ServerlessCdkDemo-ServerlessStack
Now, everything is ready, let's provision all stacks using AWS CDK. Execute the following command which will deploy all stacks in order of subordination.
sh script/deploy_stacks.sh
For Async Single Request, execute the following command, which will publish SNS message(script/input_sns.json) and finally the lambda functions will be executed to save one book into DynamoDB.
sh script/publish_sns.sh
...
...
{
"MessageId": "e78906f5-4544-5e19-9191-5e9ea2a859bd"
}
After executing this command, please check your DynamoDB. You can find a new item in that.
For Async Batch Request, execute the following command, which will upload a json file(script/input_s3.json) into S3 and finally the lambda functions will be executed to save a large of books into DynamoDB.
sh script/upload_s3.sh
...
...
upload: script/input_s3.json to s3://serverlesscdkdemo-serverlessstack-us-east-2-75157/batch/input_s3.json
After executing this command, please check your DynamoDB. You can find the multiple items in that.
For Sync Single Request, execute the following command, which will send http-get request and finally the lambda functions will be executed to get a list of books in DynamoDB.
sh script/request_api.sh
...
...
{
"status": "success",
"books": [
{
"isbn": "isbn-01",
"src": "sns",
"title": "book-01"
},
{
"isbn": "isbn-03",
"src": "s3",
"title": "book-03"
},
{
"isbn": "isbn-02",
"src": "s3",
"title": "book-02"
},
{
"isbn": "isbn-04",
"src": "s3",
"title": "book-04"
}
]
}
The cdk.json
file tells the CDK Toolkit how to execute your app.
And the more usuful CDK commands are
cdk list
list up CloudFormation Stackscdk deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk synth
emits the synthesized CloudFormation templatecdk destroy
remove resources
Execute the following command, which will destroy all resources except S3 Buckets and DynamoDB Tables. So destroy these resources in AWS web console manually.
sh script/destroy_stacks.sh
See CONTRIBUTING for more information.
This library is licensed under the MIT-0 License. See the LICENSE file.