Bewährte CDK Patterns für Serverless Architekturen
Nicolai Lang
Principal Cloud Architect & Founder of WABI Engineering. Specialized in Serverless and AWS CDK.
Serverless-Architekturen auf AWS bieten enorme Vorteile: keine Server-Verwaltung, automatische Skalierung und ein Pay-per-Use-Modell. Doch mit wachsender Komplexität braucht es klare Patterns, um die Infrastruktur wartbar und konsistent zu halten. In diesem Artikel zeige ich bewährte CDK Patterns, die sich in der Praxis bewiesen haben.
Architektur-Überblick
Eine typische Serverless-Architektur auf AWS besteht aus wenigen, gut abgestimmten Services:
Das Single-Table Design Pattern
DynamoDB entfaltet seine volle Stärke mit dem Single-Table Design. Statt für jede Entität eine eigene Tabelle anzulegen, werden alle Daten in einer Tabelle mit durchdachtem Key-Schema gespeichert.
import { Table, AttributeType, BillingMode } from "aws-cdk-lib/aws-dynamodb";
const table = new Table(this, "MainTable", {
partitionKey: { name: "PK", type: AttributeType.STRING },
sortKey: { name: "SK", type: AttributeType.STRING },
billingMode: BillingMode.PAY_PER_REQUEST,
pointInTimeRecoveryEnabled: true,
});
// GSI für invertierte Abfragen
table.addGlobalSecondaryIndex({
indexName: "GSI1",
partitionKey: { name: "GSI1PK", type: AttributeType.STRING },
sortKey: { name: "GSI1SK", type: AttributeType.STRING },
});
Vorteile
- Ein einziger API-Call für komplexe Abfragen über mehrere Entitäten
- Konsistente Skalierung — eine Tabelle, ein Kapazitätsmodell
- Einfacheres Backup & Recovery mit Point-in-Time Recovery
Lambda Function Construct Pattern
Ein wiederverwendbares CDK Construct für Lambda Functions eliminiert Boilerplate und erzwingt Best Practices:
import { Runtime, Tracing } from "aws-cdk-lib/aws-lambda";
import { NodejsFunction } from "aws-cdk-lib/aws-lambda-nodejs";
import { Duration } from "aws-cdk-lib";
import { Construct } from "constructs";
interface ServiceFunctionProps {
entry: string;
handler?: string;
environment?: Record<string, string>;
timeout?: Duration;
memorySize?: number;
}
export class ServiceFunction extends Construct {
public readonly function: NodejsFunction;
constructor(scope: Construct, id: string, props: ServiceFunctionProps) {
super(scope, id);
this.function = new NodejsFunction(this, "Handler", {
entry: props.entry,
handler: props.handler ?? "handler",
runtime: Runtime.NODEJS_22_X,
timeout: props.timeout ?? Duration.seconds(30),
memorySize: props.memorySize ?? 256,
tracing: Tracing.ACTIVE,
environment: {
NODE_OPTIONS: "--enable-source-maps",
...props.environment,
},
bundling: {
minify: true,
sourceMap: true,
target: "node22",
},
});
}
}
API Gateway + Lambda Integration
Die Integration von API Gateway mit Lambda über CDK lässt sich elegant mit dem LambdaIntegration-Pattern lösen:
import { RestApi, LambdaIntegration } from "aws-cdk-lib/aws-apigateway";
const api = new RestApi(this, "Api", {
restApiName: "service-api",
deployOptions: {
stageName: "prod",
tracingEnabled: true,
},
});
const items = api.root.addResource("items");
items.addMethod("GET", new LambdaIntegration(listFunction.function));
items.addMethod("POST", new LambdaIntegration(createFunction.function));
const item = items.addResource("{id}");
item.addMethod("GET", new LambdaIntegration(getFunction.function));
Fazit
CDK Patterns helfen dabei, Serverless-Architekturen konsistent, wartbar und sicher zu gestalten. Die drei wichtigsten Takeaways:
- Single-Table Design für DynamoDB — weniger Tabellen, mehr Effizienz
- Wiederverwendbare Constructs — Boilerplate eliminieren und Best Practices erzwingen
- Infrastructure as Code — jede Änderung ist nachvollziehbar, reviewbar und reproduzierbar
Im nächsten Artikel schauen wir uns an, wie man mit CDK Pipelines ein vollautomatisiertes Deployment aufbaut.
