Converting infrastructure to AWS CloudFormation and AWS CDK

by Adam Świątkowski
5 mins read
From pure infra to AWS CDK app

There are many supporters and opponents of describing infrastructure as a code (eng. IaC) using high-level programming languages. I am definitely one of the supporters and for this reason I would like to share an overview of a newly announced features from AWS that will allow you to convert your infrastructure to IaC smoothly. However, I wouldn’t be myself if I didn’t treat the topic holistically, so I prepared (clicked) infrastructure, which I converted into a CloudFormation stack via IaC generator and after to the CDK application via cdk migrate. Let’s check how it works!

How to convert infrastructure done in a “ClickOps” fashion to IaC?

I “clicked on” the infrastructure below. This is a simple API with a Lambda function returning an example message. This architecture is not described by any code and any IaC at the moment.

Simple ClickOps Architecture in AWS
Simple ClickOps Architecture in AWS

In the next steps I want to: create a CloudFormation stack for it, and than to create a CDK app from it. Let’s see if it can be easily done.

Moving an existing infrastructure into a CloudFormation template using IaC generator

Let’s start with going into IaC generator here and start the scanning.

Starting a scan via IaC Generator
Starting a scan via IaC Generator

After the scan is complete, click the Create template button to start connecting our API resources to the CloudFormation’s stack.

Creating an IaC Generator's Template
Creating an IaC Generator’s Template

Select the resources relevant for our API (i.e. Lambda function, IAM roles for Lambda, API Gateway).

Grouping up resources into IaC Generator's Template
Grouping up resources into IaC Generator’s Template

Trigger template creation with Create template button.

Triggering creation of IaC Generator's Template
Triggering creation of IaC Generator’s Template

When the IaC template will be created you can try to import it to CloudFormation. Click Import to stack button.

Reviewing import of an IaC Generator's Template into the CloudFormation Stack
Reviewing import of an IaC Generator’s Template into the CloudFormation Stack

We should be able to import resources into the Stack.

Creation of a proper CloudFormation Stack
Creation of a proper CloudFormation Stack

Well, we did it 🙂 We imported our services into a CloudFormation Stack.

Can we convert our CloudFormation stack to the CDK application?

We have taken the first steps to modify our infrastructure in a way that it is described with IaC. The next step that may come to your mind is to convert the resulting CloudFormation stack into an AWS CDK application. This also applies to people who already have ready-made CloudFormation stacks and want to convert them into an AWS CDK application. In my case, I had to convert CloudFormation stacks from an external service provider into CDK constructs. This vendor prepared Stacks for connecting with their systems, and since my client’s entire company was based on AWS CDK, we decided to convert this integration into code in CDK and Python.

What is AWS CDK?

AWS Cloud Developement Kit

From what AWS describes: “AWS Cloud Development Kit (AWS CDK) accelerates cloud development using common programming languages to model your applications.” and it’s totally accurate. If you come from a programming background and want to work with the AWS cloud, the easiest way to describe the infrastructure will be using the CDK, which allows you to achieve this using many, supported, programming languages.

What is CDK Migrate?

cdk migration tool
cdk migration tool

cdk migrate is a newly released feature included in the AWS CDK framework that allows you to convert your AWS cloud resources to AWS CDK applications. As of today it supports:

  • Create CDK app by scanning all resources deployed to the account,
  • Create CDK app from the CloudFormation stack,
  • Create CDK app from a local CloudFormation template.

Converting CloudFormation Stack to AWS CDK application

Let’s take the CloudFormation’s stack which we’ve created with the IaC generator and see if we can “migrate” it to the CDK application. Let’s run the cdk migrate command with selecting our stack `IaCGeneratedStack`.

Running cdk migrate command
Running cdk migrate command

We see success, so let’s see what our beloved cdk generated for us.

CDK Application created from CloudFormation stack with cdk migrate
CDK Application created from CloudFormation stack with cdk migrate

So here we see the generated CDK stack along with constructs for individual resources. The constructs are of the L1 type and are a good starting point to work on modernizing them. Certainly, as the cdk migrate tool will evolve, we will receive constructs of a higher type, but at the moment even the L1 type allows us to better understand what resources our infrastructure consists of.

CDK Migrate Limitations

AWS CDK Migrate relies on the IaC generator service, because of that it shares some limitations. If you have a very large infrastructure, you may want to use the --filter flag when using the cdk migrate command to specify the resources you want to export (you can find examples here). Also, AWS CDK Migrate, can only generate L1 CDK constructs for now.

CDK Migrate Limitations
CDK Migrate Limitations


Thank you for reaching out to that place. If you want to know more about AWS and the cloud, check the below posts:

Related Articles

This website uses cookies to improve your experience. I'll assume you're ok with this, but you can opt-out if you wish. Read Privacy Policy. Accept