From Terraform to CDK — Former2

Geoffrey Ge
carsales-dev
Published in
4 min readMar 14, 2021

Our journey of migrating to CDK (Part 2)

In the last article, we explored how to use CDK to create stacks across regions and import existing resources by CDK synth. It is not ideal because we still have to code from scratch. Today, we will try with Former2 to accelerate the migration process.

What is Former2

Cloudformer has been officially deprecated and removed. Former2 is like Cloudformer that generates Infrastructure-as-Code outputs from existing AWS resources. It provides wider coverage of AWS resources and richer output format (Cloudformation, CDK and CDK for Terraform).

Use Former2

In this example, I will demonstrate how to use Former2 to migrate a simple S3 bucket into CDK managed stack. I found using Former2 to be quite straight forward.

  • First, install the extension to your browser.
  • Scan account and add your resources.
  • Generate CDK code and copy to your own CDK project. The default language for generated CDK is typescript.

Import Resource

Let’s try again with the import process described in the previous article:

  • Create an empty stack by cdk.
  • Use cdk synth to generate the Cloudformation template.
  • Import the S3 bucket by upload the Cloudformation template in your cdk.out folder.

While this seemed straight forward, we encountered something that went wrong at the last step.

To import an existing resource into a stack, DeletionPolicy must be specified in the template.

So let’s modify our CDK code to add DeletionPolicy and cdk synth again.

Now, let’s try again. But wait, something has gone wrong again! The CDKMetadata has been changed.

After comparing the initial empty stack template and the template with S3 bucket, I noticed that cdk synth will not include the un-used reference in CDKMetadata. Hence, the empty stack template just includes the basic packages (aws-cdk and aws-cdk/core etc) while the final template includes (aws-s3)

I have not dug deep into this issue but to simplify I made a multi-stack app to work around this issue.

  • Make a copy of the stack file.
  • Comment out the original file’s content.
  • Add another stack copy in your CDK app and then use cdk synth [stack-name] to generate the empty stack template. You will find the template includes all the reference in CDKMetadata.

And then uncomment the content in the original file, generate a template and import again. Finally, the S3 bucket import is successful.

At last, I tried to generate CDK code in python which was a success. You can change your preferred output language on the settings page.

Summary

Through this journey, we’ve explored how to use Former2 to generate CDK code and import existing resources. I have also tried to create a replacement resource by use Former2.

In both scenarios, Former2 accelerated our migration process sharply as we did not have to write code from scratch. This saved a lot of time for learning and document checking.

Former2 has quite good coverage for AWS resources and I would recommend using Former2 for migrating existing resources into Cloudformation or CDK.

Next

I will try use CDK pipeline to rebuild our current Jenkins+Terraform CI/CD pipeline in the next article.

Reference:

https://former2.com/#

https://aws.amazon.com/blogs/opensource/accelerate-infrastructure-as-code-development-with-open-source-former2/

https://github.com/iann0036/former2/blob/master/RESOURCE_COVERAGE.md

--

--