0
0
AwsHow-ToBeginner · 4 min read

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 Stack to group resources.
  • Add resources: Use constructs like Bucket or Function to define AWS services.
  • Deploy: Use the cdk deploy CLI 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 bootstrap before deploying.
  • Forgetting to import required AWS service modules.
  • Trying to deploy without running cdk synth or cdk 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

CommandDescription
cdk init app --language=typescriptCreate a new CDK app in TypeScript
cdk synthGenerate CloudFormation template from CDK code
cdk deployDeploy the stack to AWS
cdk diffShow differences between deployed stack and local code
cdk bootstrapPrepare 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.