DEV Community

Alex Spinov
Alex Spinov

Posted on

AWS CDK Has a Free API: Define Cloud Infrastructure in TypeScript Instead of YAML

AWS Cloud Development Kit (CDK) lets you define AWS infrastructure using TypeScript, Python, Java, or C# — with auto-complete, type checking, and reusable components instead of writing CloudFormation YAML.

Why AWS CDK Matters

CloudFormation templates are verbose, error-prone, and lack IDE support. A simple Lambda + API Gateway setup takes 200+ lines of YAML. In CDK, it takes 15 lines of TypeScript with full type safety.

What you get for free:

  • Define infrastructure in real programming languages (TS, Python, Java, C#, Go)
  • IDE auto-complete for every AWS service and property
  • High-level constructs: new LambdaRestApi() creates API Gateway + Lambda + IAM in one line
  • Synthesizes to CloudFormation — full compatibility with existing AWS tooling
  • Built-in best practices (encryption, logging, least-privilege IAM)
  • CDK Pipelines: self-mutating CI/CD for your infrastructure

Quick Start

# Install
npm install -g aws-cdk

# Create project
mkdir my-stack && cd my-stack
cdk init app --language typescript

# Deploy
cdk deploy

# Diff before deploying
cdk diff

# Destroy
cdk destroy
Enter fullscreen mode Exit fullscreen mode

Lambda + API Gateway (15 Lines)

import * as cdk from "aws-cdk-lib";
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as apigw from "aws-cdk-lib/aws-apigateway";

export class ApiStack extends cdk.Stack {
  constructor(scope: cdk.App, id: string) {
    super(scope, id);

    const handler = new lambda.Function(this, "Handler", {
      runtime: lambda.Runtime.NODEJS_20_X,
      code: lambda.Code.fromAsset("lambda"),
      handler: "index.handler",
      memorySize: 256,
      timeout: cdk.Duration.seconds(30),
    });

    new apigw.LambdaRestApi(this, "Api", { handler });
    // That is it. API Gateway + Lambda + IAM roles — all created.
  }
}
Enter fullscreen mode Exit fullscreen mode

DynamoDB + Lambda + SQS Pipeline

import * as dynamodb from "aws-cdk-lib/aws-dynamodb";
import * as sqs from "aws-cdk-lib/aws-sqs";
import * as lambdaEvents from "aws-cdk-lib/aws-lambda-event-sources";

// DynamoDB table
const table = new dynamodb.Table(this, "Orders", {
  partitionKey: { name: "orderId", type: dynamodb.AttributeType.STRING },
  sortKey: { name: "createdAt", type: dynamodb.AttributeType.NUMBER },
  billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
  removalPolicy: cdk.RemovalPolicy.DESTROY,
});

// Dead letter queue
const dlq = new sqs.Queue(this, "DLQ", {
  retentionPeriod: cdk.Duration.days(14),
});

// Processing queue
const queue = new sqs.Queue(this, "OrderQueue", {
  visibilityTimeout: cdk.Duration.seconds(300),
  deadLetterQueue: { queue: dlq, maxReceiveCount: 3 },
});

// Processor Lambda
const processor = new lambda.Function(this, "Processor", {
  runtime: lambda.Runtime.NODEJS_20_X,
  code: lambda.Code.fromAsset("lambda/processor"),
  handler: "index.handler",
  environment: { TABLE_NAME: table.tableName },
});

// Wire everything together
table.grantReadWriteData(processor);
processor.addEventSource(new lambdaEvents.SqsEventSource(queue));
Enter fullscreen mode Exit fullscreen mode

Reusable Constructs

import { Construct } from "constructs";

export class SecureApi extends Construct {
  public readonly endpoint: string;

  constructor(scope: Construct, id: string, props: { handler: lambda.Function }) {
    super(scope, id);

    const api = new apigw.LambdaRestApi(this, "Api", {
      handler: props.handler,
      proxy: false,
      deployOptions: {
        stageName: "prod",
        throttlingRateLimit: 1000,
        throttlingBurstLimit: 500,
      },
    });

    // Add WAF, logging, custom domain — once, reuse everywhere
    this.endpoint = api.url;
  }
}

// Usage: one line in any stack
new SecureApi(this, "MyApi", { handler: myLambda });
Enter fullscreen mode Exit fullscreen mode

Useful Links


Building cloud-native data pipelines? Check out my developer tools on Apify for ready-made web scrapers, or email spinov001@gmail.com for custom solutions.

Top comments (0)