I have a terraform script, which tries to refer to the attribute in the same resource. I need to pass "azurerm_container_group.aci_caddy.fqdn" to the container instance inside the container group.
resource "azurerm_container_group" "aci_caddy" {
resource_group_name = "aci_caddy"
location = local.location
name = "aci_caddy"
os_type = "Linux"
dns_name_label = "aci-caddykang"
container {
name = "app"
image = "apptest:latest"
cpu = "0.5"
memory = "0.5"
environment_variables = {
SITE_ADDRESS = "${azurerm_container_group.aci_caddy.fqdn}"
}
}
}
but, I got the error message.
│ Error: Self-referential block
│
│ on aci-caddy-main.tf line 29, in resource "azurerm_container_group" "aci_caddy":
│ 29: SITE_ADDRESS = "${azurerm_container_group.aci_caddy.fqdn}"
│
│ Configuration for azurerm_container_group.aci_caddy may not refer to itself.
Is there a way to accomplish it? Thanks.
you should be able to export SITE_ADDRESS using your dns_name_label. So, instead of hardcoding dns_name_label. You can create a variable in Terraform
variable "dns_label" {
type = string
}
And then refer to this variable in your container group resource.
resource "azurerm_container_group" "aci_caddy" {
resource_group_name = "aci_caddy"
location = local.location
name = "aci_caddy"
os_type = "Linux"
dns_name_label = var.dns_label
container {
name = "app"
image = "apptest:latest"
cpu = "0.5"
memory = "0.5"
environment_variables = {
SITE_ADDRESS = "${var.dns_label}-westus.azurecontainer.io"
}
}
}
If you want to further improve this, you can figure out how you can get the region/location from a azurerm provider data source. I am not exposed to azure provider, so couldn't find this quickly. But I know for a fact that FQDN is derived using your dns_name_label and .azurecontainer.io.