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

Empty provider configuration blocks are not required warning when using generated json as a module #1518

Open marengaz opened 2 years ago

marengaz commented 2 years ago

Community Note

cdktf & Language Versions

python cdk 0.8.6

Affected Resource(s)

provider

Steps to Reproduce

main.py

from constructs import Construct
from cdktf import App, TerraformStack, TerraformVariable
from imports.google import (
    BigqueryDataset,
    GoogleProvider,
)

class MyStack(TerraformStack):
    def __init__(self, scope: Construct, ns: str):
        super().__init__(scope, ns)
        google = GoogleProvider(self, "google")

        ds = BigqueryDataset(
            self,
            "test_ds",
            provider=google,
            project="my-project",
            dataset_id="test_ds",
            location="EU",
        )

def main():
    app = App()
    MyStack(app, "my_stack")
    app.synth()

if __name__ == "__main__":
    main()

synthesised cdk.tf.json

{
  ...
  "provider": {
    "google": [
      {}
    ]
  },
  ...
}

calling cdk.tf.json as a module from another tf instance

module "MyStackModule" {
  source = "path/to/cdktf.out/stacks/my_stack"
}

Actual Behavior

calling the synthesised cdk.tf.json as a module from another terraform instance cases the following warning to be raised

β•·
β”‚ Warning: Empty provider configuration blocks are not required
β”‚ 
β”‚   on .terraform/modules/MyStackModule/my_stack/cdk.tf.json line 22, in provider:
β”‚   22:     "google": [
β”‚ 
β”‚ Remove the google provider block from module.MyStackModule.
β•΅

removing google = GoogleProvider(self, "google") from the cdk main.py raises the following error:

jsii.errors.JSIIError: Validation failed with the following errors:
  [dataset] Found resources without a matching provider. Please make sure to add the following providers to your stack: google

Expected Behavior

either:

Important Factoids

References

marengaz commented 2 years ago

on a similar note cdk 0.9.0 introduces another warning, because it defaults the backend to be the directory where the synth took place

{
  ...
 "terraform": {
    "backend": {
      "local": {
        "path": "path/to/terraform.batch_ingest.tfstate"
      }
    },
    ...
  },
  ...
}
β•·
β”‚ Warning: Backend configuration ignored
β”‚ 
β”‚   on .terraform/modules/MyStackModule/my_stack/cdk.tf.json line 1915, in terraform.backend:
β”‚ 1915:       "local": {
β”‚ 
β”‚ Any selected backend applies to the entire configuration, so Terraform expects provider configurations only in the root module.
β”‚ 
β”‚ This is a warning rather than an error because it's sometimes convenient to temporarily call a root module as a child module for testing purposes, but this backend configuration block will have no effect.
β•΅
jsteinich commented 2 years ago

The provider block needing to exist is mentioned here. More specifically defining providers causes them to appear in both required_providers and provider within the output. The latter could potentially be omitted if empty, but doing some might obscure errors if configuration is required. Could also potentially infer providers from resources and automatically add to required_providers. I worry about having too much hidden behavior with those options. Perhaps an explicit flag on the provider to omit would be better.

The backend warning is a result of https://github.com/hashicorp/terraform-cdk/pull/1461.

Perhaps we could allow the user to specify at the stack level that the output is intended to be used as a module and have that make any tweaks to synthesizing to make that work better.

skorfmann commented 2 years ago

Perhaps we could allow the user to specify at the stack level that the output is intended to be used as a module and have that make any tweaks to synthesizing to make that work better.

This would really have to be on a provider basis, since you might have other providers which are dedicated to that module. What would this special synth mode do then?

Could also potentially infer providers from resources and automatically add to required_providers.

This would be a lot of magic and I think user config might be required more often than not (don't have hard data on this though).

github-actions[bot] commented 1 year ago

Hi there! πŸ‘‹ We haven't heard from you in 30 days and would like to know if the problem has been resolved or if you still need help. If we don't hear from you before then, I'll auto-close this issue in 30 days.

marengaz commented 1 year ago

unresolved

shinebayar-g commented 1 week ago

We need this so bad to make cdktf + terraform hcl usage possible in our terraform workspaces. Writing terraform modules in CDKTF and using that in our existing TFE repositories is the most common use case.

Can't https://github.com/cdktf/cdktf-tf-module-stack integrated into cdktf itself? Currently HCL output is broken in this package.