Error: Cycle - pass each.key value to shell script

Have a such configuration with for_each loop
I would like to pass availability_zone variables accordingly to provisioned shell script
got an err
Error: Cycle: module.proxyserver-instance.aws_instance.sa_proxy_server[“af-south-1c”], module.proxyserver-instance.aws_instance.sa_proxy_server[“af-south-1a”], module.proxyserver-instance.aws_instance.sa_proxy_server[“af-south-1b”]

variable "region" { default = "af-south-1" }
variable "sa_zones" {
  type = map(any)
  default = {
    "af-south-1a" = "subnet-a36e81337"
    "af-south-1b" = "subnet-9b2821337"
    "af-south-1c" = "subnet-4b6a41337"
resource "aws_instance" "sa_proxy_server" {

  for_each = var.region == "af-south-1" ? var.sa_zones : {}
  availability_zone = each.key
  ami               = "ami-075ce95d3f9ecc1337"
  instance_type     = var.instance_type
  subnet_id         = each.value
  security_groups   = ["sg-01c0e662075c1337"]
  tags = {
    Name = "${var.hostname}-${each.key}"
  provisioner "local-exec" {
  # Add proxy hostname to required locations
  command = "./ ${aws_instance.sa_proxy_server.*.availability_zone}"

Is there any possibility to pass separate each key for shell script ?

Hi @eliteaz,

Each aws_resource instance is referring to every other instance, which creates dependency cycles. If you need to refer to the individual instance’s attributes in a provisioner, you can use the self identifier.

The attributes you are trying to refer to are simply the keys of the map used in the for_each expression, so instead of referring to all instances, you can refer to the same map to extract the keys with something like keys(var.sa_zones)

Your template expression should also be explicit how you want the values formatted. If you expect json, then you could use:

command = "./ '${jsonencode(keys(var.sa_zones))}'"

Thanks a lot @jbardin for Your answer.
self identifier solved a problem as well.

but i think that jsonencode could help also if there is some keys indexing ?
with jsonencode i get

module.proxyserver-instance.aws_instance.sa_proxy_server["af-south-1a"]: Provisioning with 'local-exec'...
module.proxyserver-instance.aws_instance.sa_proxy_server["af-south-1a"] (local-exec): Executing: ["/bin/sh" "-c" "./  '[\"af-south-1a\",\"af-south-1b\"]'"]

I don’t know exactly, but afaik there should be a for loop solution for variable keys ?