Skip to content

haithai91/aws-serverless-using-aws-cdk

 
 

Repository files navigation

AWS Serverless using AWS CDK

This repository provides the basic patterns of AWS Serverless using AWS CDK.

Contents

  1. Repository structure

  2. Solution coverage

  3. Solution architecture

  4. How to deploy

  5. How to test

  6. About CDK-Project

  7. How to clean up

  8. Security

  9. License

Repository structure

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.

ProjectStructure

Solution coverage

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

Solution architecture

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)

SolutionArchitecture

AWS services used are as follows:

How to deploy

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 .

AWSCDKIntro

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(Amazon SageMaker's Endpoint is not serverless).

Prerequisites

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.

How to set up

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-1",
        "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  

How to provision

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

How to test

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-1-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
...
...
[{"isbn": "isbn-01", "title": "book-01"}, {"isbn": "isbn-03", "title": "book-03"}, {"isbn": "isbn-02", "title": "book-02"}, {"isbn": "isbn-04", "title": "book-04"}]

About CDK-Project

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 Stacks
  • cdk deploy deploy this stack to your default AWS account/region
  • cdk diff compare deployed stack with current state
  • cdk synth emits the synthesized CloudFormation template
  • cdk destroy remove resources

How to clean up

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

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

About

No description, website, or topics provided.

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • TypeScript 44.4%
  • Shell 25.6%
  • Python 25.0%
  • JavaScript 5.0%