Migrating from Serverless Framework to AWS CDK: Part 2 - Setting Up Your CDK Environment
Learn how to structure a CDK project for serverless applications, configure TypeScript for Lambda development, and establish patterns that ease migration from Serverless Framework.
When teams decide to migrate from Serverless Framework to CDK, the immediate question becomes: where to start? Working with Lambda functions across multiple environments requires careful planning, especially when coordinating between multiple developers.
Scaling CDK from personal projects to production systems presents unique challenges. Teams need structure, conventions, and patterns that work for everyone involved.
This guide covers designing a CDK project structure that allows multiple engineers to work in parallel without conflicts, maintains familiar patterns from Serverless Framework, and serves as a foundation for production platforms.
Series Navigation:
- Part 1: Why Make the Switch?
- Part 2: Setting Up Your CDK Environment (this post)
- Part 3: Migrating Lambda Functions and API Gateway
- Part 4: Database Resources and Environment Management
- Part 5: Authentication, Authorization, and IAM
- Part 6: Migration Strategies and Best Practices
The Project Structure That Actually Scales
Initial attempts often struggle with simple tutorial structures. Common issues include merge conflicts, unclear ownership, and confusion about file organization.
Here's the evolution from chaos to order:
Key insight: Domain-driven organization prevents merge conflicts when multiple engineers work in parallel.
Initializing Your CDK Project
First, ensure you have the prerequisites:
Now create your project:
Configuring TypeScript for Lambda Development
CDK generates a basic tsconfig.json. Let's optimize it for serverless development:
Environment Configuration Management
Serverless Framework uses YAML files for environment-specific configuration. Let's create a TypeScript-based equivalent:
Creating Your First Construct
Constructs are CDK's building blocks. Let's create a reusable pattern for Lambda functions:
Setting Up Your First Stack
Now let's create a stack that uses our construct:
CDK App Entry Point
Update the CDK app entry point to use our configuration system:
Your First Lambda Handler
Create a Lambda handler using TypeScript:
Deployment Commands
Add these scripts to your package.json:
First Deployment
Bootstrap your AWS environment (one-time setup that prepares your AWS account for CDK deployments by creating necessary S3 buckets and IAM roles):
Deploy to development:
CDK will show you what resources it plans to create. Review and confirm.
Local Development Setup
Unlike Serverless Framework's serverless-offline, CDK doesn't provide built-in local API Gateway emulation. For local development, you have several options:
- SAM CLI Integration (Recommended):
- Direct Handler Testing:
Key Differences to Remember
What's Next
You now have a solid CDK foundation that mirrors Serverless Framework conventions while embracing CDK's type safety and composability. Your Lambda functions live in familiar locations, but your infrastructure is now code - real, testable TypeScript code.
Related reading: For a comprehensive comparison of different CDK organization patterns (service-based vs domain-based vs feature-based), see AWS CDK Code Organization: Service-Based vs Domain-Based Architecture Patterns.
In Part 3, we'll migrate Lambda functions and API Gateway configurations, including:
- Request/response transformations
- API Gateway models and validators
- Lambda layers and dependencies
- Error handling patterns
- API versioning strategies
The foundation is set. Let's build your serverless API.
References
- docs.aws.amazon.com - AWS CDK Developer Guide.
- github.com - AWS CDK source repository and release notes.
- docs.aws.amazon.com - AWS Overview (official whitepaper).
- cloud.google.com - Google Cloud documentation.
- ietf.org - IETF RFC index (protocol standards).
- arxiv.org - arXiv software engineering recent submissions (research context).
- cheatsheetseries.owasp.org - OWASP Cheat Sheet Series (applied security guidance).
Migrating from Serverless Framework to AWS CDK
A comprehensive 6-part guide covering the complete migration process from Serverless Framework to AWS CDK, including setup, implementation patterns, and best practices.