CDK is a software development framework for defining your cloud infrastructure in a high-level programming language such as Typescript, Python, JavaScript, and Java. AWS CDK eliminates the abstraction of manually provisioning AWS resources. With AWS CDK, you can deploy and provision hundreds of CloudFormation templates from your source code using any programming language. For example, If you like to build an application that exposes a REST API and triggers the request from AWS Lambda, you can build the entire layers of every resource using AWS CDK. This process lets you simplify resources and deploy applications using the CDK patterns directly from your CLI.

In this tutorial, you’ll learn how to:

  • Set up and code your CDK project
  • Deploy your CDK application using the CDK command to CloudFormation
  • View resources in AWS Console.

Prerequisites

  1. Create a sample application folder in your terminal using mkdir sample-app

2. Initialize your CDk sample-app using cdk init app –language typescript

3. When it’s done, open your VS Code to see the details of your CDK project. You can see that all your dependencies and packages are installed.

  • In your directory above, you can see your jest.config.js file, tsconfig.json file, and all your dependencies and packages.

4. In your sample-app-stack.ts you can see your sample app configuration. Construct are the basic building blocks of your CDK app. They represent a cloud component like EC2, SNS, SQS, SES, etc. it encapsulates all the components required for cloud formation to create the resource for you.

  • In your lib folder create your lambda.ts file for your lambda functions.
import { Architecture, Runtime } from "aws-cdk-lib/aws-lambda";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { RetentionDays } from "aws-cdk-lib/aws-logs";
import { Construct } from "constructs";
import * as path from "path"


export class Lambda extends NodejsFunction {
   constructor(scope: Construct, fileName: string) {
       super(scope, fileName, {
           architecture: Architecture.ARM_64,
           runtime: Runtime.NODEJS_14_X,
           entry: path.join(__dirname,`../backend/lambda/${fileName}.ts`),
           logRetention: RetentionDays.ONE_DAY
       })
   }
}

  • Also, create the file for your ApiGateway.
import { RemovalPolicy } from "aws-cdk-lib";
import { LambdaIntegration, LogGroupLogDestination, RestApi } from "aws-cdk-lib/aws-apigateway";
import { IFunction } from "aws-cdk-lib/aws-lambda";
import { LogGroup, RetentionDays } from "aws-cdk-lib/aws-logs";
import { Construct } from "constructs";


export class ApiGateway extends RestApi {
   constructor(scope: Construct) {
       super(scope, "ApiGateway", {
           restApiName: 'todo-app',
           deployOptions: {
               accessLogDestination: new LogGroupLogDestination(new LogGroup(scope, "ApiLogGroup", {
                   logGroupName: "api_gateway",
                   retention: RetentionDays.ONE_DAY,
                   removalPolicy: RemovalPolicy.DESTROY
               }))
           }
       })
   }


   addIntegration(method: string, path: string, lambda: IFunction) {
       const resource = this.root.resourceForPath(path);
       resource.addMethod(method, new LambdaIntegration(lambda))
   }
}

  • Install all the types in your lambda function using npm install @types/aws-lambda

5. Next, create a proxy.ts file for your workflow.

import { APIGatewayEvent, APIGatewayProxyResult } from "aws-lambda";


export const handler = async (event: APIGatewayEvent): Promise<APIGatewayProxyResult> => {
   console.log("proxy lambda executed", event);
  
   return {
       body: "OK",
       statusCode: 200
   }
}

6. Next, deploy your application and execute it using cdk synth; this causes your resources defined in it to be translated into an AWS CloudFormation template.

  • In your terminal, you can see all the auto-generate cloudformation template that will be created in your terminal.

7. Provision your resources for your AWS CDK application using cdk bootstrap before you can finally deploy it in your AWS environment.

8. Next, deploy your applications using cdk deploy

  • In your terminal, cdk will list out all the IAM functions it’ll create for you and your team.

  • In your AWS console, you’ll see that your lambda function, APi gateway, will be created with your proxy service.

  • Back in your API Gateway route, you can see your proxy route defined to be deployed.

  • Back in your AWS lambda dashboard, you can see that your App is created and triggered using the API Gateway you deployed in your code.

I'd like to build something with AWS CDK + CTO.ai!

Heck yeah! Here's our opensource workflow to try this out yourself. There are so many things that you can do with AWS CDK on CTO.ai; sign up for free to define your infrastructure with code, rollback changes, configure repeatable deployments, and drift provisioning directly on CTO.ai developer control plane.