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!
Table of Contents
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.
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.
After the scan is complete, click the Create template button to start connecting our API resources to the CloudFormation’s stack.
Select the resources relevant for our API (i.e. Lambda function, IAM roles for Lambda, API Gateway).
Trigger template creation with Create template button.
When the IaC template will be created you can try to import it to CloudFormation. Click Import to stack button.
We should be able to import resources into the 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?
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 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`.
We see success, so let’s see what our beloved cdk generated for us.
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.
Summarize
Thank you for reaching out to that place. If you want to know more about AWS and the cloud, check the below posts:
- Autoscaling solution for Amazon ECS Cluster
- Unit testing Lambda functions
- Networks not so scary with Amazon VPC Lattice
- Is AWS certification really worth it?