hashicorp / terraform-provider-chef

Terraform Chef provider
https://www.terraform.io/docs/providers/chef/
Mozilla Public License 2.0
12 stars 34 forks source link

Chef provisioner fails when using "for_each" and "each" for resources #33

Open kszlachcikowski opened 5 years ago

kszlachcikowski commented 5 years ago

Hi,

There seems to be an issue with the chef provisioner when generating resources using "for_each" (new in terraform 0.12.6). Whenever the "each" variable is used within the chef provisioner block of the resource to dynamically fill out attributes (eg. "node_name") terraform fails during the apply phase (after the vm instance is created but not yet provisioned) with the following error:

Error: 2 problems:

- Reference to "each" in context without for_each: The "each" object can be used only in "resource" blocks, and only when the "for_each" argument is set.
- Reference to "each" in context without for_each: The "each" object can be used only in "resource" blocks, and only when the "for_each" argument is set.

Terraform Version

Terraform v0.12.6

Affected Resource(s)

azurerm_virtual_machine

Terraform Configuration Files

# Provisioner block from the azurerm_virtual_machine
# A list of server variables (name, size, etc.) are provided as a map to the "for_each" attribute
# Creating VM resources without the provisioner block works correctly
provisioner "chef" {
    node_name = each.key
    server_url = "https://chef.XXX.io/organizations/XXX/"
    user_key = var.chef_provisioner_private_key
    user_name = var.chef_admin_username
    channel = "stable"
    version = "15.1.36"
    client_options = ["chef_license 'accept'"]
    attributes_json = jsonencode({"cache_db_name" = join("_", ["pgcache", each.value.region, each.value.tenant])})
    recreate_client = true
    secret_key = var.chef_provisioner_databag_key
    ssl_verify_mode = ":verify_none"
    connection {
      host = azurerm_public_ip.pgsqlcache-ip[each.key].fqdn
      private_key = var.ssh_admin_private_key
      type = "ssh"
      user = var.ssh_admin_username
    }
    run_list = ["postgresql"]
  }

Expected Behavior

1 or more VM's should be created and provisioning applied to all of them using the "each" variable to provide individual VM details.

Actual Behavior

The "apply" is run and VM instances created but no provisioning occurs. The apply run ends with:

Error: 2 problems:

- Reference to "each" in context without for_each: The "each" object can be used only in "resource" blocks, and only when the "for_each" argument is set.
- Reference to "each" in context without for_each: The "each" object can be used only in "resource" blocks, and only when the "for_each" argument is set.

Steps to Reproduce

  1. terraform apply

References

https://github.com/hashicorp/terraform/issues/17179

pselle commented 5 years ago

Hi @kszlachcikowski! Thanks for the report! There are known issues with for_each in provisioners (https://github.com/hashicorp/terraform/issues/22289) and this fix will go out in 0.12.7, which hopefully changes your issue's status :)

pselle commented 5 years ago

Terraform 0.12.7 is now released! https://github.com/hashicorp/terraform/blob/v0.12.7/CHANGELOG.md