hashicorp / terraform-cdk

Define infrastructure resources using programming constructs and provision them using HashiCorp Terraform
https://www.terraform.io/cdktf
Mozilla Public License 2.0
4.79k stars 442 forks source link

Support kubernetes backend #1628

Open iridian-ks opened 2 years ago

iridian-ks commented 2 years ago

Community Note

Description

The Kubernetes backend in Terraform: https://www.terraform.io/language/settings/backends/kubernetes Seems to be missing from Terraform CDK: https://www.terraform.io/cdktf/concepts/remote-backends#supported-backends

It looks like CDK is generating a JSON file for backends, which I tried creating manually like so:

remote.tf.json

{
    "terraform": {
        "backend": {
            "kubernetes": {
            }
        }
    }
}

Then in the Pod setting KUBE_IN_CLUSTER_CONFIG and KUBE_NAMESPACE env vars and running cdktf deploy does not initialize the backend and uses local state.

ansgarm commented 2 years ago

Hi @iridian-ks 👋 If no backend construct is used, the CDKTF will default to using local state (and thereby render a local backend in the generated cdk.tf.json). That one probably overrides the one you are manually setting separately.

You can instead try to use an override on your Stack instance: https://www.terraform.io/cdktf/concepts/providers-and-resources#escape-hatch

However, we should add the kubernetes backend to the ones we support.

iridian-ks commented 2 years ago

Tentatively, it looks like I'm getting this:

  "terraform": {
    "backend": {
      "kubernetes": {
      },
      "local": {
        "path": ".../cdk/terraform.gloo.tfstate"
      }
    }
  }

Not sure if that's the intended behavior.

But it's resulting in:

[2022-03-21T19:34:03.007] [ERROR] default - ╷
│ Error: Duplicate backend configuration
│
│   on cdk.tf.json line 38, in terraform.backend:
│   38:       "local": {
│
│ A module may have only one backend configuration. The backend was
│ previously configured at cdk.tf.json:36,21-22.
╵

╷
│ Error: Duplicate backend configuration
│
│   on cdk.tf.json line 38, in terraform.backend:
│   38:       "local": {
│
│ A module may have only one backend configuration. The backend was
│ previously configured at cdk.tf.json:36,21-22.
iridian-ks commented 2 years ago

Sorry for the spam, but looks like this is better:

        self.add_override("terraform.backend.kubernetes", {})
        self.add_override("terraform.backend.local", None)