cdktf / cdktf-tf-module-stack

A drop-in replacement for cdktf.TerraformStack that lets you define Terraform modules as constructs
Mozilla Public License 2.0
18 stars 2 forks source link

Provider & Backend information is not removed in cdktf 0.20.0 --hcl output #299

Open shinebayar-g opened 5 months ago

shinebayar-g commented 5 months ago

Description

Using the following example

import { App } from 'cdktf';
import { Construct } from 'constructs';
import {
    TFModuleStack,
    TFModuleVariable,
    TFModuleOutput,
    ProviderRequirement,
} from '@cdktf/tf-module-stack';
import { Resource } from '@cdktf/provider-null/lib/resource';

class MyAwesomeModule extends TFModuleStack {
    constructor(scope: Construct, id: string) {
        super(scope, id);

        new ProviderRequirement(this, 'null', '~> 2.0');
        const resource = new Resource(this, 'resource');

        new TFModuleVariable(this, 'my_var', {
            type: 'string',
            description: 'A variable',
            default: 'default',
        });

        new TFModuleOutput(this, 'my_output', {
            value: resource.id,
        });
    }
}

const app = new App();
new MyAwesomeModule(app, 'my-awesome-module');
app.synth();

cdktf synth generates

cdktf-modules
├── cdktf.out
│   ├── manifest.json
│   └── stacks
│       └── my-awesome-module
│           └── cdk.tf.json
└── main.ts

cdk.tf.json content

{
  "//": {
    "metadata": {
      "backend": "local",
      "stackName": "my-awesome-module",
      "version": "0.20.0"
    },
    "outputs": {
      "my-awesome-module": {
        "my_output": "my_output"
      }
    }
  },
  "output": {
    "my_output": [
      {
        "value": "${null_resource.resource.id}"
      }
    ]
  },
  "resource": {
    "null_resource": {
      "resource": {
        "//": {
          "metadata": {
            "path": "my-awesome-module/resource",
            "uniqueId": "resource"
          }
        }
      }
    }
  },
  "terraform": {
    "required_providers": {
      "null": {
        "version": "~> 2.0"
      }
    }
  },
  "variable": {
    "my_var": [
      {
        "default": "default",
        "description": "A variable",
        "type": "string"
      }
    ]
  }
}

cdktf synth --hcl generates

cdktf-modules
├── cdktf.out
│   ├── manifest.json
│   └── stacks
│       └── my-awesome-module
│           ├── cdk.tf
│           └── metadata.json
└── main.ts

cdk.tf content

terraform {
  required_providers {
    null = {
      version = "~> 2.0"
    }
  }
  backend "local" {
    path = "/Users/Home/cdktf-modules/terraform.my-awesome-module.tfstate"
  }

}

provider "null" {
}
resource "null_resource" "resource" {
}

variable "my_var" {
  default     = "default"
  description = "A variable"
  type        = string
}

output "my_output" {
  value = "${null_resource.resource.id}"
}

metadata.json content

{
  "//": {
    "metadata": {
      "backend": "local",
      "stackName": "my-awesome-module",
      "version": "0.20.0"
    },
    "outputs": {
      "my-awesome-module": {
        "my_output": "my_output"
      }
    }
  },
  "resource": {
    "null_resource": {
      "resource": {
        "//": {
          "metadata": {
            "path": "my-awesome-module/resource",
            "uniqueId": "resource"
          }
        }
      }
    }
  },
  "terraform": {
    "backend": {
      "local": {
        "path": "/Users/Home/cdktf-modules/terraform.my-awesome-module.tfstate"
      }
    },
    "required_providers": {
      "null": {
        "version": "~> 2.0"
      }
    }
  }
}

Versions

language: typescript
cdktf-cli: 0.20.0
cdktf: 0.20.0
node: v20.10.0
constructs: 10.3.0
terraform: v1.3.7
arch: arm64
os: darwin 23.2.0

Providers

"@cdktf/provider-null": "10.0.0",
"@cdktf/tf-module-stack": "5.0.0"

Gist

No response

Possible Solutions

No response

Workarounds

No response

Anything Else?

Is metadata.json file needed? It looks identical to the previous json version.

References

No response

Help Wanted

Community Note

DanielMSchmidt commented 5 months ago

We don't support HCL output in this repository right now, sorry.