AWS CDK — Define Cloud Infrastructure Using Real Programming Languages
The AWS Cloud Development Kit lets you define cloud infrastructure in TypeScript, Python, Java, Go, or C# instead of YAML templates. CDK synthesizes your code into CloudFormation and deploys it with a single command.
Instalación lista para agent
Este activo puede instalarse después de elegir el runtime, revisar el plan y ejecutar el comando correspondiente.
npx -y tokrepo@latest install b9fb8a8c-398f-11f1-9bc6-00163e2b0d79 --target codexEjecutar después de confirmar el plan con dry-run.
What it is
The AWS Cloud Development Kit (CDK) lets you define cloud infrastructure using familiar programming languages instead of YAML or JSON templates. Write TypeScript, Python, Java, Go, or C# code that describes your AWS resources, and CDK synthesizes it into CloudFormation templates and deploys them.
CDK provides high-level constructs that bundle multiple AWS resources with best-practice defaults. A single L2 construct can create a Lambda function with its IAM role, log group, and API Gateway integration.
How it saves time or tokens
CloudFormation YAML is verbose and error-prone. A Lambda function with API Gateway can take 100+ lines of YAML. The same setup in CDK takes 10 lines of TypeScript with sensible defaults for IAM, logging, and error handling.
For AI code generation, CDK's type-safe interfaces mean LLMs can produce correct infrastructure code with autocomplete-friendly APIs. TypeScript CDK code benefits from full type checking before deployment.
How to use
- Install CDK and create a project:
npm install -g aws-cdk
cdk init app --language typescript
- Define infrastructure in code:
import * as cdk from 'aws-cdk-lib';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as apigateway from 'aws-cdk-lib/aws-apigateway';
export class MyApiStack extends cdk.Stack {
constructor(scope: cdk.App, id: string) {
super(scope, id);
const fn = new lambda.Function(this, 'Handler', {
runtime: lambda.Runtime.PYTHON_3_12,
code: lambda.Code.fromAsset('lambda'),
handler: 'index.handler',
});
new apigateway.LambdaRestApi(this, 'Api', { handler: fn });
}
}
- Deploy:
cdk deploy
- CDK synthesizes CloudFormation, creates the stack, and provisions all resources.
Example
// CDK creates IAM role, log group, and API Gateway automatically
const fn = new lambda.Function(this, 'Processor', {
runtime: lambda.Runtime.NODEJS_20_X,
code: lambda.Code.fromAsset('src'),
handler: 'processor.handler',
timeout: cdk.Duration.seconds(30),
memorySize: 256,
});
// Grant the function read access to an S3 bucket
bucket.grantRead(fn);
Related on TokRepo
- AI Tools for DevOps — Infrastructure-as-code and deployment tools
- AI Tools for Coding — AI-assisted infrastructure development
Common pitfalls
- Not running cdk diff before cdk deploy. Always review the change set to understand what resources will be created, modified, or destroyed.
- Using L1 constructs when L2 exists. L1 constructs are raw CloudFormation mappings. L2 constructs provide higher-level abstractions with best-practice defaults. Always check for L2 first.
- Ignoring CDK context and environment separation. Use cdk.json context and environment-specific stacks to manage dev, staging, and production deployments cleanly.
- Failing to review community discussions and changelogs before upgrading. Breaking changes in major versions can disrupt existing workflows. Pin versions in production and test upgrades in staging first.
Preguntas frecuentes
CDK uses real programming languages with loops, conditions, and abstractions. Terraform uses HCL, a domain-specific language. CDK is AWS-only (though CDK for Terraform exists). Terraform supports all major cloud providers. CDK generates CloudFormation; Terraform has its own state management.
Yes. CDK supports Python as a first-class language. Install aws-cdk-lib via pip and write stacks in Python. The API is identical in structure to TypeScript, with Python naming conventions.
Constructs are the building blocks of CDK. L1 constructs map 1:1 to CloudFormation resources. L2 constructs provide higher-level abstractions with defaults. L3 constructs (patterns) bundle multiple resources for common architectures like API + Lambda + DynamoDB.
Yes. CDK generates least-privilege IAM policies automatically when you use grant methods (bucket.grantRead, table.grantWriteData). You do not need to write IAM policy JSON manually for most use cases.
CDK can import existing resources into a stack using from* methods (like Bucket.fromBucketName). However, CDK cannot modify resources it did not create unless you explicitly import them into the stack.
Referencias (3)
- AWS CDK GitHub— AWS CDK defines cloud infrastructure in programming languages
- AWS CDK Documentation— AWS CDK documentation and construct library
- CloudFormation Documentation— AWS CloudFormation infrastructure provisioning
Relacionados en TokRepo
Discusión
Activos relacionados
CDKTF — Define Terraform Infrastructure with TypeScript, Python, Go or Java
CDK for Terraform (CDKTF) lets you define cloud infrastructure using familiar programming languages instead of HCL, then synthesizes standard Terraform JSON for plan and apply.
cdk8s — Define Kubernetes Manifests Using Real Programming Languages
cdk8s (Cloud Development Kit for Kubernetes) lets you define Kubernetes resources using TypeScript, Python, Java, or Go, generating standard YAML manifests from code.
Prowler — Cloud Security Assessment for AWS, Azure and GCP
Prowler is an open-source security tool that audits your cloud infrastructure against hundreds of compliance checks for AWS, Azure, GCP, and Kubernetes, generating actionable reports.
Cloud Nuke — Wipe AWS Resources with a Single Command
A CLI tool from Gruntwork for cleaning up AWS accounts by deleting all resources across regions. Cloud Nuke is essential for tearing down sandbox environments, reducing cloud costs, and preventing resource sprawl.