John
John

Reputation: 11831

Importing existing CloudFormation resources into CDK at scale

Assume I have a very large number of existing CloudFormation stacks (many 1000s across multiple accounts). The in-house custom tooling that was used to create these stacks and resources is no longer maintainable and I want to move to CDK.

I want to bring these stacks and resources under CDK control. Is there an accepted best practice for doing this? The number of stacks means rewriting the stacks or manual resource by resource imports is not viable.

It feels like I’m possibly after a means of synthesising a high level language script from a CloudFormation template (basically the reverse of cdk synth). Any ideas on how I might achieve this? Are there other approaches I should consider?

Upvotes: 1

Views: 781

Answers (1)

fedonev
fedonev

Reputation: 25679

Edit Feb 2024: ✨ CDK codegen with cdk migrate

AWS has introduced the cdk migrate command, which can generate a CDK app from a deployed CloudFormation stack. Here's an example that creates a new Python CDK app from the cloud-side MyCloudFormationStack:

cdk migrate --from-stack --stack-name "MyCloudFormationStack" --language python

You can also migrate deployed but unmanaged resources (--from-scan) and local templates (--from-path).

See the migrate command reference and the accompanying page Migrate to AWS CDK in the docs.

The command creates a new CDK app with a single stack.


Original Answer:

The CfnInclude construct "imports" resources from an existing CloudFormation template file. The resulting CDK construct is a collection of L1 resources, corresponding 1:1 to the template resources.

const cfnInclude = new CfnInclude(this, "Include", {
  templateFile: "path/to/template/file",
  parameters: {
    MyParameter: isA ? "valueA" : "valueB",
  },
});

You will want assurance that the resulting CDK stacks have recreated the existing templates with 100% fidelity. The CDK has your back with this, too:

  • The CDK's unit and integration testing constructs have assertion and snapshotting tools.

  • The CDK CLI can diff CDK artefacts against the cloud-side CloudFormation templates. And the --method=prepare-change-set flag on cdk deploy will create a change set without executing it.

Upvotes: 2

Related Questions