Reputation: 3938
I have a terraform folder structure like below. I am accessing an output variable from 01_app
module to the 02_tenant
module. However the output variable is not accessible across modules.
I am managing two different state files for these two folders. Can someone help me in accessing the output variables across modules.
I have declared a output variable in 01_app/main_infra_app.tf
:
output "key_vault_id" {
value = "${azurerm_key_vault.key_vault.id}"
}
02_tenant/variables.tf
:
variable "key_vault_id" {
}
I have added below block in 02_tenant/main_infra_app.tf
:
module "kv" {
source = "./01_app"
key_vault_value_name = "${01_app.key_vault_id}"
}
[EDIT: 01] This is the error log.
Terraform v0.12.9
2019-09-23T17:15:39.9624707Z [command]C:\hostedtoolcache\windows\terraform\0.12.9\x64\terraform.exe init --backend-config=d:\a\r1\a/_Infrastructure/terraform/02_tenant/var/backend.tfvars --reconfigure
2019-09-23T17:15:40.0555160Z There are some problems with the configuration, described below.
2019-09-23T17:15:40.0555674Z
2019-09-23T17:15:40.0556565Z The Terraform configuration must be valid before initialization so that
2019-09-23T17:15:40.0556859Z Terraform can determine which modules and providers need to be installed.
2019-09-23T17:15:40.0557069Z
2019-09-23T17:15:40.0557287Z Error: Extra characters after interpolation expression
2019-09-23T17:15:40.0557482Z
2019-09-23T17:15:40.0557678Z on main_infra_tenant.tf line 42, in module "x":
2019-09-23T17:15:40.0557881Z 42: key_vault_value_name = "${01_app.key_vault_id}"
2019-09-23T17:15:40.0558082Z
2019-09-23T17:15:40.0558277Z Expected a closing brace to end the interpolation expression, but found extra
2019-09-23T17:15:40.0558484Z characters.
2019-09-23T17:15:40.0558656Z
2019-09-23T17:15:40.0696178Z ##[error]Terraform command 'init' failed with exit code '1'.: Extra characters after interpolation expression
[EDIT 2] I have added below block in 02_tenant/main_infra_tenant.tf. As I have naming KV and RG conventions, I am now able to fetch the kv_id in second .tf file.
data "azurerm_key_vault" "kv" {
name = var.kv_name
resource_group_name = var.rg_name
}
# Write to Azure KV
resource "azurerm_key_vault_secret" "kv_secret_for_key" {
name = var.key_name
value = base64encode(random_string.generate_key.result)
key_vault_id = "${data.azurerm_key_vault.kv.id}"
}
Upvotes: 2
Views: 2780
Reputation: 16806
It looks like your current code is using the key_vault_id
output from the 01_app
module as an input to initialize the module itself. You should instead use the key_vault_id
output from 01_app
in another piece of infrastructure.
Try doing something like this instead to use 01_app
's output variable key_vault_id
in 02_tenant
:
In 02_tenant/main_infra_app.tf
:
module "kv" {
source = "./01_app"
key_vault_value_name = "somevalue"
}
data "azurerm_key_vault_secret" "test" {
name = "key-vault-name"
key_vault_id = "${module.kv.key_vault_id}"
}
So now your code uses 01_app
's output key_vault_id
in the 02_tenant
module.
Upvotes: 1