TheSupe87 - Fotolia


Compare AWS CDK vs. CloudFormation, and the state of IaC on AWS

Infrastructure as code has become an essential part of the cloud development process. Learn how the AWS CDK addresses the weaknesses of CloudFormation and improves automation on AWS.

Infrastructure as code has quickly become a go-to process to automatically provision and manage cloud resources. With increasing sophistication, engineers and DevOps teams are codifying infrastructure for greater application flexibility and functionality, with a single-source language across an organization.

IT teams have two AWS-native options for infrastructure as code -- AWS CloudFormation and the AWS Cloud Development Kit (CDK). CloudFormation templates were AWS' first foray into cloud-based infrastructure as code, and while still useful, CloudFormation has clear weaknesses. More specifically, it doesn't offer built-in logic capabilities and has a steep learning curve.

The AWS CDK, an open source software development framework to define cloud infrastructure, addresses these weaknesses. The AWS CDK supports popular programming languages, which developers can use to build, automate and manage infrastructure based on an imperative approach. Finally, developers can provision these commands through CloudFormation.

As an extensible, open software development framework, the AWS CDK features integrated development environment (IDE) capabilities. As of publication, the AWS CDK supports TypeScript, JavaScript, Python, Java and C#/.Net. In this article, we'll compare the AWS CDK vs. CloudFormation, including their key features, the role of constructs in building application stacks and the benefits of using a common language for AWS-native infrastructure as code.

AWS provisioning with CloudFormation

In order to understand the advantages of AWS CDK vs. CloudFormation, we need to look at how CloudFormation is used to access and manage AWS resources. Based on a series of formatted configuration files, AWS CloudFormation provides programmatic access to an application's resources. Developers create YAML templates or JSON files to define basic operators, such as if statements and for loops.

These files describe resources such as VPC configurations, Relational Database Service instances, S3 buckets, API endpoints and more. Developers can then run the necessary template files from a DevOps pipeline such as Jenkins to spin up the desired infrastructure.

However, YAML and JSON files are based on data serialization and are not actual programming languages. And as AWS adds more infrastructure integrations to CloudFormation, it becomes increasingly difficult to work with larger JSON and YAML files. Moreover, CloudFormation templates lack abstraction, so developers must write numerous lines of text to indicate lower level details. The AWS CDK is meant to overcome these limitations by enabling reusable code and proper testing.

AWS CDK Construct Library and key features

Although it's relatively easy for developers to work from the AWS Management Console, a key element that's been missing in AWS is a simple, automated way to iterate infrastructure. Introduced in August 2018, the AWS CDK enables programmers to write infrastructure as code using an object model, then synthesize it into CloudFormation templates to smoothly provision resources.

There are a few key elements and features to understand before you work with the AWS CDK. The building blocks of AWS CDK applications are called constructs. They can represent a range of architectures, from a single S3 bucket or static website to a multi-stack application spanning multiple AWS regions. A construct can also incorporate other constructs, composed together into stacks. Developers can then deploy these into an AWS environment as an application, possibly consisting of multiple stacks.

Within the AWS CDK, there's the AWS Construct Library, which includes a range of high- and low-level constructs. The low-level constructs, known as CFN resources, simplify the provisioning process by automatically defining the designated resource without requiring extensive details. A second level of constructs handle common tasks -- boilerplate code and the glue logic required by CFN resources. Finally, developers can use high-level constructs, called patterns, to complete tasks that involve multiple AWS resources.

AWS CDK excels at provisioning multiple services that need to communicate with one another. High-level constructs enable developers to get started quickly by substituting clear default values for any unfilled parameters. In addition to high-level constructs and integration, the CDK framework provides type checking, auto completion and extensive tooling, such as its command-line interface and toolkit for Visual Studio Code.

Use the AWS CDK to define infrastructure end to end

Because the AWS CDK expands the number of resources that developers can manipulate through a code base, it offers more functionality than limited tools such as CloudFormation and Hashicorp TerraForm. The AWS CDK not only streamlines the provisioning process, it also simplifies verification and review.

For example, organizations benefit from a smaller codebase. This comes in handy when decoding problematic configuration files or documenting procedures, and it further eases the process of infrastructure reviews and rebuilds. In addition to improved readability through reduced codebase size, developers can transfer deployment and configuration scripts into a single codebase that relies on one common language.

Finally, by working within modern IDEs, developers can create applications and infrastructure using key features such as autocompletion and parameter suggestions. With the AWS CDK, developers benefit from further IDE features, such as highlighting, syntax checking, inspections and refactoring support.

Ultimately, the AWS CDK provides developers with the ability to write proper, reusable code in a familiar language and, in the process, produce more secure, consistent infrastructure.

Next Steps

AWS CloudFormation vs Terraform: How to choose?

Dig Deeper on AWS cloud development

App Architecture
Cloud Computing
Software Quality