hashicorp / cdktf-aws-cdk

Use AWS CDK constructs in CDKTF projects
Mozilla Public License 2.0
83 stars 11 forks source link

Support AWS CDK Aspects #153

Open ansgarm opened 2 years ago

ansgarm commented 2 years ago

Invoke them before converting AWS CDK constructs to CDKTF constructs.

sapslaj commented 4 months ago

I took a stab at implementing this, but ran into a few issues.

  1. CDK's invokeAspects function isn't exported https://github.com/aws/aws-cdk/blob/eca1bcf0b8a449d07692e6363cefa149b1fe0ce4/packages/aws-cdk-lib/core/lib/private/synthesis.ts#L217. For my proof-of-concept I just copied the function body and that worked, but that's probably not great for anything PR-able.
  2. The Aspect execution order gets really tricky, especially since CDKTF doesn't have any way to tweak that order without arcane tricks (e.g. Aspects.of(scope)['_aspects'].unshift(new MyAspect(this))). This means that the Aspect responsible for doing the resource conversion pretty much has to be the last Aspect invoked, otherwise you end up with half-built resources.
  3. createGuessingResourceMapper doesn't seem to handle Tags very well. A lot of CloudFormation resources use a type shaped like Array<{ Key: string; Value: string }> but most Terraform schemas use something more akin to Record<string, string>. Even in CDK there's a whole lot of logic to render tags correctly based on the resource. I'll admit this is mostly solved by #554 but even then tags are gonna be a pain point for any custom mappings.

Aside from those issues the implementation is actually quite simple, as you just have to call invokeAspects with the CDK scope before host.convert(). That said, invokeAspects itself uses CDK Annotations so we need #152 before it can be fully feature-complete.