hashicorp / terraform-provider-azurerm

Terraform provider for Azure Resource Manager
https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs
Mozilla Public License 2.0
4.53k stars 4.6k forks source link

App Service : connection_string type confusion #2892

Closed Djiit closed 5 years ago

Djiit commented 5 years ago

Community Note

Terraform (and AzureRM Provider) Version

Terraform v0.11.11

Affected Resource(s)

Terraform Configuration Files

# in module/main.tf
resource "azurerm_app_service" "app_service" {
  name                    = "${var.app_name}"
  location                = "${var.location}"
  resource_group_name     = "${var.resource_group_name}"
  app_service_plan_id     = "${var.app_service_plan_id}"
  client_affinity_enabled = false
  https_only              = true

  app_settings = "${merge(
    map("WEBSITES_ENABLE_APP_SERVICE_STORAGE", false),
    var.app_settings)}"

  connection_string = "${var.connection_string}"

  # ...
}

# in main.tf
module "service" {
  source                          = "../../modules/mymodule"
  # ...
  connection_string {
    name  = "defaultConnection"
    type  = "MySQL"
    value = "<string_with_interpolations>"
  }
}

Debug Output

2019/02/14 14:36:40 [DEBUG] Setting Unknown Variable Value for computed key: connection_string.value
2019/02/14 14:36:40 [TRACE] root: eval: *terraform.EvalCoerceMapVariable
2019/02/14 14:36:40 [DEBUG] Resource state not found for "module.service.azurerm_app_service.app_service": module.service.azurerm_app_service.app_service
2019/02/14 14:36:40 [DEBUG] ReferenceTransformer: "module.service.azurerm_app_service.app_service" references: []
2019/02/14 14:36:40 [TRACE] Graph after step *terraform.ReferenceTransformer:

module.service.azurerm_app_service.app_service - *terraform.NodeValidatableResourceInstance
2019/02/14 14:36:40 [ERROR] root.service: eval: *terraform.EvalValidateResource, err: Warnings: []. Errors: [connection_string: should be a list]
2019/02/14 14:36:40 [ERROR] root.service: eval: *terraform.EvalSequence, err: Warnings: []. Errors: [connection_string: should be a list]
2019/02/14 14:36:40 [DEBUG] Resource state not found for "module.service.azurerm_app_service_custom_hostname_binding.onedior_hostname_binding": module.service.azurerm_app_service_custom_hostname_binding.onedior_hostname_binding
2019/02/14 14:36:40 [DEBUG] ReferenceTransformer: "module.service.azurerm_app_service_custom_hostname_binding.onedior_hostname_binding" references: []
2019/02/14 14:36:40 [TRACE] Graph after step *terraform.ReferenceTransformer:

module.service.azurerm_app_service_custom_hostname_binding.onedior_hostname_binding - *terraform.NodeValidatableResourceInstance
2019/02/14 14:36:40 [TRACE] Graph after step *terraform.RootTransformer:

module.service.azurerm_app_service_custom_hostname_binding.onedior_hostname_binding - *terraform.NodeValidatableResourceInstance
2019/02/14 14:36:40 [TRACE] dag/walk: added new vertex: "module.service.azurerm_app_service_custom_hostname_binding.onedior_hostname_binding"
2019/02/14 14:36:40 [TRACE] [walkValidate] Exiting eval tree: module.service.azurerm_dns_cname_record.dns_cname_record
2019/02/14 14:36:40 [TRACE] vertex 'root.module.service.azurerm_dns_cname_record.dns_cname_record': expanding/walking dynamic subgraph
2019/02/14 14:36:40 [TRACE] dag/walk: walking "module.service.azurerm_app_service_custom_hostname_binding.onedior_hostname_binding"
2019/02/14 14:36:40 [TRACE] vertex 'root.service.module.service.azurerm_app_service_custom_hostname_binding.onedior_hostname_binding': walking
2019/02/14 14:36:40 [TRACE] Graph after step *terraform.ResourceCountTransformer:

module.service.azurerm_dns_cname_record.dns_cname_record - *terraform.NodeValidatableResourceInstance
2019/02/14 14:36:40 [DEBUG] Resource state not found for "module.service.azurerm_dns_cname_record.dns_cname_record": module.service.azurerm_dns_cname_record.dns_cname_record
2019/02/14 14:36:40 [TRACE] Graph after step *terraform.AttachStateTransformer:

module.service.azurerm_dns_cname_record.dns_cname_record - *terraform.NodeValidatableResourceInstance
2019/02/14 14:36:40 [TRACE] vertex 'root.service.module.service.azurerm_app_service_custom_hostname_binding.onedior_hostname_binding': evaluating
2019/02/14 14:36:40 [TRACE] Graph after step *terraform.TargetsTransformer:

module.service.azurerm_dns_cname_record.dns_cname_
2019/02/14 14:36:40 [DEBUG] plugin: waiting for all plugin processes to complete...
Error: module.service.azurerm_app_service.app_service: connection_string: should be a list

Expected Behavior

App Service should have been created with a correct connection_string attached.

Actual Behavior

Saw this error :

Error: module.service.azurerm_app_service.app_service: connection_string: should be a list

Steps to Reproduce

  1. create a module with a app_service including a connection_string. Connection string should use a variable.
  2. Use this module in another main.tf, passing a map to connection_string.
  3. Appy or plan
Djiit commented 5 years ago

FYI : when not using interpolation with other resource (say, just var.*) in connection_string.value; things are fine. It might be related to some dependancy-management stuff.

tombuildsstuff commented 5 years ago

hi @Djiit

Thanks for opening this issue :)

Taking a look into this, you're seeing the following error:

`Error: module.service.azurerm_app_service.app_service: connection_string: should be a list`

because a map is being provided to the module and not a list (which in turn means this configuration isn't valid). Unfortunately Terraform 0.11 doesn't allow a list of maps to be provided as input, as such this approach won't work at this time; however this should be possible in Terraform 0.12 using Rich Value Types.

Instead for the moment I'd suggest the best approach would be to pass these through as regular variables - for example:

module "app_service" {
  source = "..."
  default_connection_string = "Server=127.0.0.1; User ID=efvfewfqsfv; Password=rgfedfsvdfsfd"
}

# and within the module
variable "default_connection_string" {}
resource "azurerm_app_service" "app_service" {
  # ...
  connection_string {
    name = "defaultConnection"
    type = "MySQL"
    value = "${var.default_connection_string}"
  }
}

Would you be able to take a look and see if that works for you? Whilst it's unfortunate this approach requires defining a variable for each connection string in the short-term - the recommended approach isn't available until Terraform 0.12 comes out - but I believe that should work when this is released. Since this is a question about Terraform Configuration rather than a bug in Terraform I'm going to close this issue for the moment, however please let us know if that doesn't work for you and we'll take another look :)

Thanks!

ghost commented 4 years 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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. If you feel I made an error 🤖 🙉 , please reach out to my human friends 👉 hashibot-feedback@hashicorp.com. Thanks!