CDK8s Library Project¶
The CDK8s Library Project type provides an opinionated setup for creating AWS CDK construct libraries with CDK8s integration for Kubernetes deployment, combining best practices and common configurations.
Overview¶
This project type extends the standard AwsCdkConstructLibrary from projen with additional CDK8s features:
- CDK8s integration for Kubernetes manifest generation
- Multi-version Kubernetes support (v1.29 to v1.33)
- Prettier configuration and VSCode settings - see Default Configurations
- Common project structure and tooling setup
- Configurable CDK8s options for custom workflows
Quick Start¶
To create a new CDK8s Library project, use the projen CLI:
This command will create a new project with the CDK8s Library template and prompt you for the required configuration options such as:
- Project name
- Author information
- Repository URL
- CDK version
- Kubernetes version
- Default branch
After the project is created, you can customize it further by editing the .projenrc.ts file:
Highlighted lines explanation:
- Line 4: Choose a descriptive name for your CDK8s construct library
- Line 8: Pin to a specific CDK version for consistency
- Line 13: Set the Kubernetes version for CDK8s imports
- Line 14: Configure custom path for Kubernetes source code
- Line 15: Set output directory for generated manifests
- Line 16: Add custom Kubernetes imports
Features¶
CDK8s Integration¶
The CDK8s Library project includes full CDK8s integration with:
- Automatic CDK8s setup with configurable Kubernetes versions
- Import generation from Kubernetes APIs and custom resources
- TypeScript-first Kubernetes manifest authoring
- CDK8s tasks for building and synthesizing manifests
Kubernetes Version Support¶
Choose from multiple supported Kubernetes versions:
| Version | CDK8s Plus Package | Description |
|---|---|---|
| v1.29 | cdk8s-plus-29 |
Kubernetes 1.29 API support |
| v1.30 | cdk8s-plus-30 |
Kubernetes 1.30 API support (default) |
| v1.31 | cdk8s-plus-31 |
Kubernetes 1.31 API support |
| v1.32 | cdk8s-plus-32 |
Kubernetes 1.32 API support |
| v1.33 | cdk8s-plus-33 |
Kubernetes 1.33 API support |
Common Configurations¶
This project type includes the same common configurations as other project types:
Default Configurations
For complete details about Prettier and VSCode configurations, see Default Configurations. This includes:
- Prettier Configuration - Code formatting rules and customization
- VSCode Configuration - Editor settings and recommended extensions
Configuration Options¶
Basic Configuration¶
All standard AwsCdkConstructLibraryOptions are supported, plus CDK8s-specific options:
CDK8s-Specific Options¶
Kubernetes Version¶
| Kubernetes Version Configuration | |
|---|---|
Custom Paths¶
Configure where CDK8s files are located:
| Custom Path Configuration | |
|---|---|
Custom Imports¶
Add additional Kubernetes resource imports:
| Custom Imports Configuration | |
|---|---|
Common Configuration Options¶
For Prettier and VSCode configuration options, refer to Default Configurations:
- Prettier: Customization Guide
- VSCode: Configuration Options
Examples¶
Basic CDK8s Library¶
Simple setup with default configurations:
Advanced CDK8s Library¶
Customized setup with specific Kubernetes version and custom paths:
Highlighted lines explanation:
- Line 12: Use Kubernetes v1.31 APIs
- Line 13: Place CDK8s source files in
src/kubernetes/ - Line 14: Main entry file for CDK8s application
- Line 15: Output generated manifests to
dist/manifests/ - Lines 16-19: Import cert-manager and Prometheus operator CRDs
Enterprise Setup¶
Example with minimal prettier configuration and disabled VSCode:
Highlighted lines explanation:
- Line 11: Use latest supported Kubernetes version
- Line 15: Disable VSCode configuration for enterprise environment
- Lines 16-21: Custom prettier settings with longer lines and double quotes
Project Structure¶
After running projen synth, your CDK8s library project will have the following structure:
my-cdk8s-library/
├── src/
│ ├── main.ts # Main CDK8s application entry point
│ └── index.ts # Library exports
├── test/ # Test files
├── kubernetes/ # Generated Kubernetes manifests (after synth)
├── cdk8s.yaml # CDK8s configuration
├── package.json # Dependencies including CDK8s packages
├── .prettierrc.json # Code formatting configuration
├── .vscode/ # VSCode workspace settings
│ ├── settings.json
│ └── extensions.json
└── .projenrc.ts # Projen configuration
Generated CDK8s Files¶
src/main.ts: Sample CDK8s application with basic Kubernetes resourcescdk8s.yaml: CDK8s configuration specifying app entry point and output locationkubernetes/: Directory where synthesized YAML manifests are generated
CDK8s Tasks¶
The project includes these npm scripts for CDK8s workflows:
| Script | Command | Description |
|---|---|---|
cdk8s |
cdk8s synth |
Generate Kubernetes manifests |
cdk8s:import |
cdk8s import |
Import Kubernetes APIs and CRDs |
cdk8s:synth |
cdk8s synth |
Alias for cdk8s command |
Best Practices¶
Development Workflow¶
- Define your constructs in TypeScript using CDK8s APIs
- Import CRDs for custom resources:
npm run cdk8s:import - Generate manifests regularly:
npm run cdk8s - Test generated YAML with your Kubernetes cluster
- Version your library using standard npm/projen publishing
Resource Organization¶
Testing Strategy¶
- Unit test your CDK8s constructs with Jest
- Snapshot test generated manifests
- Integration test with real Kubernetes clusters
- Validate YAML output with kubeval or similar tools
Troubleshooting¶
Common Issues¶
Import Errors¶
If you encounter CDK8s import errors:
# Reimport Kubernetes APIs
npm run cdk8s:import
# Check your cdk8s.yaml configuration
cat cdk8s.yaml
Version Mismatches¶
Ensure your Kubernetes version matches your cluster:
Path Issues¶
Verify your CDK8s paths are correctly configured:
Getting Help¶
- CDK8s Documentation: https://cdk8s.io/
- Project Issues: GitHub Issues
- AWS CDK: https://docs.aws.amazon.com/cdk/
Migration Guide¶
From Standard CDK Library¶
To migrate from a standard CDK library to CDK8s library:
-
Update your
.projenrc.ts: -
Add CDK8s configuration:
-
Run projen to regenerate:
From Plain CDK8s¶
To adopt this project type from a plain CDK8s project:
-
Install the package:
-
Create
.projenrc.tswith your current configuration - Run projen to apply the project template
- Review and commit the generated configuration files
Contributing¶
Contributions to improve the CDK8s Library project type are welcome! Please check the repository for details on:
- Reporting bugs
- Suggesting enhancements
- Submitting pull requests
- Development workflow
Support¶
For support with the CDK8s Library project type:
- Repository: GitHub Repository
- Issues: GitHub Issues
- Discussions: GitHub Discussions