CDK8s Component¶
The CDK8s Component allows you to integrate CDK for Kubernetes (CDK8s) into any AWS CDK TypeScript application, enabling you to define Kubernetes manifests using familiar TypeScript constructs alongside your AWS infrastructure.
Overview¶
CDK8s is a software development framework for defining Kubernetes applications and reusable abstractions using familiar programming languages. This component seamlessly integrates CDK8s into your existing CDK projects, allowing you to:
- Define Kubernetes manifests in TypeScript alongside your AWS resources
- Share code and constructs between your AWS and Kubernetes infrastructure
- Synthesize Kubernetes YAML from TypeScript code
- Import existing Kubernetes resources and CRDs
- Leverage the power of programming for Kubernetes configuration
Universal Compatibility
This component works with any AWS CDK TypeScript application, not just those created with our project types. You can add it to existing CDK apps, apps created with standard projen, or any other CDK project setup.
Key Features¶
🔧 Automatic Setup¶
- Installs required CDK8s dependencies (
cdk8s,cdk8s-cli,cdk8s-plus-XX) - Configures TypeScript compilation for Kubernetes manifests
- Sets up proper project structure for CDK8s development
⚙️ Task Automation¶
cdk8s:import: Import Kubernetes APIs and CRDscdk8s:synth: Synthesize Kubernetes manifests from TypeScriptcdk8s: Run any CDK8s command with arguments
🎯 Flexible Configuration¶
- Support for multiple Kubernetes versions (1.29 to 1.33)
- Custom import paths for external resources
- Configurable output and source directories
- Integration with existing project structure
📁 Project Structure Integration¶
- Generates sample application code
- Creates proper
cdk8s.yamlconfiguration - Maintains separation between AWS and Kubernetes code
- Supports both library and application patterns
Quick Start¶
Adding to an Existing CDK App¶
To add the CDK8s component to any existing AWS CDK TypeScript application:
Highlighted lines explanation:
- Line 2: Import the CDK8s component
- Line 12: Add the component to your project with default settings
Adding to Our CDK App Projects¶
For projects created with our CDK App project type:
Highlighted lines explanation:
- Line 2: Import both the CDK App project and CDK8s component
- Line 11: Add CDK8s functionality to your CDK app
First Steps After Setup¶
After adding the component and running yarn projen:
Configuration Options¶
Basic Configuration¶
The CDK8s component accepts the following configuration options:
Kubernetes Version Support¶
Choose from supported Kubernetes versions:
Custom Paths and Structure¶
Organize your CDK8s code with custom paths:
| Custom Project Structure | |
|---|---|
Highlighted lines explanation:
- Line 3: Put CDK8s code in
k8s/directory instead ofsrc/ - Line 4: Use
cluster.tsas the main application file - Line 5: Output manifests to
manifests/directory
Usage Examples¶
Example 1: Basic Kubernetes Application¶
After adding the component, create a simple Kubernetes application:
Highlighted lines explanation:
- Lines 8-13: Define Kubernetes resources using imported constructs
- Resources are defined in TypeScript with full IDE support and type safety
Example 2: AWS + Kubernetes Hybrid Application¶
Combine AWS resources with Kubernetes manifests:
Highlighted lines explanation:
- Lines 15-18: Create an EKS cluster that can run the Kubernetes manifests generated by CDK8s
Then deploy your CDK8s manifests to the cluster:
Example 3: Using External Kubernetes Resources¶
Import and use external Kubernetes resources like operators:
| Using External Resources | |
|---|---|
Highlighted lines explanation:
- Lines 5-7: Import specific versions of popular Kubernetes operators and tools
After running yarn cdk8s:import, you can use these resources:
| Using Imported Operators | |
|---|---|
Project Structure¶
After adding the CDK8s component, your project structure will look like:
my-cdk-app/
├── lib/ # AWS CDK stacks
│ ├── my-stack.ts
│ └── ...
├── src/ # CDK8s application (configurable)
│ ├── main.ts # CDK8s main app
│ └── imports/ # Auto-generated Kubernetes imports
│ ├── k8s.ts # Core Kubernetes API
│ ├── prometheus-operator.ts # External imports
│ └── ...
├── kubernetes/ # Generated manifests (configurable)
│ ├── my-chart.k8s.yaml
│ └── ...
├── test/
│ ├── *.test.ts # AWS CDK tests
│ └── main.test.ts # CDK8s tests (for cdk8s projects)
├── cdk8s.yaml # CDK8s configuration
├── package.json # Updated with CDK8s dependencies
└── ...
Separation of Concerns
Keep your AWS CDK stacks in lib/ and your Kubernetes definitions in src/ (or your configured appPath). This maintains clear separation between cloud infrastructure and Kubernetes workloads.
Available Tasks¶
The component automatically adds these tasks to your project:
yarn cdk8s:import¶
Import Kubernetes APIs and external resources:
yarn cdk8s:synth¶
Synthesize Kubernetes manifests from TypeScript:
yarn cdk8s¶
Run any CDK8s command:
Best Practices¶
1. Version Consistency¶
Keep Kubernetes versions consistent between your EKS cluster and CDK8s configuration:
// In your CDK stack
const cluster = new eks.Cluster(this, 'Cluster', {
version: eks.KubernetesVersion.V1_30, // Match this version...
});
// In your CDK8s component
new Cdk8sComponent(project, 'cdk8s', {
k8sVersion: K8sVersion.V1_30, // ...with this version
});
2. Namespace Organization¶
Use namespaces to organize your Kubernetes resources:
// Create environment-specific namespaces
new KubeNamespace(this, 'production', {
metadata: { name: 'production' }
});
new KubeNamespace(this, 'staging', {
metadata: { name: 'staging' }
});
3. Resource Management¶
Define resource requests and limits:
containers: [{
name: 'app',
image: 'my-app:latest',
resources: {
requests: { memory: '128Mi', cpu: '100m' },
limits: { memory: '256Mi', cpu: '200m' }
}
}]
4. Configuration Management¶
Use ConfigMaps and Secrets for application configuration:
// Configuration
new KubeConfigMap(this, 'app-config', {
metadata: { name: 'app-config' },
data: {
'database-url': 'postgres://...',
'api-endpoint': 'https://api.example.com'
}
});
// Secrets (values should be base64 encoded)
new KubeSecret(this, 'app-secrets', {
metadata: { name: 'app-secrets' },
data: {
'database-password': Buffer.from('secret').toString('base64')
}
});
5. CI/CD Integration¶
Integrate CDK8s synthesis into your CI/CD pipeline:
name: Deploy
on:
push:
branches: [main]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: actions/setup-node@v4
with:
node-version: '18'
# Build and deploy AWS resources
- run: yarn install
- run: yarn build
- run: npx cdk deploy --require-approval never
# Generate and apply Kubernetes manifests
- run: yarn cdk8s:synth
- name: Apply manifests
run: |
aws eks update-kubeconfig --name $CLUSTER_NAME
kubectl apply -f kubernetes/
Deployment Workflows¶
Local Development¶
- Setup: Add component and run
yarn projen - Import: Run
yarn cdk8s:importfor Kubernetes APIs - Develop: Write CDK8s code in TypeScript
- Synthesize: Run
yarn cdk8s:synthto generate manifests - Deploy: Apply manifests with
kubectl apply -f kubernetes/
Production Deployment¶
- AWS Infrastructure: Deploy CDK stacks first (
npx cdk deploy) - Cluster Access: Configure kubectl for your EKS cluster
- Manifest Generation: Run
yarn cdk8s:synthin CI/CD - Kubernetes Deployment: Apply manifests automatically or via GitOps
GitOps Integration¶
For GitOps workflows with tools like ArgoCD or Flux:
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: my-app
spec:
source:
repoURL: https://github.com/myorg/my-app
path: kubernetes/ # CDK8s output directory
targetRevision: HEAD
destination:
server: https://kubernetes.default.svc
namespace: default
Troubleshooting¶
Import Issues¶
If cdk8s:import fails:
# Clear import cache
rm -rf src/imports
yarn cdk8s:import
# Check CDK8s version compatibility
yarn list cdk8s cdk8s-cli
Synthesis Errors¶
For TypeScript compilation errors:
# Check TypeScript configuration
npx tsc --noEmit
# Verify CDK8s configuration
cat cdk8s.yaml
# Run synthesis with verbose output
yarn cdk8s:synth -- --verbose
Deployment Issues¶
For kubectl deployment problems:
# Validate generated manifests
kubectl apply --dry-run=client -f kubernetes/
# Check cluster connectivity
kubectl cluster-info
# Verify namespace existence
kubectl get namespaces
Version Mismatches¶
For compatibility issues:
# Check your cluster's Kubernetes version
kubectl version
# Update CDK8s component to match
# Edit .projenrc.ts and set matching k8sVersion
yarn projen
Advanced Configuration¶
Custom CDK8s Configuration¶
Override the generated cdk8s.yaml:
| Advanced cdk8s.yaml Configuration | |
|---|---|
Multiple Chart Applications¶
Structure for multiple Kubernetes applications:
| Multi-Chart Structure | |
|---|---|
Environment-Specific Configuration¶
Use CDK context for environment-specific Kubernetes configuration:
| Environment Configuration | |
|---|---|
Migration Guide¶
From Helm Charts¶
To migrate from Helm charts to CDK8s:
- Analyze existing Helm templates and values
- Convert YAML resources to CDK8s constructs
- Replace Helm values with TypeScript variables
- Test generated manifests match original output
From Raw YAML¶
To convert existing Kubernetes YAML:
- Import existing resources:
kubectl get -o yaml > existing.yaml - Convert manually or use tools like
cdk8s import - Refactor into reusable constructs
- Validate output matches original resources
Contributing¶
To contribute improvements to the CDK8s component:
- Fork the repository
- Add tests for new functionality
- Update documentation
- Submit a pull request
Support¶
For help with the CDK8s component:
- Documentation: Check this guide and CDK8s docs
- Issues: Create a GitHub issue with detailed information
- Community: Join CDK8s community discussions
- Examples: Check the project's example directory
The CDK8s component is maintained by Jump to the Cloud team and leverages the open-source CDK8s project.