cloudamqp / terraform-provider-cloudamqp

Terraform Provider for CloudAMQP
https://registry.terraform.io/providers/cloudamqp/cloudamqp
Mozilla Public License 2.0
35 stars 39 forks source link

datasource cloudamqp_credentials is not handling (known after apply) values correctly #114

Closed floge07 closed 3 years ago

floge07 commented 3 years ago

Terraform: v1.0.7 Cloudamqp: v1.9.2

I'm trying to create a cloudamqp_instance and to write the cloudamqp_credentials into a azure keyvault as a secret.

The source code looks like this (simplified):

resource "cloudamqp_instance" "tenant_rmq_instance" {
  name = "tenant_abcdef"
  plan = "lemur"
  region = "amazon-web-services::eu-west-1"
}

data "cloudamqp_credentials" "tenant_rmq_credentials" {
  instance_id = cloudamqp_instance.tenant_rmq_instance.id
}

resource "azurerm_key_vault_secret" "tenant_secret_rmq_mgmt_user" {
  key_vault_id = azurerm_key_vault.tenant_keyvault.id
  name = "rabbitmq-management-username"
  value = data.cloudamqp_credentials.tenant_rmq_credentials.username
}

I get this error on terraform plan:

│ Error: Missing required argument
│ 
│   with azurerm_key_vault_secret.tenant_secret_rmq_mgmt_user,
│   on cloudamqp.tf line 38, in resource "azurerm_key_vault_secret" "tenant_secret_rmq_mgmt_user":
│   38:   value = data.cloudamqp_credentials.tenant_rmq_credentials.username
│ 
│ The argument "value" is required, but no definition was found.

As you can see in the code the "value" of the azurerm_key_vault_secret has a definition. I think the problem is that the credentials are unknown since the instance has yet to be created.

The terraform documentation (https://www.terraform.io/docs/language/expressions/references.html) mentions this:

If unknown values are used in the configuration of a data resource, that data resource cannot be read during the plan phase and so it will be deferred until the apply phase. In this case, the results of the data resource will also be unknown values.

In my case, if I write the host of the cloudamqp_instance.tenant_rmq_instance into the keyvault, it works. So it can't be a general issue with the azurerm_key_vault_secret. It even works if I add the cloudamqp_instance as a datasource and use it instead of the resource. Just the cloudamqp_credentials datasource seems broken.

If I remove this part, plan & apply (thus creating the instance) and re-add the credentials, everything works (because the values are now known).

I'm certain that I configured everything correctly. This seems to be a problem in this provider, specifically the cloudamqp_credentials datasource.

dentarg commented 3 years ago

Maybe related to the change https://github.com/cloudamqp/terraform-provider-cloudamqp/commit/9d733796a93c5421d7163054a022e57a58ec354a

floge07 commented 3 years ago

Somehow I did not see the new releases. Maybe I had the provider documentation tab opened too long... I added the original versions to my post.

I upgraded to 1.10.0 and the problem is solved. Sorry about the confusion.