CDKTF Generate Module

I normally write all of my Terraform manually in HCL. However, I have a complex setup for some of my infra that is cumbersome and hard to maintain with manual HCL. Ideally I could generate the Terraform into its own module and then consume it as such. CDKTF looks like a great solution, and it even says “CDKTF can generate modules that HCL Terraform projects can use in their configurations.” (HCL Interoperability | Terraform by HashiCorp)

I have a testing setup, but I am unable to fully make it work without manually modifying the output JSON. My infra needs the AWS provider, but it needs it in multiple regions. I accomplish this manually by setting configuration aliases, but I cannot find a way via CDKTF to do this. My output JSON looks like:

{
  "//": {
    "metadata": {
      "backend": "local",
      "stackName": "testing",
      "version": "0.12.0"
    },
    "outputs": {}
  },
  "provider": {
    "aws": [
      {
        "alias": "us-east-1",
        "region": "us-east-1"
      },
      {
        "alias": "us-east-2",
        "region": "us-east-2"
      }
    ]
  },
  "resource": {
      "tgw-us-east-1": {
        "< removed for shorter sample >": null,
        "provider": "aws.us-east-1"
      },
      "tgw-us-east-2": {
        "< removed for shorter sample >": null,
        "provider": "aws.us-east-2"
      }
    }
  },
  "terraform": {
    "backend": { "< removed for shorter sample >": null },
    "required_providers": {
      "aws": {
        "source": "hashicorp/aws",
        "version": "4.32.0"
      }
    }
  }
}

To make it work as a module I have to:

  1. delete .provider
  2. delete .terraform.backend
  3. setup .terraform.required_providers["aws"].configuration_aliases with an array of aliases: ["aws.us-east-1", "aws.us-east-2"]

Without those modifications I cannot use it properly as a module. Am I missing something when setting up my CDKTF code? Or when running synth? Ideally I would not have to take the steps mentioned above to make it work as a module.

For this use-case you should use GitHub - cdktf/cdktf-tf-module-stack: A drop-in replacement for cdktf.TerraformStack that lets you define Terraform modules as construct, it’s exactly built for this use-case. I’m currently working on documentation about this workflow, right now it’s a bit sparse, but we have a CDK Day talk about this topic: CDK Day 2022: Using CDKTF constructs to generate TerraformModule - YouTube

This looks promising! Is there any way to use it in Go? My team is most familiar with Go which is how we are writing our CDKTF as well as other projects.

Not right now, but I’ll look into it. It’s mostly some mechanics related to publishing in other languages that is missing from the cdktf-tf-module-stack. For the projen template (and generating constructs to use in other languages): this won’t work in other languages then typescript.

Thanks! That would be greatly appreciated. Writing TF providers in Go I was somewhat surprised Go was not the first language for CDKTF.