Coder Social home page Coder Social logo

cdktf-aznaming-output-failure-example's Introduction

  • 2024-02-05 - Workaround using cdktf.Fn_Lookup(..) example added.
  • Example code to reproduce the issue described in hashicorp/terraform-cdk#3477

Summary

This demonstrates an issue with the creation of the cdktf bindings for the "Azure/naming/azurerm" module (https://github.com/Azure/terraform-azurerm-naming). The root of the issue is that the language specific functions generated for this module yields a token that references a key->value map type, and not the actual output (string) value.

For example, calling ResourceGroupOutput() yields ${module.resourceNaming.resource_group}, where the resource_group output value is actually a map. When running tf plan, we're greeted with the error module.resourceNaming.resource_group is object with 8 attributes

The module reference in question can be found at main.tf and output.tf

Generated cdktf.json is

        "name": "${module.resourceNaming.resource_group}"

Needed cdktf.json is

        "name": "${module.resourceNaming.resource_group[\"name\"]}"

To reproduce

cdktf get # fetch the Go TF bindings
go run main.go # build, compile and run the module. This performs the Synth()..
cd cdktf.out/stacks/naming-output-failure-example # the cdk.tf.json is placed within
terraform init # use a local statefile for this test
terraform plan # yield the error

View tf plan results

$ terraform plan
...
Plan: 2 to add, 0 to change, 0 to destroy.
╷
│ Error: Incorrect attribute value type
│
│   on cdk.tf.json line 44, in resource.azurerm_resource_group.resource_group:
│   44:         "name": "${module.resourceNaming.resource_group}"
│     ├────────────────
│     │ module.resourceNaming.resource_group is object with 8 attributes
│
│ Inappropriate value for attribute "name": string required.

Example of the Synth'd cdk.tf.json

The following block of json is computed during Synth() execution. The ResourceGroupOutput() call yields "name": "${module.resourceNaming.resource_group}" which is a reference to the object (with 8 keys) instead of direct access to the map or name key.

Ideally ResourceGroupOutput() returns *map[string]interface{} that would allow us to fetch our desired key eg name.

  "resource": {
    "azurerm_resource_group": {
      "resource_group": {
        "//": {
          "metadata": {
            "path": "naming-output-failure-example/resource_group",
            "uniqueId": "resource_group"
          }
        },
        "location": "Canada Central",
        "name": "${module.resourceNaming.resource_group}"
      }
    }
  },

Workaround

See comment at hashicorp/terraform-cdk#3477 (comment)

Workaround using cdktf.Fn_Lookup, the function states: retrieves the value of a single element from a map, given its key. If the given key does not exist, the given default value is returned instead.

Using the following will generate the correct cdk.tf.json output. For example... Notice the name field is now referenced correctly.

      "resource_group_workaround": {
        "//": {
          "metadata": {
            "path": "naming-output-failure-example/resource_group_workaround",
            "uniqueId": "resource_group_workaround"
          }
        },
        "location": "Canada Central",
        "name": "${module.resourceNaming.resource_group.name}"
      }

Added in main.tf

	resourcegroup.NewResourceGroup(stack, jsii.String("resource_group_workaround"), &resourcegroup.ResourceGroupConfig{
		Name:     cdktf.Token_AsString(cdktf.Fn_Lookup(n.ResourceGroupOutput(), jsii.String("name"), nil), nil),
		Location: jsii.String("Canada Central"),
	})

cdktf-aznaming-output-failure-example's People

Contributors

jesseward avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.