How to convert HCL for loop

Hello,

I’m trying to create a Fastly fastly_service_dictionary_items resource but I don’t know how to convert this to typescript:

resource "fastly_service_dictionary_items" "items" {
  for_each = {
  for d in fastly_service_vcl.myservice.dictionary : d.name => d if d.name == var.mydict_name
  }
  service_id = fastly_service_vcl.myservice.id
  dictionary_id = each.value.dictionary_id

  items = {
    key1: "value1"
    key2: "value2"
  }
}

The cdktf convert command generates:

 const itemsForEachIterator = TerraformIterator.fromList(
      Token.asAny(
        "${{ for d in ${" +
          myservice.dictionary +
          '} : d.name => d if d.name == "configuration"}}'
      )
    );
    new ServiceDictionaryItems(this, "items", {
      dictionaryId: Token.asString(
        Fn.lookupNested(itemsForEachIterator.value, ["dictionary_id"])
      ),
      items: {
        key1: "value1",
        key2: "value2",
      },
      serviceId: myservice.id,
      forEach: itemsForEachIterator,
    });

But synthesizing returns the following errors:

⠹  Synthesizing
[2023-09-12T09:45:16.685] [ERROR] default - You're using escape sequences (${...}) with CDKTF Built-in functions. This is not supported yet, and the output may be incorrect.

[2023-09-12T09:45:16.686] [ERROR] default - ${{ for d in ${${TfToken[TOKEN.1]}} : d.name => d if d.name == var.mydict_name}}

And:


 Error: Invalid character
 
   on cdk.tf.json line 28, in resource.fastly_service_dictionary_items.items (items):
   28:         "for_each": "${toset(${{ for d in ${tolist(fastly_service_vcl.myservice (myservice).dictionary)} : d.name => d if d.name == var.mydict_name}})}",
 
 This character is not used within the language.


 Error: Invalid expression
 
   on cdk.tf.json line 28, in resource.fastly_service_dictionary_items.items (items):
   28:         "for_each": "${toset(${{ for d in ${tolist(fastly_service_vcl.myservice (myservice).dictionary)} : d.name => d if d.name == var.mydict_name}})}",
 
 Expected the start of an expression, but found an invalid expression token.

Any help would be appreciated. Thanks!

I would probably suggest going back to overrides rather than an iterator.
Something along the lines of

let dict = new ServiceDictionaryItems(this, "items", {
      dictionaryId: "each.value.dictionary_id,
      items: {
        key1: "value1",
        key2: "value2",
      },
      serviceId: myservice.id
    });

dict.addOverride("for_each", "${{for d in fastly_service_vcl.myservice.dictionary : d.name => d if d.name == var.mydict_name}}");

Thanks jsteinich, I’ll try it.