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.83k stars 448 forks source link

Documentation: Complex Iterator example fails on plan #3157

Closed Maed223 closed 11 months ago

Maed223 commented 11 months ago

Description

Currently our example is as so:

const complexIterator = TerraformIterator.fromList([
      {
        name: "website-static-files",
        tags: { app: "website" },
      },
      {
        name: "images",
        tags: { app: "image-converter" },
      },
    ] as any);

    new S3Bucket(this, "complex-iterator-bucket", {
      forEach: complexIterator,
      bucket: complexIterator.getString("name"),
      tags: complexIterator.getStringMap("tags"),
    });

Which synths fine (and it why we haven't caught it) but fails on plan with the following error:

│ Error: Invalid for_each set argument
│
│   on cdk.tf.json line 28, in resource.aws_s3_bucket.complex-iterator-bucket (complex-iterator-bucket):
│   28:         "for_each": "${toset([{\"name\" = \"website-static-files\", \"tags\" = {\"app\" = \"website\"}}, {\"name\" = \"images\", \"tags\" = {\"app\" = \"image-converter\"}}])}",
│
│ The given "for_each" argument value is unsuitable: "for_each" supports maps
│ and sets of strings, but you have provided a set containing type object.

Debugged it and got the following which works:

const complexIterator = TerraformIterator.fromMap({
      "website-static-files": { name: "website-static-files", tags: { app: "website" } },
      "images": { name: "images", tags: { app: "image-converter" } }
});

new S3Bucket(this, "complex-iterator-bucket", {
  forEach: complexIterator,
  bucket: complexIterator.getString("name"),
  tags: complexIterator.getStringMap("tags"),
})

Links

https://developer.hashicorp.com/terraform/cdktf/concepts/iterators#using-iterators-on-complex-types:~:text=Using%20Iterators%20on%20Complex%20Types

Help Wanted

Community Note

xiehan commented 11 months ago

Duplicate of #2452

github-actions[bot] commented 10 months ago

I'm going to lock this issue because it has been closed for 30 days. This helps our maintainers find and focus on the active issues. If you've found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.