AWS CDK and how it compares to CloudFormation

Infrastructure as Code CDK
Share the post:
Infrastructure as Code (IaC) allows you to define and provision cloud resources using code, making the process more automated and consistent. AWS CDK is a powerful IaC tool that comes with the flexibility of a programming language.

In this blog post, we’ll explore AWS Cloud Development Kit, or CDK with Python examples and compare it to the other popular Instracture as Code (IaC) method, AWS CloudFormation, highlighting when and how CDK might be better.

What is AWS CDK

AWS CDK (Cloud Development Kit) is a framework from Amazon Web Services (AWS) that allows you to define cloud resources using programming languages like Python, TypeScript, JavaScript, Java, and C#. Instead of using low-level JSON or YAML configurations, CDK enables you to use high-level programming constructs to describe your infrastructure.

Here’s a simple Python example using AWS CDK to create an S3 bucket:

				
					from aws_cdk import core
import aws_cdk.aws_s3 as s3

class MyStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # Create an S3 bucket
        s3.Bucket(self, 
                  "MyBucket",
                  versioned=True
                  )

app = core.App()
MyStack(app, "MyStack")
app.synth()
				
			

In this Python example, we define a stack with a versioned S3 bucket. The CDK framework compiles this code into an AWS CloudFormation template, which provisions the resources.

Here’s a simple Python example using AWS CDK to create an S3 bucket:

Comparing AWS CDK with CloudFormation

AWS CDK is one of several tools available for IaC. Let’s compare it with AWS CloudFormation, focusing on scenarios where CDK might be better.

AWS CloudFormation is the foundational service that AWS CDK uses. It allows you to define your infrastructure using JSON or YAML templates. Here’s how AWS CDK compares:

Abstraction and Simplicity: CloudFormation requires you to define every detail in JSON or YAML, which can be verbose and complex. AWS CDK provides higher-level abstractions and constructs. For example, in CDK, creating an S3 bucket is straightforward with a single line of code, while in CloudFormation, it requires a more detailed configuration.

CloudFormation example
				
					Resources:
  MyBucket:
    Type: 'AWS::S3::Bucket'
    Properties:
      VersioningConfiguration:
        Status: Enabled
				
			
CDK example
				
					from aws_cdk import core
import aws_cdk.aws_s3 as s3

class MyStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # Create an S3 bucket
        s3.Bucket(self, 
                  "MyBucket",
                  versioned=True
                  )
				
			

Programming logic: CDK allows you to use the full power of a programming language so you can use constructs like loops and conditionals. This makes dynamic resource creation easier. CloudFormation is extremely limited in applying conditional or advanced logic. One real example of such a limitation is in dynamic naming of resources, as noted in this post.

The example below shows how CDK can use a ‘for’ loop to create S3 buckets. The buckets are also named dynamically, as ‘MyBucket1, MyBucket2, MyBucket3 etc. Incorporating this kind of logic in CloudFormation templates is going to be quite a challenge.

CDK example
				
					from aws_cdk import core
import aws_cdk.aws_s3 as s3

class MyStack(core.Stack):

    def __init__(self, scope: core.Construct, id: str, **kwargs) -> None:
        super().__init__(scope, id, **kwargs)

        # Create multiple S3 buckets
        for i in range(3):
            s3.Bucket(self, 
                      f"MyBucket{i}",
                      versioned=True
                      )

				
			

Many developers who get used to using CDK tend to never go back to using CloudFormation! It is definitely worth a try to define your next IaC template in CDK. But before you can use AWS CDK, you will need to install a few additional components and set up your project. This is covered in a separate post here.

Share the post:

Related posts

Azure Virtual Desktop

Why small businesses should consider using Azure Virtual Desktop

For startups and small enterprises looking to future-proof their operations and empower a dynamic, remote workforce, Azure Virtual Desktop is an oustanding choice. By leveraging Azure, small businesses can enhance productivity, improve security, and position themselves to adapt to the ever-evolving business landscape, all while keeping costs under control and maximizing their potential for growth.

Read More
aws cdk how to get started

How to get started with AWS CDK

AWS CDK (Cloud Development Kit) is a great way to manage your cloud infrastructure using familiar programming languages. AWS CDK allows you to define your cloud resources in a more intuitive way compared to traditional JSON or YAML-based approaches.

Read More

Leave a Reply

Your email address will not be published. Required fields are marked *