Reputation: 43
I am very new to Terraform, and programming, however, I have to develop my skills, so I would appreciate your suggestion to have the correct output.tf file.
In my main.tf, I wrote:
terraform {
required_version = ">=0.12"
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~>2.0"
}
}
}
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "RG1" {
name = "AZ-VNET"
location = var.resource_group_location
}
resource "azurerm_resource_group" "RG2" {
name = "AZ-Migrate"
location = var.resource_group_location
}
In my output.tf I wrote:
output "resource_group_name" {
value = azurerm_resource_group.RG*.name
}
The result is not printing both Resource Groups names.
Upvotes: 3
Views: 20073
Reputation: 23
You can also try this :
output"resource_group_name{value=azurerm_resource_group.RG*.access_ip
}
Upvotes: -3
Reputation: 908
You can have multiple resources in a single output, but to make it work, you need to use some terraform function, or format the output depending on the type, if it is a list, string, map.. When you have a chance, take a look on https://www.terraform.io/language/functions -- I'll show an example that you will have a list of the ids.
1 - By the way, we can't use * in a string like a wildcard, the * is used mostly when we are working with modules, or dynamic resources or your resources that outputs a list, but in any case, putting a * in the resource name will not work. We use *
before the attribute from a resource, let's say we have a module that creates an ABC
resource and this resource has an id
attribute. It would be module.ABC.*.id
- but it doesn't work for everything, you need to know what's the type of the output, if it is a string, a list, a map as I said.
3 - I don't work with Azure, but If you go to the doc of that resource on https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/resources/resource_group -- You can see the only exported attribute from this resource is id
. Given that, you can do something like:
output "resource_group_name" {
value = [azurerm_resource_group.RG1.id, azurerm_resource_group.RG2.id]
}
Now let's say you really need to output the name of these resources, you can create locals
block to define the name of the resources, and then you will be able to export them querying the locals
values, like:
locals {
azurerm_resource_group_rg1 = "AZ-VNET"
azurerm_resource_group_rg2 = "AZ-Migrate"
}
resource "azurerm_resource_group" "RG1" {
name = local.azurerm_resource_group_rg1
location = var.resource_group_location
}
resource "azurerm_resource_group" "RG2" {
name = local.azurerm_resource_group_rg2
location = var.resource_group_location
}
And an output with:
output "resource_group_name" {
value = [local.azurerm_resource_group_rg1, local.azurerm_resource_group_rg2]
}
But don't do that now, I don't know if they would be recreated, I don't know these resources. Just do this if you are good with this.
Let me know if you got it.
Upvotes: 7