Development workflow– Programmatic Approach to IaC with AWS CDK

There are three main stages of progression when working with a CDK-based application:

  1. Create: Create the project structure and define the source code (stacks and constructs) that represents your application environment. Let’s look at the relevant commands at this stage. To bootstrap the target account/region pair, use the following command:

cdk bootstrap

To create a new project directory with the required prerequisites for CDK, in the programming language of your choice, use the following command:

cdk init app –language typescript

  1. Synthesize: Use the CDK CLI to reference the construct definitions and generate the required artifacts. An important intermediate stage used by CDK is Cloud Assembly, which involves collecting CloudFormation templates and assets. These assets are later uploaded in S3 buckets and ECR registries for services such as CloudFormation to consume.

To create the templates and assets that represent Cloud Assembly, use the following command: cdk synth

  1. Deploy: CloudFormation takes over the deployment responsibility and makes the required changes to the resources so that they match the target state defined by the user.

To push changes to the cloud using CloudFormation deployments, use the following command: cdk deploy

Once the CloudFormation resources have been created, end users can use the respective AWS services or applications.

Pros and cons of working with CDK

Like all other frameworks and services, CDK has its share of pros and cons. Ultimately, it comes down to how well the service fits with the present needs of the organization.

Benefits of using CDK

The main benefit of CDK is its rich ecosystem of constructed libraries. With different levels of abstraction, it empowers all types of users equally well. You can reuse the foundational constructs and also adopt higher-level opinionated abstractions with a seamless experience. Furthermore, extending the AWS CDK concepts into additional domains such as Kubernetes and Terraform makes it an interesting choice for customers who would like to reap the benefits from the time they invested in upskilling their teams with CDK.

For reference, check out the cdk8s and cdktf projects on the internet.

Cons of using CDK

The biggest disadvantage of using CDK is its initial learning curve. The tool is only intended for moderate to highly experienced AWS users. Its testing capabilities, on the other hand, are somewhat limited as you can only write some synthetic tests after the CloudFormation templates have been generated. Tools such as Pulumi offer a more advanced testing experience as they directly interact with the cloud provider, thereby offering capabilities to mock different types of calls, for example.

In the next section, we’ll understand the test application we are going to deploy in this chapter. We will then move on to getting our hands dirty by deploying this stack into an AWS account and region of your choice.

Deploying a test application with AWS CDK

CDK is quite mature when it comes to CLIs, its support for general-purpose programming languages, and its rich construct libraries, which provide reusable patterns for easy adoption.

So far in this book, we have mostly worked with Amazon Machine Images (AMI) and used them to deploy application code that was either baked into the image itself or deployed at runtime using services such as AWS CodeDeploy. In this chapter, we will use a different test application for demonstrating CDK capabilities – automatic image recognition and labeling. We will use the Amazon Rekognition service, which allows users to add image and video analysis capabilities to their applications. You can provide an image to the service and it can identify objects, people, text, and scenes. This is a very good example of how end users can leverage AWS services to build innovative applications, without having to develop a deep understanding of machine learning. The test application will also allow you to experience the ease of building supporting infrastructure services in CDK for such an application.

Let’s dive straight into the specifics of how the application works.

Leave a Comment