Repository: hashicorp/terraform-cdk

Issue #2341: Variable names clobbered, unusable
Created: 2022-11-24T16:02:22Z
Community Note

Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
If you are interested in working on this issue or have submitted a pull request, please leave a comment


cdktf & Language Versions
Affected Resource(s)
Variables
Debug Output
Expected Behavior
I should be able to provide a variable aws_secret_key which will be used by my stack.
Actual Behavior

The variable aws_secret_key is ignored (I spent a while trying to figure out why -var wasn't working, -var-file, etc)
The actual variable it looks for is named localdev_awssecretkey_AFAB001C

Steps to Reproduce
With the variable defined as (Java):
    final TerraformVariable awsSecretKey =
        new TerraformVariable(
            scope,
            "aws_secret_key",
            TerraformVariableConfig.builder()
                .type("string")
                .sensitive(true)
                .nullable(false)
                .build());

Important Factoids
References

Duplicate issue #2344: Make Terraform CDK and Terraform interoperable (without the CDKTF CLI)
Created: 2022-11-24T17:06:05Z
Community Note

Please vote on this issue by adding a 👍 reaction to the original issue to help the community and maintainers prioritize this request
Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
If you are interested in working on this issue or have submitted a pull request, please leave a comment


Description
When you write Terraform CDK code (personally only used Go), the main function runs Synth() at the end which generates a cdk.tf.json. This json can be used by both terraform to plan and apply and also by the cdktf cli to diff and deploy.
However they cannot be used independently.
Consider the following scenario:

go run . (Synth)
terraform init (From inside the folder which contains cdk.tf.json)
terraform plan (From inside the folder which contains cdk.tf.json)
cdktf diff
terraform plan (From inside the folder which contains cdk.tf.json)

Step 3 and Step 4 are going to generate completely different resource names (and therefore different diff/plan) however Step 4 and Step 5 are going to generate the same diff/plan
This makes it not possible to use the Terraform CLI without the CDKTF CLI as well.
It would be very nice to be able to write terraform CDK code and not necessarily have to rely on the cdktf cli (which came as a surprise to me as I thought they were interoperable) which would avoid dependencies in Node, possibilities to use community tooling built around Terraform, json output and a lot more.
References
Here's a simple example that reproduces this
package main

import (
	"github.com/aws/jsii-runtime-go"
	"github.com/cdktf/cdktf-provider-null-go/null/v4/provider"
	"github.com/cdktf/cdktf-provider-null-go/null/v4/resource"
	"github.com/hashicorp/terraform-cdk-go/cdktf"
)

func main() {
	app := cdktf.NewApp(nil)

	stack := cdktf.NewTerraformStack(app, jsii.String("my-stack"))

	provider.NewNullProvider(stack, jsii.String("null_provider"), &provider.NullProviderConfig{})

	resource.NewResource(stack, jsii.String("dummy"), &resource.ResourceConfig{})

	app.Synth()
}
On step 3 we get

On step 4 we get

On Step 5 we get

Closing comment (2022-11-25T09:34:57Z): Hi @alethenorio 👋
It seems like you are running into the same problem as reported in #2341