AWS CDK Skill
Infrastructure as Code (IaC) using AWS Cloud Development Kit with TypeScript/Python for building scalable cloud applications.
When to Use This Skill
Activate this skill when the user:
- Requests AWS infrastructure setup
- Needs serverless application architecture
- Wants to define cloud resources as code
- Mentions "AWS CDK", "infrastructure as code", "CloudFormation", "serverless"
- Requires best practices for AWS resource management
- Asks about container orchestration (ECS, EKS)
- Needs API Gateway, Lambda, DynamoDB patterns
Core Capabilities
1. Common CDK Patterns
- Serverless API: API Gateway + Lambda + DynamoDB
- Static Website: S3 + CloudFront + Route53
- Container Service: ECS Fargate + ALB + RDS
- Event-Driven: EventBridge + Lambda + SQS/SNS
- Data Pipeline: S3 + Lambda + Glue + Athena
- CI/CD Pipeline: CodePipeline + CodeBuild + CodeDeploy
2. CDK Constructs
- L1 (CloudFormation): Direct CFN resources
- L2 (Curated): AWS construct library
- L3 (Patterns): High-level patterns
- Custom Constructs: Reusable components
3. Best Practices
- Multi-environment deployment (dev, staging, prod)
- Tagging and cost allocation
- Security best practices (IAM, VPC, encryption)
- Monitoring and logging (CloudWatch)
- Resource cleanup and lifecycle management
Example Patterns
Serverless API Stack
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';
import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
export class ServerlessApiStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// DynamoDB Table
const table = new dynamodb.Table(this, 'ItemsTable', {
partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
removalPolicy: cdk.RemovalPolicy.DESTROY,
});
// Lambda Function
const handler = new lambda.Function(this, 'ItemsHandler', {
runtime: lambda.Runtime.NODEJS_18_X,
code: lambda.Code.fromAsset('lambda'),
handler: 'index.handler',
environment: {
TABLE_NAME: table.tableName,
},
});
table.grantReadWriteData(handler);
// API Gateway
const api = new apigateway.RestApi(this, 'ItemsApi', {
restApiName: 'Items Service',
description: 'This service manages items.',
});
const items = api.root.addResource('items');
items.addMethod('GET', new apigateway.LambdaIntegration(handler));
items.addMethod('POST', new apigateway.LambdaIntegration(handler));
const item = items.addResource('{id}');
item.addMethod('GET', new apigateway.LambdaIntegration(handler));
item.addMethod('PUT', new apigateway.LambdaIntegration(handler));
item.addMethod('DELETE', new apigateway.LambdaIntegration(handler));
}
}
Static Website with CloudFront
import * as s3 from 'aws-cdk-lib/aws-s3';
import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';
import * as s3deploy from 'aws-cdk-lib/aws-s3-deployment';
export class StaticWebsiteStack extends cdk.Stack {
constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// S3 Bucket
const siteBucket = new s3.Bucket(this, 'SiteBucket', {
websiteIndexDocument: 'index.html',
websiteErrorDocument: 'error.html',
publicReadAccess: true,
removalPolicy: cdk.RemovalPolicy.DESTROY,
autoDeleteObjects: true,
});
// CloudFront Distribution
const distribution = new cloudfront.CloudFrontWebDistribution(this, 'SiteDistribution', {
originConfigs: [{
s3OriginSource: {
s3BucketSource: siteBucket,
},
behaviors: [{ isDefaultBehavior: true }],
}],
});
// Deploy site contents
new s3deploy.BucketDeployment(this, 'DeployWebsite', {
sources: [s3deploy.Source.asset('./website')],
destinationBucket: siteBucket,
distribution,
distributionPaths: ['/*'],
});
new cdk.CfnOutput(this, 'DistributionDomainName', {
value: distribution.distributionDomainName,
});
}
}
Best Practices
Do's
- ✅ Use typed constructs (TypeScript recommended)
- ✅ Separate stacks by lifecycle and team ownership
- ✅ Tag all resources for cost tracking
- ✅ Use environment variables for configuration
- ✅ Implement proper IAM least privilege
- ✅ Enable CloudWatch logs and metrics
- ✅ Use CDK context for environment-specific values
- ✅ Version lock your CDK dependencies
Don'ts
- ❌ Don't hardcode sensitive values (use Secrets Manager)
- ❌ Don't create circular dependencies between stacks
- ❌ Don't forget to set removal policies
- ❌ Don't ignore CDK security warnings
- ❌ Don't deploy to production without testing
Resources
1---2name: aws-cdk3description: AWS Cloud Development Kit infrastructure as code patterns and best practices for serverless, containers, and cloud-native applications4---5
6# AWS CDK Skill
7
8Infrastructure as Code (IaC) using AWS Cloud Development Kit with TypeScript/Python for building scalable cloud applications.
9
10## When to Use This Skill
11
12Activate this skill when the user:
13- Requests AWS infrastructure setup
14- Needs serverless application architecture
15- Wants to define cloud resources as code
16- Mentions "AWS CDK", "infrastructure as code", "CloudFormation", "serverless"
17- Requires best practices for AWS resource management
18- Asks about container orchestration (ECS, EKS)
19- Needs API Gateway, Lambda, DynamoDB patterns
20
21## Core Capabilities
22
23### 1. Common CDK Patterns
24- **Serverless API**: API Gateway + Lambda + DynamoDB
25- **Static Website**: S3 + CloudFront + Route53
26- **Container Service**: ECS Fargate + ALB + RDS
27- **Event-Driven**: EventBridge + Lambda + SQS/SNS
28- **Data Pipeline**: S3 + Lambda + Glue + Athena
29- **CI/CD Pipeline**: CodePipeline + CodeBuild + CodeDeploy
30
31### 2. CDK Constructs
32- **L1 (CloudFormation)**: Direct CFN resources
33- **L2 (Curated)**: AWS construct library
34- **L3 (Patterns)**: High-level patterns
35- **Custom Constructs**: Reusable components
36
37### 3. Best Practices
38- Multi-environment deployment (dev, staging, prod)
39- Tagging and cost allocation
40- Security best practices (IAM, VPC, encryption)
41- Monitoring and logging (CloudWatch)
42- Resource cleanup and lifecycle management
43
44## Example Patterns
45
46### Serverless API Stack
47```typescript
48import * as cdk from 'aws-cdk-lib';
49import * as lambda from 'aws-cdk-lib/aws-lambda';
50import * as apigateway from 'aws-cdk-lib/aws-apigateway';
51import * as dynamodb from 'aws-cdk-lib/aws-dynamodb';
52
53export class ServerlessApiStack extends cdk.Stack {
54 constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
55 super(scope, id, props);
56
57 // DynamoDB Table
58 const table = new dynamodb.Table(this, 'ItemsTable', {
59 partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING },
60 billingMode: dynamodb.BillingMode.PAY_PER_REQUEST,
61 removalPolicy: cdk.RemovalPolicy.DESTROY,
62 });
63
64 // Lambda Function
65 const handler = new lambda.Function(this, 'ItemsHandler', {
66 runtime: lambda.Runtime.NODEJS_18_X,
67 code: lambda.Code.fromAsset('lambda'),
68 handler: 'index.handler',
69 environment: {
70 TABLE_NAME: table.tableName,
71 },
72 });
73
74 table.grantReadWriteData(handler);
75
76 // API Gateway
77 const api = new apigateway.RestApi(this, 'ItemsApi', {
78 restApiName: 'Items Service',
79 description: 'This service manages items.',
80 });
81
82 const items = api.root.addResource('items');
83 items.addMethod('GET', new apigateway.LambdaIntegration(handler));
84 items.addMethod('POST', new apigateway.LambdaIntegration(handler));
85
86 const item = items.addResource('{id}');
87 item.addMethod('GET', new apigateway.LambdaIntegration(handler));
88 item.addMethod('PUT', new apigateway.LambdaIntegration(handler));
89 item.addMethod('DELETE', new apigateway.LambdaIntegration(handler));
90 }
91}
92```
93
94### Static Website with CloudFront
95```typescript
96import * as s3 from 'aws-cdk-lib/aws-s3';
97import * as cloudfront from 'aws-cdk-lib/aws-cloudfront';
98import * as s3deploy from 'aws-cdk-lib/aws-s3-deployment';
99
100export class StaticWebsiteStack extends cdk.Stack {
101 constructor(scope: cdk.App, id: string, props?: cdk.StackProps) {
102 super(scope, id, props);
103
104 // S3 Bucket
105 const siteBucket = new s3.Bucket(this, 'SiteBucket', {
106 websiteIndexDocument: 'index.html',
107 websiteErrorDocument: 'error.html',
108 publicReadAccess: true,
109 removalPolicy: cdk.RemovalPolicy.DESTROY,
110 autoDeleteObjects: true,
111 });
112
113 // CloudFront Distribution
114 const distribution = new cloudfront.CloudFrontWebDistribution(this, 'SiteDistribution', {
115 originConfigs: [{
116 s3OriginSource: {
117 s3BucketSource: siteBucket,
118 },
119 behaviors: [{ isDefaultBehavior: true }],
120 }],
121 });
122
123 // Deploy site contents
124 new s3deploy.BucketDeployment(this, 'DeployWebsite', {
125 sources: [s3deploy.Source.asset('./website')],
126 destinationBucket: siteBucket,
127 distribution,
128 distributionPaths: ['/*'],
129 });
130
131 new cdk.CfnOutput(this, 'DistributionDomainName', {
132 value: distribution.distributionDomainName,
133 });
134 }
135}
136```
137
138## Best Practices
139
140### Do's
141- ✅ Use typed constructs (TypeScript recommended)
142- ✅ Separate stacks by lifecycle and team ownership
143- ✅ Tag all resources for cost tracking
144- ✅ Use environment variables for configuration
145- ✅ Implement proper IAM least privilege
146- ✅ Enable CloudWatch logs and metrics
147- ✅ Use CDK context for environment-specific values
148- ✅ Version lock your CDK dependencies
149
150### Don'ts
151- ❌ Don't hardcode sensitive values (use Secrets Manager)
152- ❌ Don't create circular dependencies between stacks
153- ❌ Don't forget to set removal policies
154- ❌ Don't ignore CDK security warnings
155- ❌ Don't deploy to production without testing
156
157## Resources
158
159- AWS CDK Docs: https://docs.aws.amazon.com/cdk/
160- CDK Patterns: https://cdkpatterns.com/
161- AWS Construct Library: https://docs.aws.amazon.com/cdk/api/v2/