shantanuo
shantanuo

Reputation: 32336

Convert CloudFormation template (yaml) to cdk python code

I have this template that works as expected. Is there any way to convert it as cdk python code?

https://github.com/shantanuo/cloudformation/blob/master/updated/api-to-sns-cf%20(1).yml

Upvotes: 3

Views: 9386

Answers (3)

fedonev
fedonev

Reputation: 25669

Edit Feb 2024: ✨ CDK codegen with cdk migrate

AWS has introduced the cdk migrate command, which can generate a CDK app from a local YAML or JSON template. Here's an example that creates a new Python CDK app with a stack named MyStack from a local file:

cdk migrate --from-path "path/to/template.yaml" --stack-name "MyStack" --language "python"

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

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

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


Original:

Yes. The CDK's cloudformation_include module natively imports CloudFormation templates, converting each resource into a L1 construct.

After you "convert" the CloudFormation YAML (or JSON) template to CDK, you can read and update imported resources:

from pathlib import Path
from typing import cast
from constructs import Construct
from aws_cdk import Stack, cloudformation_include as include, aws_apigateway as api

class CfnIncludeStack(Stack):
    def __init__(self, scope: Construct, construct_id: str, **kwargs) -> None:
        super().__init__(scope, construct_id, **kwargs)

        converted = include.CfnInclude(
            self,
            "Converted",
            template_file=str(Path(__file__).with_name("template.yml")),
        )

        cfn_api = cast(api.CfnRestApi, converted.get_resource("ApiGatewayRestApi"))

Upvotes: 3

Jon Nichols
Jon Nichols

Reputation: 2351

There is nothing wrong with using low-level resources, as it's a useful building block. Yes, there are high level constructs, but like everything else in software, occasionally you have to dig deeper. It's a shame that the attitude like this has continued, because for some of us, that have spent years building out a nice collection of CloudFormation templates, are discouraged from using CDK.

Yes, there are some issues with this, mostly because of some poor architecture decisions and a desire to focus on the new instead of building up layers of functionality, but it's possible and you shouldn't be dissuaded in converting your templates to low-level constructs, and then refactoring into higher-level ones as your use case requires.

That said, I would NOT use any kind of automatic tools to do this conversion. You won't actually understand what's going on, and you'll more than likely have issues that you won't know how to handle. Dig in, convert line-by-line, and then enjoy the results.

Upvotes: 3

bgdnlp
bgdnlp

Reputation: 1145

You can't convert a CloudFormation template to CDK code. It doesn't really make sense to do it, either. Kind of like asking to convert assembly, or C, to Python.

In Troposphere's case it makes sense, because Troposphere has a 1:1 relationship to CloudFormation. The CDK is supposed to be higher level, much like Python's relation with Assembly or C. The CDK does have CfnResource classes, which do translate directly to CloudFormation, but that's just the necessary underlying layer, it's not how the CDK is supposed to be used. If you want to go that way, you'd be better off with Troposphere.

It might sound like it would make sense to be able to do it during a transition period, but in my opinion it's more trouble than it's worth. Anyway, there's nothing that does it at this time.

Edit: For Typescript, there's cdk-dasm. It's Typescript only and it converts CloudFormation to Cfn resources. Quoting from that page:

Generally, this is not a recommended approach when using the AWS CDK, but some people may find this useful as a means to get started or migrate an existing template.

Using this method means that you will have to use the low-level resources (e.g. s3.CfnBucket instead of s3.Bucket). This means that you lose a substantial portion of the value of the CDK, which abstracts away much of the boilerplate and glue logic required to work with AWS resources.

Upvotes: -1

Related Questions