Skip to content

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

License

Notifications You must be signed in to change notification settings

aws-samples/aws-serverless-using-aws-cdk

AWS Serverless using AWS CDK

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:

Contents

  1. Repository structure

  2. Solution coverage

  3. Solution architecture

  4. Quick Guide

  5. About CDK-Project

  6. How to deploy

  7. How to test

  8. How to clean up

  9. Security

  10. 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-demo.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:

Quick Guide

Just execute the following commands step by step for quick practice.

vim config/app-config-demo.json # change Account/Region
sh script/deploy_stacks.sh      # deploy a stack
sh script/request_api.sh        # invoke APIGateway(empty book list)
sh script/publish_sns.sh        # add a book through SNS
sh script/request_api.sh        # invoke APIGateway(a book in list)
sh script/upload_s3.sh          # add 3 books through S3
sh script/request_api.sh        # invoke APIGateway(4 books in list)
sh script/destroy_stacks.sh     # destroy stacks

About CDK-Project

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.

CDK specific file

The cdk.json file tells the CDK Toolkit how to execute your app.

CDK Commands

And the more useful 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 deploy

Caution: This solution contains not-free tier AWS services. So be careful about the possible costs. Fortunately, serverless services minimize cost if not used.

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-demo.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 command 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 a stack using AWS CDK. Execute the following command which will deploy the stack and create a cdk-output.json file in script directory, which includes deployment result outouts. For details, please check deploy_stacks.sh file.

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-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"
    }
  ]
}

How to clean up

Execute the following command, which will destroy all resources including S3 Buckets and DynamoDB Tables. For details, please check destroy_stacks.sh file.

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.