Version 2 of the AWS Cloud Development Kit library for defining cloud infrastructure using familiar programming languages
npx @tessl/cli install tessl/npm-aws-cdk-lib@2.214.0The AWS CDK (Cloud Development Kit) library is a comprehensive framework for defining cloud infrastructure using familiar programming languages. The aws-cdk-lib package serves as the complete construct library for CDK v2, providing high-level constructs and APIs for virtually all AWS services including compute, storage, networking, databases, and security services.
npm install aws-cdk-lib constructsimport { App, Stack, StackProps } from "aws-cdk-lib";
import { Construct } from "constructs";For specific AWS services:
import { aws_s3 as s3, aws_lambda as lambda, aws_ec2 as ec2 } from "aws-cdk-lib";Alternative service import syntax:
import * as s3 from "aws-cdk-lib/aws-s3";
import * as lambda from "aws-cdk-lib/aws-lambda";
import * as ec2 from "aws-cdk-lib/aws-ec2";CommonJS:
const { App, Stack } = require("aws-cdk-lib");
const s3 = require("aws-cdk-lib/aws-s3");import { App, Stack, StackProps, RemovalPolicy } from "aws-cdk-lib";
import { aws_s3 as s3, aws_lambda as lambda } from "aws-cdk-lib";
import { Construct } from "constructs";
export class MyStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
// Create an S3 bucket
const bucket = new s3.Bucket(this, "MyBucket", {
versioned: true,
removalPolicy: RemovalPolicy.DESTROY,
});
// Create a Lambda function
const fn = new lambda.Function(this, "MyFunction", {
runtime: lambda.Runtime.NODEJS_20_X,
handler: "index.handler",
code: lambda.Code.fromInline(`
exports.handler = async function(event) {
return { statusCode: 200, body: 'Hello CDK!' };
};
`),
});
// Grant the Lambda function read access to the bucket
bucket.grantRead(fn);
}
}
const app = new App();
new MyStack(app, "MyStack");The AWS CDK library is built around several key architectural components:
App, Stack, Stage) that form the foundation of CDK applicationsFundamental CDK constructs and utilities that form the foundation of all CDK applications. Essential for creating apps, stacks, and managing resources.
class App extends Construct {
constructor(props?: AppProps);
synth(): CloudAssembly;
}
class Stack extends Construct {
constructor(scope: Construct, id: string, props?: StackProps);
readonly account: string;
readonly region: string;
readonly stackName: string;
}
class Stage extends Construct {
constructor(scope: Construct, id: string, props?: StageProps);
readonly account: string;
readonly region: string;
}AWS storage services for data persistence, content delivery, and backup solutions. Includes S3, EFS, FSx, and related storage utilities.
// S3 - Simple Storage Service
class Bucket extends Resource implements IBucket {
constructor(scope: Construct, id: string, props?: BucketProps);
grantRead(identity: IGrantable): Grant;
grantWrite(identity: IGrantable): Grant;
readonly bucketName: string;
readonly bucketArn: string;
}
// EFS - Elastic File System
class FileSystem extends Resource implements IFileSystem {
constructor(scope: Construct, id: string, props?: FileSystemProps);
readonly fileSystemId: string;
readonly fileSystemArn: string;
}AWS compute services for running applications, functions, and workloads. Covers Lambda, EC2, ECS, EKS, and containerization.
// Lambda Functions
class Function extends Resource implements IFunction {
constructor(scope: Construct, id: string, props: FunctionProps);
grantInvoke(identity: IGrantable): Grant;
readonly functionName: string;
readonly functionArn: string;
}
// EC2 Infrastructure
class Vpc extends Resource implements IVpc {
constructor(scope: Construct, id: string, props?: VpcProps);
readonly vpcId: string;
readonly availabilityZones: string[];
}
class SecurityGroup extends Resource implements ISecurityGroup {
constructor(scope: Construct, id: string, props: SecurityGroupProps);
addIngressRule(peer: IPeer, connection: Port): void;
}AWS database services for relational, NoSQL, and specialized data storage needs. Includes RDS, DynamoDB, ElastiCache, and more.
// DynamoDB Tables
class Table extends Resource implements ITable {
constructor(scope: Construct, id: string, props: TableProps);
grantReadData(identity: IGrantable): Grant;
grantWriteData(identity: IGrantable): Grant;
readonly tableName: string;
readonly tableArn: string;
}
// RDS Databases
class DatabaseInstance extends Resource implements IDatabaseInstance {
constructor(scope: Construct, id: string, props: DatabaseInstanceProps);
readonly instanceIdentifier: string;
readonly instanceEndpoint: Endpoint;
}AWS security and identity services for authentication, authorization, and access control. Covers IAM, KMS, Secrets Manager, and Cognito.
// IAM Roles and Policies
class Role extends Resource implements IRole {
constructor(scope: Construct, id: string, props: RoleProps);
addToPolicy(statement: PolicyStatement): boolean;
grantPassRole(identity: IGrantable): Grant;
readonly roleArn: string;
readonly roleName: string;
}
// KMS Keys
class Key extends Resource implements IKey {
constructor(scope: Construct, id: string, props?: KeyProps);
grantEncryptDecrypt(identity: IGrantable): Grant;
readonly keyId: string;
readonly keyArn: string;
}AWS networking services for connectivity, load balancing, and content distribution. Includes CloudFront, Route 53, Load Balancers, and API Gateway.
// CloudFront Distributions
class Distribution extends Resource implements IDistribution {
constructor(scope: Construct, id: string, props: DistributionProps);
readonly distributionId: string;
readonly distributionDomainName: string;
}
// Application Load Balancers
class ApplicationLoadBalancer extends BaseLoadBalancer {
constructor(scope: Construct, id: string, props: ApplicationLoadBalancerProps);
addListener(id: string, props: BaseApplicationListenerProps): ApplicationListener;
}
// API Gateway
class RestApi extends Resource implements IRestApi {
constructor(scope: Construct, id: string, props?: RestApiProps);
readonly restApiId: string;
readonly restApiName: string;
}AWS developer tools for CI/CD, source control, and application lifecycle management. Includes CodePipeline, CodeBuild, and CodeDeploy.
// CodePipeline
class Pipeline extends Resource {
constructor(scope: Construct, id: string, props: PipelineProps);
addStage(props: StageProps): IStage;
readonly pipelineName: string;
readonly pipelineArn: string;
}
// CodeBuild Projects
class Project extends Resource implements IProject {
constructor(scope: Construct, id: string, props: ProjectProps);
readonly projectName: string;
readonly projectArn: string;
}AWS monitoring and management services for observability, logging, and resource governance. Covers CloudWatch, CloudTrail, and Systems Manager.
// CloudWatch Alarms
class Alarm extends Resource implements IAlarm {
constructor(scope: Construct, id: string, props: AlarmProps);
readonly alarmArn: string;
readonly alarmName: string;
}
// Log Groups
class LogGroup extends Resource implements ILogGroup {
constructor(scope: Construct, id: string, props?: LogGroupProps);
grantWrite(identity: IGrantable): Grant;
readonly logGroupName: string;
readonly logGroupArn: string;
}AWS services for connecting and coordinating distributed applications. Includes SNS, SQS, EventBridge, and Step Functions.
// SNS Topics
class Topic extends Resource implements ITopic {
constructor(scope: Construct, id: string, props?: TopicProps);
addSubscription(subscription: ITopicSubscription): void;
grantPublish(identity: IGrantable): Grant;
readonly topicArn: string;
readonly topicName: string;
}
// SQS Queues
class Queue extends Resource implements IQueue {
constructor(scope: Construct, id: string, props?: QueueProps);
grantConsumeMessages(identity: IGrantable): Grant;
grantSendMessages(identity: IGrantable): Grant;
readonly queueArn: string;
readonly queueName: string;
}CDK testing framework and utility modules for validating infrastructure code and managing deployment processes.
// Template Testing
class Template {
static fromStack(stack: Stack): Template;
hasResourceProperties(type: string, props: any): void;
resourceCountIs(type: string, count: number): void;
}
// Pipelines for CI/CD
class CodePipeline extends PipelineBase {
constructor(scope: Construct, id: string, props: CodePipelineProps);
addStage(stage: StageOptions): StageDeployment;
}interface AppProps {
readonly analyticsReporting?: boolean;
readonly autoSynth?: boolean;
readonly context?: { [key: string]: any };
readonly outdir?: string;
readonly stackTraces?: boolean;
readonly treeMetadata?: boolean;
}
interface StackProps {
readonly analyticsReporting?: boolean;
readonly crossRegionReferences?: boolean;
readonly description?: string;
readonly env?: Environment;
readonly stackName?: string;
readonly synthesizer?: IStackSynthesizer;
readonly tags?: { [key: string]: string };
readonly terminationProtection?: boolean;
}
interface StageProps {
readonly env?: Environment;
readonly outdir?: string;
readonly permissionsBoundary?: PermissionsBoundary;
readonly policyValidationBeta1?: IPolicyValidationPluginBeta1[];
readonly stageName?: string;
}
interface Environment {
readonly account?: string;
readonly region?: string;
}
enum RemovalPolicy {
DESTROY = "destroy",
RETAIN = "retain",
SNAPSHOT = "snapshot",
RETAIN_ON_UPDATE_OR_DELETE = "retain-on-update-or-delete"
}
interface Duration {
static days(amount: number): Duration;
static hours(amount: number): Duration;
static minutes(amount: number): Duration;
static seconds(amount: number): Duration;
static millis(amount: number): Duration;
static parse(duration: string): Duration;
toSeconds(opts?: TimeConversionOptions): number;
toMilliseconds(opts?: TimeConversionOptions): number;
toMinutes(opts?: TimeConversionOptions): number;
toHours(opts?: TimeConversionOptions): number;
toDays(opts?: TimeConversionOptions): number;
toIsoString(): string;
plus(rhs: Duration): Duration;
minus(rhs: Duration): Duration;
}
interface Size {
static bytes(amount: number): Size;
static kibibytes(amount: number): Size;
static mebibytes(amount: number): Size;
static gibibytes(amount: number): Size;
static tebibytes(amount: number): Size;
static pebibytes(amount: number): Size;
toBytes(opts?: SizeConversionOptions): number;
toKibibytes(opts?: SizeConversionOptions): number;
toMebibytes(opts?: SizeConversionOptions): number;
toGibibytes(opts?: SizeConversionOptions): number;
toTebibytes(opts?: SizeConversionOptions): number;
toPebibytes(opts?: SizeConversionOptions): number;
}
interface TimeConversionOptions {
readonly integral?: boolean;
}
interface SizeConversionOptions {
readonly rounding?: SizeRoundingBehavior;
}
enum SizeRoundingBehavior {
FAIL = "fail",
FLOOR = "floor"
}