AWS CDK Development

Infrastructure as code development using AWS CDK with patterns, references, and validation scripts for stack management.


npx degit LangbaseInc/agent-skills/aws-cdk-development my-aws-cdk

Build cloud infrastructure using AWS CDK (Cloud Development Kit) with TypeScript, Python, or other supported languages.


  • Type Safety - Full IDE support and type checking
  • Reusable Constructs - Component libraries
  • Higher-Level Abstractions - L3 constructs
  • Testing - Unit and integration tests
  • Best Practices - Built-in patterns

  • TypeScript (recommended)
  • Python
  • Java
  • C#
  • Go

Constructs

Building blocks of CDK applications:

  • L1 (CFN Resources) - Direct CloudFormation
  • L2 (Curated) - Enhanced with defaults
  • L3 (Patterns) - Complete architectures

Stacks

Units of deployment:

export class MyStack extends Stack { constructor(scope: Construct, id: string, props?: StackProps) { super(scope, id, props); // Define resources new s3.Bucket(this, 'MyBucket'); } }

Apps

Entry point:

const app = new App(); new MyStack(app, 'MyStack');

API with Lambda

const api = new apigateway.RestApi(this, 'Api'); const lambda = new lambda.Function(this, 'Handler', { runtime: lambda.Runtime.NODEJS_18_X, handler: 'index.handler', code: lambda.Code.fromAsset('lambda'), }); api.root.addMethod('GET', new apigateway.LambdaIntegration(lambda));

S3 Static Website

const bucket = new s3.Bucket(this, 'Website', { websiteIndexDocument: 'index.html', publicReadAccess: true, });

DynamoDB Table

const table = new dynamodb.Table(this, 'Table', { partitionKey: { name: 'id', type: dynamodb.AttributeType.STRING }, billingMode: dynamodb.BillingMode.PAY_PER_REQUEST, });

1. Initialize Project

cdk init app --language typescript

2. Define Infrastructure

Edit stack files with resources

3. Synthesize

cdk synth

4. Validate

npm test

5. Deploy

cdk deploy

Unit Tests

test('S3 Bucket Created', () => { const stack = new MyStack(app, 'TestStack'); const template = Template.fromStack(stack); template.hasResourceProperties('AWS::S3::Bucket', { PublicAccessBlockConfiguration: { BlockPublicAcls: true } }); });

Integration Tests

Deploy to test environment and validate


  • Use L2/L3 constructs when available
  • Tag all resources
  • Enable removal policies
  • Use environment variables
  • Implement proper IAM
  • Enable encryption
  • Add CloudWatch alarms
  • Document stacks

cdk init # Initialize new project cdk synth # Synthesize CloudFormation cdk diff # Show changes cdk deploy # Deploy stack cdk destroy # Delete stack cdk bootstrap # Setup CDK in account cdk ls # List stacks

  • Least privilege IAM
  • Enable encryption
  • Use secrets manager
  • Enable logging
  • Implement guardrails
  • Scan for vulnerabilities