How to Use AWS CDK: Simple Guide to Infrastructure as Code
To use
AWS CDK, write infrastructure code in supported languages like TypeScript or Python, define your cloud resources as constructs, then deploy them using the cdk deploy command. This lets you manage AWS infrastructure with familiar programming tools and version control.Syntax
The basic syntax of AWS CDK involves importing the CDK library, defining a stack class, adding AWS resources as constructs inside the stack, and then deploying the stack.
- Import CDK modules: Load the AWS CDK libraries for your language.
- Define a stack: Create a class that extends
Stackto group resources. - Add resources: Use constructs like
BucketorFunctionto define AWS services. - Deploy: Use the
cdk deployCLI command to create or update resources in AWS.
typescript
import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; import * as s3 from 'aws-cdk-lib/aws-s3'; class MyStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); new s3.Bucket(this, 'MyBucket', { versioned: true }); } } const app = new cdk.App(); new MyStack(app, 'MyStack');
Example
This example shows how to create an S3 bucket with versioning enabled using AWS CDK in TypeScript. It demonstrates defining a stack, adding a bucket resource, and deploying it.
typescript
import * as cdk from 'aws-cdk-lib'; import { Construct } from 'constructs'; import * as s3 from 'aws-cdk-lib/aws-s3'; class VersionedBucketStack extends cdk.Stack { constructor(scope: Construct, id: string, props?: cdk.StackProps) { super(scope, id, props); new s3.Bucket(this, 'VersionedBucket', { versioned: true }); } } const app = new cdk.App(); new VersionedBucketStack(app, 'VersionedBucketStack');
Output
✨ Synthesis time: Xs
[+] Deploying stack VersionedBucketStack
[+] Successfully deployed stack VersionedBucketStack
Common Pitfalls
Common mistakes when using AWS CDK include:
- Not bootstrapping your AWS environment with
cdk bootstrapbefore deploying. - Forgetting to import required AWS service modules.
- Trying to deploy without running
cdk synthorcdk deploy. - Using mutable state inside stacks which can cause unexpected behavior.
Always run cdk bootstrap once per environment and check your imports.
bash
/* Wrong: Missing bootstrap step */ // Running 'cdk deploy' without 'cdk bootstrap' causes deployment failure. /* Right: Bootstrap before deploy */ // Run 'cdk bootstrap aws://ACCOUNT-NUMBER/REGION' once before deploying stacks.
Quick Reference
| Command | Description |
|---|---|
| cdk init app --language=typescript | Create a new CDK app in TypeScript |
| cdk synth | Generate CloudFormation template from CDK code |
| cdk deploy | Deploy the stack to AWS |
| cdk diff | Show differences between deployed stack and local code |
| cdk bootstrap | Prepare AWS environment for CDK deployments |
Key Takeaways
Write infrastructure as code using AWS CDK constructs in your preferred language.
Always run 'cdk bootstrap' once per AWS environment before deploying stacks.
Use 'cdk deploy' to create or update AWS resources defined in your CDK app.
Import required AWS service modules to avoid deployment errors.
Check your stack code with 'cdk synth' and 'cdk diff' before deploying.