Introduction

In the ever-evolving world of cloud infrastructure management, the transition from AWS CloudFormation or deployed resources to AWS Cloud Development Kit (CDK) has become more accessible and streamlined. A notable development in this journey is the introduction of the "cdk migrate" command, offering a simple and automated way to convert constructed resources into AWS CDK projects. This command aims to make the migration process smoother, providing developers with an efficient path to leverage the benefits of AWS CDK without the complexities of manual code transformation.

The YouTube Channels in both English (En) and French (Fr) are now accessible, feel free to subscribe by clicking here.

The AWS “cdk migrate” Command

The "cdk migrate" is a new feature that’s part of the AWS CDK CLI, so make sure to install or update it using the command "npm install -g aws-cdk“. This command automates the transformation process, eliminating the need for manual code adjustments and reducing the time and effort traditionally associated with AWS infrastructure migration.

Key Benefits of the “cdk migrate” Command

  1. Time and Effort Savings
    Developers and Cloud Engineers can significantly reduce the time and effort required for migration by automating the conversion process, enabling a faster transition to AWS CDK.
  2. User-Friendly Migration
    With a simple command, developers can initiate the migration process without delving into the intricacies of manual code transformation, making it more accessible to a broader audience.
  3. Automatic Code Conversion
    The “cdk migrate” command intelligently analyzes deployed AWS resources, deployed AWS CloudFormation stacks, or Local AWS CloudFormation templates and automatically converts them into equivalent AWS CDK code. This automated process ensures that the resulting CDK code maintains the functionality and structure of the original CloudFormation template.
  4. Preservation of Resource Configurations
    The command maps CloudFormation or deployed resources to their corresponding AWS CDK constructs, preserving resource configurations and settings during the migration. This ensures that the behavior of the infrastructure remains consistent after the transition.

How to Use “cdk migrate”

Using the “cdk migrate” command is straightforward and can be executed with a single command-line entry as illustrated below depending on your use case.

For the next commands to work, you have to Configure the AWS CLI on your machine or at least provide the credentials in the appropriate file:

The CDK Migrate feature is currently in a preview release for AWS CDK and may undergo modifications, it is an experimental feature so no guarantees about the outcome or stability of the functionality.

CDK Migrate from an AWS CloudFormation Stack

For migrating from an existing AWS CloudFormation stack, utilize the –from-stack option. Specify the name of your deployed AWS CloudFormation stack using the –stack-name parameter. Here is an illustrative example:

cdk migrate --from-stack --stack-name "PowerUpStackDev"
CDK Migrate from an AWS CloudFormation Stack

Oooooohaaaaaa, an AWS CDK project has been created in the current folder, and it contains the resources I had in my original stack.

Note: Considering that you have a stack called PowerUpStackDev in your AWS Account, change that name depending on your infrastructure.

CDK Migrate from an AWS CloudFormation Template

Let’s consider you have a CloudFormation template, either in JSON or YAML format. The CDK Migrate command will practically migrate the defined AWS Cloud resources to a CDK Project.

Use the following command to do it:

cdk migrate --from-path "./template.json" --stack-name "PowerUpStackDev"

In this command, the –form-path option specifies the path to your local CloudFormation template, and the –stack-name is the name of the final stack.

CDK Migrate from Deployed Resources

To migrate from deployed AWS resources, provide the --from-scan option as well as the required --stack-name parameter as illustrated below:

cdk migrate --from-scan --stack-name "PowerUpStackDev"
CDK Migrate from an AWS deployed resources

It may take a few moments depending on the number of resources you have on your AWS account.

The AWS CDK CLI performs the following actions behind the scene:

  1. Scan for resource and property details in your AWS account:
    The AWS CDK CLI leverages the Infrastructure as Code (IaC) generator feature to scan your AWS account, collecting resource and property details.
  2. Generate an AWS CloudFormation template:
    Following the scanning process, the AWS CDK CLI uses the IaC generator to create the corresponding AWS CloudFormation template based on the gathered information.
  3. Initialize a new CDK app and migrate your template:
    Subsequently, the AWS CDK CLI executes the cdk init command to initialize a new AWS CDK app. It then seamlessly migrates your AWS CloudFormation template into the CDK app to form a single stack.

Despite its huge capabilities, the CDK Migrate command is still in heavy development and should not be used without more consideration, read more on this page.

———————

We have just started our journey to build a network of professionals to grow even more our free knowledge-sharing community that’ll give you a chance to learn interesting things about topics like cloud computing, software development, and software architectures while keeping the door open to more opportunities.

Does this speak to you? If YES, feel free to Join our Discord Server to stay in touch with the community and be part of independently organized events.

———————

Conclusion

The “cdk migrate” command represents a significant advancement in simplifying the migration journey from AWS CloudFormation or Deployed resources to AWS CDK. With its automated capabilities, developers can embrace the benefits of CDK’s programmatic infrastructure management without the complexities of manual code conversion. Despite its huge capabilities in the IaC space, the CDK Migrate command is still in heavy development and should be used with care.

Thanks for reading this article. Like, recommend, and share if you enjoyed it. Follow us on FacebookTwitter, and LinkedIn for more content.

author-avatar

About Orleando Dassi

I'm a Solutions Architect with 10 years of experience who is constantly learning while impacting the community by producing technical articles/videos, building projects, and conducting tech mentoring/coaching sessions. What describes me the most is my flexibility. Follow me on Twitter and LinkedIn.