DEV Community

Mikaeel Khalid for AWS Community Builders

Posted on • Originally published at blog.mikaeels.com on

What does the AWS CDK Diff command do

Image description

Introduction

The AWS Cloud Development Kit (CDK) is an open-source software development framework that simplifies the process of creating, deploying and managing AWS infrastructure. It enables developers to use familiar programming languages such as TypeScript, Python, Java, and C# to define reusable cloud components and provision them using AWS CloudFormation.

One of the commands that the AWS CDK offers is the cdk diff command. In this blog post, we'll delve into the cdk diff command, its purpose, and how it can help you manage your infrastructure with ease. We will also explore practical examples to illustrate its utilizations.

What is the CDK Diff?

The cdk diff command is a part of the AWS CDK CLI and helps developers identify the differences between the current AWS CDK application state and the deployed state in AWS CloudFormation. This comparison can be useful in various scenarios, such as validating changes before deployment, auditing infrastructure modifications, or reviewing pull requests in collaborative projects.

The command provides a clear and concise output, showing the differences between your local and deployed resources, properties, and settings.

How to use CDK Diff?

To use the cdk diff command, first, ensure that you have the AWS CDK CLI installed. If you don't have it installed, you can follow the official AWS CDK installation guide.

Once the AWS CDK CLI is installed, navigate to your CDK project directory and run the following command

cdk diff

Enter fullscreen mode Exit fullscreen mode

This command will display the differences between your local AWS CDK application and the deployed AWS CloudFormation stack. The output will show added, modified, or removed resources, as well as changes in their properties.

Examples

Let's consider a simple example to understand how the cdk diff command works. We'll use TypeScript for this example, but the concepts apply to other programming languages as well.

  1. Creating a new CDK project
cdk init app --language typescript

Enter fullscreen mode Exit fullscreen mode
  1. Adding an Amazon S3 bucket to your CDK stack

Modify the file lib/your_stack_name-stack.ts and add the following code snippet

import { Stack, StackProps} from 'aws-cdk-lib';
import { Bucket, RemovalPolicy } from 'aws-cdk-lib/aws-s3';

export class YourStackNameStack extends Stack {
  constructor(scope: cdk.App, id: string, props?: StackProps) {
    super(scope, id, props);

    new Bucket(this, 'MyFirstBucket', {
      versioned: true,
      removalPolicy: RemovalPolicy.DESTROY,
    });
  }
}

Enter fullscreen mode Exit fullscreen mode
  1. Deploy the CDK stack
cdk deploy

Enter fullscreen mode Exit fullscreen mode

Now, let's say you want to update the S3 bucket to enable server access logging. Modify the lib/your_stack_name-stack.ts file as follows

import { Stack, StackProps } from 'aws-cdk-lib';
import { Bucket, RemovalPolicy } from 'aws-cdk-lib/aws-s3';

export class YourStackNameStack extends Stack {
  constructor(scope: cdk.App, id: string, props?: StackProps) {
    super(scope, id, props);

    const loggingBucket = new Bucket(this, 'LoggingBucket', {
      removalPolicy: RemovalPolicy.DESTROY,
    });

    new Bucket(this, 'MyFirstBucket', {
      versioned: true,
      removalPolicy: RemovalPolicy.DESTROY,
      serverAccessLogsBucket: loggingBucket,
    });
  }
}

Enter fullscreen mode Exit fullscreen mode
  1. Running the CDK Diff command

Before deploying the changes, let's run the cdk diff command to inspect the differences between the local AWS CDK application and the deployed AWS CloudFormation stack

cdk diff

Enter fullscreen mode Exit fullscreen mode

You should see an output similar to the following

Stack YourStackNameStack
Resources
[+] AWS::S3::Bucket LoggingBucket LoggingBucketA1B2C3D4
[-] AWS::S3::Bucket MyFirstBucket MyFirstBucketE5F6G7H8
[+] AWS::S3::Bucket MyFirstBucket MyFirstBucketI9J0K1L2
   [+] LoggingConfiguration
       [+] DestinationBucketName: {"Ref":"LoggingBucketA1B2C3D4"}

Enter fullscreen mode Exit fullscreen mode

The output indicates the following changes:

  • A new S3 bucket (LoggingBucket) has been added.

  • The existing S3 bucket (MyFirstBucket) will be replaced with a new S3 bucket.

  • The new S3 bucket (MyFirstBucket) will have a logging configuration pointing to the LoggingBucket.

  1. Deploying the changes

Now that we've reviewed the differences using cdk diff, we can proceed to deploy the changes with the cdk deploy command

cdk deploy

Enter fullscreen mode Exit fullscreen mode

This will update the AWS CloudFormation stack with the new resources and configurations.

Conclusion

In this blog post, we've explored the importance and usage of the cdk diff command in the AWS CDK. The command serves as an efficient tool for managing and validating infrastructure changes before deployment, allowing you to catch potential issues early and maintain a clear understanding of your AWS infrastructure.

By incorporating cdk diff into your development workflow, you can maintain better control over your AWS resources, ensure that your infrastructure changes are consistent with your expectations, and improve collaboration in your team by reviewing infrastructure changes more effectively.

Top comments (0)