Skip to content

create and deploy a serverless application with AWS CDK in 10 mins

Notifications You must be signed in to change notification settings

bobbyquennell/serverless-the-cdk-way

Repository files navigation

serverless-with-cdk

create and deploy a simple serverless application within 10 mins

Prerequisites

  • Node.js (v18 or higher)
  • yarn
  • an AWS account
  • install AWS SAM CLI by running:
    brew tap aws/tap
    brew install aws-sam-cli

Infrastructure Tree

we organize infra into a tree - the construct tree, which will make our infra resources "loosely coupled" between services and make them easy to maintain and read.

Composition is the killer feature of IAC

  • reusable components can implement best practices and share it within an organisation/team/company, or publicly
  • higher-level abstractions are composed from many lower-level constructs, and those lower-level constructs can be composed from even lower-level constructs #iac-composition

3 Layers of the tree

  • App is the root of the tree, a top layer construct which represents the entire application.
  • the APP is a container of one or more Stacks. Each stack represents a deployable Cloudformation Stack template. A stack defines a collection of AWS resources that you can manage as a single unit:(create, update, delete)
  • Constructs L0-3 are the basic building blocks of AWS CDK apps. Most often used constructs are Level2 and Level3
graph TD
    App[App] --> s1
    App --> s2
    subgraph CDK Constructs
        s2r1[lambda function]
    end
    subgraph CDK Constructs
        s1r1[event bus]
        s1r2[event bus log group]
    end
    subgraph CDK Stacks
        s1(Stack 1: shared Infra)
        s2(Stack 2: service A)
    end
    s1 --> s1r1
    s1 --> s1r2
    s2 --> s2r1
Loading

note:

Setup

  1. Install dependencies:

    yarn install
  2. Config Environment Variables

    After cloning the repository, create a .env.local file based on the provided example:

    cp .env.local.example .env.local

    Then, fill in the necessary values in the .env.local file.

Development and Deploy

  1. if you run CDK deploy locally, modify the .env.local to set the target account and region explicitly

  2. run cdk diff before you deploy to AWS this cmd will output the difference between the already deployed CloudFormation template and the CloudFormation template equivalent of our current CDK code.

  3. if you are happy the result from cdk diff, run cdk deploy to deploy the resources

Since this app includes more than a single stack, specify which stacks to use (wildcards are supported) or specify --all

Local Invocation of Lambda Functions

Ensure you have a default testEvent.json in src/scripts/. This file should contain a sample event that your Lambda function expects.

Creating the Default Test Event

  1. Navigate to the src/scripts/ directory.
  2. Create a file named testEvent.json.
  3. Populate it with a sample event structure. For example:

About

create and deploy a serverless application with AWS CDK in 10 mins

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published