Output CSV file using terraform - terraform

I'm trying to use terraform variable data (CSV file) to create a resource group and the name of the resource group are added into the CSV file.
I'm currently experiencing the below error.
provider "azurerm" {
features{}
}
locals {
resource_groupname = csvdecode(file("./test.csv"))
}
resource "azurerm_resource_group" "Main" {
count = length(locals.resource_groupname)
name = locals.resource_groupname[count.index].groupname
location = "North europe"
}
Error Message
Error: Reference to undeclared resource
│
│ on testvariable.tf line 10, in resource "azurerm_resource_group" "Customer11":
│ 10: count = length(locals.groupname)
│
│ A managed resource "locals" "groupname" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared resource
│
│ on testvariable.tf line 11, in resource "azurerm_resource_group" "Customer11":
│ 11: name = data.locals.groupname[count.index].groupname
│
│ A data resource "locals" "groupname" has not been declared in the root module.
╵
Updated Error Messgae
╷
│ Error: Reference to undeclared resource
│
│ on testtf.tf line 10, in resource "azurerm_resource_group" "Main":
│ 10: count = length(locals.resource_groupname)
│
│ A managed resource "locals" "resource_groupname" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared resource
│
│ on testtf.tf line 11, in resource "azurerm_resource_group" "Main":
│ 11: name = locals.resource_groupname[count.index].groupname
│
│ A managed resource "locals" "resource_groupname" has not been declared in the root module.

Your code should be (assuming this time you posted correct code):
resource "azurerm_resource_group" "Main" {
count = length(local.resource_groupname)
name = local.resource_groupname[count.index].groupname
location = "North europe"
}
Since ./test.csv is not shown its difficult to speculate on its content and the use in your code.

Suppose your CSV file has headers like
name, location, ABC, XXY, CDF, your
then you use for_each as well here
locals {
resource_groupname = csvdecode(file("./test.csv"))
}
resource "azurerm_resource_group" "Main" {
for_each = { for inst in locals.resource_groupname) : inst.location=> inst
}
name = each.value.name
location = each.value.location
}

Related

Migrating Azure Key Vault secrets from one Azure subscription to another

I have multiple Azure subscriptions, and I'm working on getting the key vault from one to another. I've written a terraform program to do this. Here I'm calling the data blocks and using for each loop condition from one subscription and using the output value of those as input values in another subscription. But I'm getting the error please help. Below is the code
data "azurerm_key_vault" "ewo1" {
provider = azurerm.demo-eworx-terraform-automation
name = "demo-eworx-keyvault"
resource_group_name = "demo-eworx-rg"
}
output "vault_uri_ewo1" {
value = data.azurerm_key_vault.ewo1.vault_uri
}
data "azurerm_key_vault_secret" "ewo1" {
provider = azurerm.demo-eworx-terraform-automation
for_each = toset(["demo-eworx-terraform-automation-client-secret", "demo-eworx-terraform-automation-client-id", "demo-eworx-terraform-automation-tenant-id", "demo-eworx-terraform-automation-subscription-id"])
name = each.key
key_vault_id = data.azurerm_key_vault.ewo1.id
}
output "secret_value" {
value = [ for secret in data.azurerm_key_vault_secret.ewo1 : secret.name]
}
data "azurerm_key_vault" "ewo11" {
provider = azurerm.terraform-automation
name = "demo-bteb-keyvault"
resource_group_name = "bteb-demo-work"
}
output "vault_uri_ewo11" {
value = data.azurerm_key_vault.ewo11.vault_uri
}
resource "azurerm_key_vault_secret" "ewo11" {
provider = azurerm.terraform-automation
for_each = toset(["demo-eworx-terraform-automation-client-secret", "demo-eworx-terraform-automation-client-id", "demo-eworx-terraform-automation-tenant-id", "demo-eworx-terraform-automation-subscription-id"])
name = each.key
value = [ for secret in data.azurerm_key_vault_secret.ewo1 : secret.name]
key_vault_id = data.azurerm_key_vault.ewo11.id
}
I need to migrate azure key vaults secrets from one subscription to another subscription. I have written the above terraform code, I'm passing the data "azurerm_key_vault" "ewo1" output value as resource "azurerm_key_vault_secret" "ewo11" value input. But getting below error.
Can some check and help me in solving the issue
error message:
Error: Incorrect attribute value type
│
│ on demo-keyvault-migration.tf line 38, in resource "azurerm_key_vault_secret" "ewo11":
│ 38: value = [ for secret in data.azurerm_key_vault_secret.ewo1 : secret.name]
│ ├────────────────
│ │ data.azurerm_key_vault_secret.ewo1 is object with 4 attributes
│
│ Inappropriate value for attribute "value": string required.
╵
╷
│ Error: Incorrect attribute value type
│
│ on demo-keyvault-migration.tf line 38, in resource "azurerm_key_vault_secret" "ewo11":
│ 38: value = [ for secret in data.azurerm_key_vault_secret.ewo1 : secret.name]
│ ├────────────────
│ │ data.azurerm_key_vault_secret.ewo1 is object with 4 attributes
│
│ Inappropriate value for attribute "value": string required.
╵
╷
│ Error: Incorrect attribute value type
│
│ on demo-keyvault-migration.tf line 38, in resource "azurerm_key_vault_secret" "ewo11":
│ 38: value = [ for secret in data.azurerm_key_vault_secret.ewo1 : secret.name]
│ ├────────────────
│ │ data.azurerm_key_vault_secret.ewo1 is object with 4 attributes
│
│ Inappropriate value for attribute "value": string required.
╵
╷
│ Error: Incorrect attribute value type
│
│ on demo-keyvault-migration.tf line 38, in resource "azurerm_key_vault_secret" "ewo11":
│ 38: value = [ for secret in data.azurerm_key_vault_secret.ewo1 : secret.name]
│ ├────────────────
│ │ data.azurerm_key_vault_secret.ewo1 is object with 4 attributes
│
╵
There are a couple of issues here, but the primary one is related to the azurerm_key_vault_secret data source. You are querying the data source while using for_each. That means that the result will be an object with key value pairs. That is why you are getting this in the output:
data.azurerm_key_vault_secret.ewo1 is object with 4 attributes
as in for_each you will use four keys:
for_each = toset(["demo-eworx-terraform-automation-client-secret", "demo-eworx-terraform-automation-client-id", "demo-eworx-terraform-automation-tenant-id", "demo-eworx-terraform-automation-subscription-id"])
The easiest and probably the cleanest way to fix the error is as follows:
resource "azurerm_key_vault_secret" "ewo11" {
provider = azurerm.terraform-automation
for_each = data.azurerm_key_vault_secret.ewo1
name = each.key
value = each.value.value
key_vault_id = data.azurerm_key_vault.ewo11.id
}
Here it is a bit unfortunate that each.value.value [1] has to be used due to the attribute naming, but there is not another way. Also, please make sure you understand how the for_each meta-argument [2] works.
[1] https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs/data-sources/key_vault_secret#value
[2] https://developer.hashicorp.com/terraform/language/meta-arguments/for_each

Terraform keep saying variable not defined though its defined

I am trying to create a resource in azure via Terraform. Even though I have declared all required variables, its erroring out. I am using modules for the same...
Here is the below code that I tried:
My Module's Bastion/main.tf that used to create resource:
resource "azurerm_public_ip" "syn_pip" {
name = "pip-${var.prefix}-${var.postfix}"
location = var.location
resource_group_name = var.rg_name
allocation_method = var.bastion_allocation_method
sku = var.bastion_sku_type
}
My modules's Bastion/variables.tf :
variable "bastion_allocation_method" {
type = string
description = "allocation method for bastion"
}
variable "bastion_sku_type" {
type = string
description = "sku to be used for bastion"
}
My root module bastion.tf :
module "bastion" {
source = "../modules/bastion"
rg_name = module.resource_group.name
location = module.resource_group.location
allocation_method = var.bastion_allocation_method
sku = var.bastion_sku_type
prefix = var.prefix
postfix = random_string.postfix.result
subnet_id = azurerm_subnet.bastion_subnet.id
}
root module's variable.tf:
variable "bastion_allocation_method" {
type = string
}
variable "bastion_sku_type" {
type = string
}
my terraform.tfvars which passed to terraform plan :
"bastion_allocation_method": "Static",
"bastion_sku_type": "Standard"
Error that I get:
│ Error: Missing required argument
│
│ on bastion.tf line 1, in module "bastion":
│ 1: module "bastion" {
│
│ The argument "bastion_allocation_method" is required, but no definition was found.
╵
╷
│ Error: Missing required argument
│
│ on bastion.tf line 1, in module "bastion":
│ 1: module "bastion" {
│
│ The argument "bastion_sku_type" is required, but no definition was found.
╵
╷
│ Error: Unsupported argument
│
│ on bastion.tf line 7, in module "bastion":
│ 7: allocation_method = var.bastion_allocation_method
│
│ An argument named "allocation_method" is not expected here.
╵
╷
│ Error: Unsupported argument
│
│ on bastion.tf line 8, in module "bastion":
│ 8: sku = var.bastion_sku_type
│
│ An argument named "sku" is not expected here.
╵
Exited with code exit status 1
Can someone suggest, what is the mistake I am doing ?
In your Bastion module in the variables.tf you specify the arguments
variable "bastion_allocation_method" {
...
}
variable "bastion_sku_type" {
...
}
In your case your module expects the vars bastion_allocation_method and bastion_sku_type
But when you call the module, you dont pass the variable names properly
module "bastion" {
source = "../modules/bastion"
...
allocation_method = var.bastion_allocation_method
sku = var.bastion_sku_type
...
}
Use instead
module "bastion" {
source = "../modules/bastion"
...
bastion_allocation_method = var.bastion_allocation_method
bastion_sku_type = var.bastion_sku_type
...
}
The variable assignments that you pass in module clause should match the definitions in the module's variables.tf

Error Reference to undeclared resource - Trying to get terraform modules to work

New terraform learner here, trying to learn by example and by coding. I was looking for terraform modules that would allow one to create a VM, found the example here https://faun.pub/creating-a-windows-vm-in-azure-using-terraform-which-way-is-best-13aff3ed9b74 which I thought would take my very basic terraform knowledge up a notch, since creating a VM is a process I am familiar with and can build upon. The experience has been nothing but frustrating. This is my 3rd week on it, and it still feels like miles away.
I read up the first solution and my files are structured in exactly thesame way as described.Mind, I have tried the first method, and it was riddled with errors, so I decided to try the second method which was supposed to be simpler.
I created a folder called simple_vm and inside it, another folder called vm.
Here are the contents.
vm_module_local_example.tf
# Create an Azure VM cluster with Terraform calling a Module. Creates 1 for Windows 10 desktop and 1 for Windows 2019 Server.
module windows_desktop_vm_using_local_module {
source = "./vm"
resource_group_name = azurerm_resource_group.rg.name
location = "uksouth"
sloc = "uks"
vm_subnet_id = module.network.vnet_subnets[0]
vm_name = "tfdtlocmod"
vm_size = var.desktop_vm_size
publisher = var.desktop_vm_image_publisher
offer = var.desktop_vm_image_offer
sku = var.desktop_vm_image_sku
static_ip_address = "10.0.1.15"
activity_tag = "Windows Desktop"
admin_password = module.vmpassword.secretvalue
}
module windows_server_vm_using_local_module {
source = "./vm"
resource_group_name = azurerm_resource_group.rg.name
location = "uksouth"
sloc = "uks"
vm_subnet_id = module.network.vnet_subnets[1]
vm_name = "tfsvlocmod"
vm_size = var.server_vm_size
publisher = var.server_vm_image_publisher
offer = var.server_vm_image_offer
sku = var.server_vm_image_sku
static_ip_address = "10.0.2.15"
activity_tag = "Windows Server"
admin_password = module.vmpassword.secretvalue
}
Within the folder called VM. I have the following files.
main.tf
resource "random_string" "nic_prefix" {
length = 4
special = false
}
resource "azurerm_network_interface" "vm_nic" {
name = "${var.vm_name}-nic1"
location = var.location
resource_group_name = var.resource_group_name
ip_configuration {
name = "${var.vm_name}_nic_${random_string.nic_prefix.result}"
subnet_id = var.vm_subnet_id
private_ip_address_allocation = "Static"
private_ip_address = var.static_ip_address
}
tags = var.tags
}
resource "azurerm_network_interface_security_group_association" "vm_nic_sg" {
network_interface_id = azurerm_network_interface.vm_nic.id
network_security_group_id = var.network_security_group_id
count = var.network_security_group_id == "" ? 0 : 1
}
resource "azurerm_virtual_machine" "windows_vm" {
name = var.vm_name
vm_size = var.vm_size
location = var.location
resource_group_name = var.resource_group_name
tags = merge(var.tags, { activityName = "${var.activity_tag} " })
network_interface_ids = [
"${azurerm_network_interface.vm_nic.id}",
]
storage_image_reference {
publisher = var.publisher
offer = var.offer
sku = var.sku
version = "latest"
}
identity {
type = "SystemAssigned"
}
storage_os_disk {
name = "${var.vm_name}-os-disk"
caching = "ReadWrite"
create_option = "FromImage"
managed_disk_type = "Standard_LRS"
}
os_profile {
admin_password = module.vmpassword.secretvalue
admin_username = "azureuser"
computer_name = var.vm_name
}
os_profile_windows_config {
provision_vm_agent = true
}
delete_os_disk_on_termination = var.vm_os_disk_delete_flag
delete_data_disks_on_termination = var.vm_data_disk_delete_flag
}
outputs.tf
output "vm_id" {
value = "${azurerm_virtual_machine.windows_vm.id}"
}
output "vm_name" {
value = "${azurerm_virtual_machine.windows_vm.name}"
}
output "vm_location" {
value = "${azurerm_virtual_machine.windows_vm.location}"
}
output "vm_resource_group_name" {
value = "${azurerm_virtual_machine.windows_vm.resource_group_name}"
}
variables.tf
variable "resource_group_name" {
}
variable "location" {
}
variable "sloc" {
}
variable "vm_size" {
default = "Standard_B1s"
}
variable "vm_subnet_id" {
}
variable "vm_name" {
}
variable "vm_os_disk_delete_flag" {
default = true
}
variable "vm_data_disk_delete_flag" {
default = true
}
variable "network_security_group_id" {
default = ""
}
variable "static_ip_address" {
}
variable "publisher" {
}
variable "offer" {
}
variable "sku" {
}
variable "tags" {
type = map
description = "All mandatory tags to use on all assets"
default = {
activityName = "AzureVMWindowsDemo"
automation = "Terraform"
costCenter1 = "A00000"
dataClassification = "Demo"
managedBy = "example#test.com"
solutionOwner = "example#test.com"
}
}
variable "activity_tag" {
}
variable "admin_password" {
}
Taking what is on the website, I get the following errors.
Error: Reference to undeclared resource
│
│ on vm_module_local_example.tf line 4, in module "windows_desktop_vm_using_local_module":
│ 4: resource_group_name = azurerm_resource_group.rg.name
│
│ A managed resource "azurerm_resource_group" "rg" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│ on vm_module_local_example.tf line 7, in module "windows_desktop_vm_using_local_module":
│ 7: vm_subnet_id = module.network.vnet_subnets[0]
│
│ No module call named "network" is declared in the root module.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on vm_module_local_example.tf line 9, in module "windows_desktop_vm_using_local_module":
│ 9: vm_size = var.desktop_vm_size
│
│ An input variable with the name "desktop_vm_size" has not been declared. This variable can be declared with a variable "desktop_vm_size" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on vm_module_local_example.tf line 10, in module "windows_desktop_vm_using_local_module":
│ 10: publisher = var.desktop_vm_image_publisher
│
│ An input variable with the name "desktop_vm_image_publisher" has not been declared. This variable can be declared with a variable "desktop_vm_image_publisher" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on vm_module_local_example.tf line 11, in module "windows_desktop_vm_using_local_module":
│ 11: offer = var.desktop_vm_image_offer
│
│ An input variable with the name "desktop_vm_image_offer" has not been declared. This variable can be declared with a variable "desktop_vm_image_offer" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on vm_module_local_example.tf line 12, in module "windows_desktop_vm_using_local_module":
│ 12: sku = var.desktop_vm_image_sku
│
│ An input variable with the name "desktop_vm_image_sku" has not been declared. This variable can be declared with a variable "desktop_vm_image_sku" {} block.
╵
╷
│ Error: Reference to undeclared module
│
│ on vm_module_local_example.tf line 15, in module "windows_desktop_vm_using_local_module":
│ 15: admin_password = module.vmpassword.secretvalue
│
│ No module call named "vmpassword" is declared in the root module.
╵
╷
│ Error: Reference to undeclared resource
│
│ on vm_module_local_example.tf line 19, in module "windows_server_vm_using_local_module":
│ 19: resource_group_name = azurerm_resource_group.rg.name
│
│ A managed resource "azurerm_resource_group" "rg" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│ on vm_module_local_example.tf line 22, in module "windows_server_vm_using_local_module":
│ 22: vm_subnet_id = module.network.vnet_subnets[1]
│
│ No module call named "network" is declared in the root module.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on vm_module_local_example.tf line 24, in module "windows_server_vm_using_local_module":
│ 24: vm_size = var.server_vm_size
│
│ An input variable with the name "server_vm_size" has not been declared. This variable can be declared with a variable "server_vm_size" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on vm_module_local_example.tf line 25, in module "windows_server_vm_using_local_module":
│ 25: publisher = var.server_vm_image_publisher
│
│ An input variable with the name "server_vm_image_publisher" has not been declared. This variable can be declared with a variable "server_vm_image_publisher" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on vm_module_local_example.tf line 26, in module "windows_server_vm_using_local_module":
│ 26: offer = var.server_vm_image_offer
│
│ An input variable with the name "server_vm_image_offer" has not been declared. This variable can be declared with a variable "server_vm_image_offer" {} block.
╵
╷
│ Error: Reference to undeclared input variable
│
│ on vm_module_local_example.tf line 27, in module "windows_server_vm_using_local_module":
│ 27: sku = var.server_vm_image_sku
│
│ An input variable with the name "server_vm_image_sku" has not been declared. This variable can be declared with a variable "server_vm_image_sku" {} block.
╵
╷
│ Error: Reference to undeclared module
│
│ on vm_module_local_example.tf line 30, in module "windows_server_vm_using_local_module":
│ 30: admin_password = module.vmpassword.secretvalue
│
│ No module call named "vmpassword" is declared in the root module.
I can see that a lot of the errors relate to undeclared variables. I then create a variable file as follows (not within the vm folder).
variable "subscription_id" {
}
variable "client_id" {
}
variable "client_secret" {
}
variable "tenant_id" {
}
variable "global_settings" {
}
variable "desktop_vm_image_publisher" {
}
variable "desktop_vm_image_offer" {
}
variable "desktop_vm_image_sku" {
}
variable "desktop_vm_image_version" {
}
variable "desktop_vm_size" {
}
variable "server_vm_image_publisher" {
}
variable "server_vm_image_offer" {
}
variable "server_vm_image_sku" {
}
variable "server_vm_image_version" {
}
variable "server_vm_size" {
}
created a file called terraform.auto.tfvars.
# This file should not be checked into source control (add to .gitignore)
subscription_id = "xxxxxxxxxxxxxx"
client_id = "xxxxxxxxxxxxxx"
client_secret = "xxxxxxxxxxxxxx"
tenant_id = "xxxxxxxxxxxxxx"
## globalsettings
global_settings = {
#Set of tags
tags = {
applicationName = "Windows VM Demo"
businessUnit = "Technical Solutions"
costCenter = "MPN Sponsorship"
DR = "NON-DR-ENABLED"
deploymentType = "Terraform"
environment = "Dev"
owner = "Jack Roper"
version = "0.1"
}
}
# Desktop VM variables
desktop_vm_image_publisher = "MicrosoftWindowsDesktop"
desktop_vm_image_offer = "Windows-10"
desktop_vm_image_sku = "20h1-pro"
desktop_vm_image_version = "latest"
desktop_vm_size = "Standard_B1s"
# Server VM Variables
server_vm_image_publisher = "MicrosoftWindowsServer"
server_vm_image_offer = "WindowsServer"
server_vm_image_sku = "2019-Datacenter"
server_vm_image_version = "latest"
server_vm_size = "Standard_B1s"
Running terraform plan this time, I get the following errors.
Error: Reference to undeclared resource
│
│ on vm_module_local_example.tf line 4, in module "windows_desktop_vm_using_local_module":
│ 4: resource_group_name = azurerm_resource_group.rg.name
│
│ A managed resource "azurerm_resource_group" "rg" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│ on vm_module_local_example.tf line 7, in module "windows_desktop_vm_using_local_module":
│ 7: vm_subnet_id = module.network.vnet_subnets[0]
│
│ No module call named "network" is declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│ on vm_module_local_example.tf line 15, in module "windows_desktop_vm_using_local_module":
│ 15: admin_password = module.vmpassword.secretvalue
│
│ No module call named "vmpassword" is declared in the root module.
╵
╷
│ Error: Reference to undeclared resource
│
│ on vm_module_local_example.tf line 19, in module "windows_server_vm_using_local_module":
│ 19: resource_group_name = azurerm_resource_group.rg.name
│
│ A managed resource "azurerm_resource_group" "rg" has not been declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│ on vm_module_local_example.tf line 22, in module "windows_server_vm_using_local_module":
│ 22: vm_subnet_id = module.network.vnet_subnets[1]
│
│ No module call named "network" is declared in the root module.
╵
╷
│ Error: Reference to undeclared module
│
│ on vm_module_local_example.tf line 30, in module "windows_server_vm_using_local_module":
│ 30: admin_password = module.vmpassword.secretvalue
│
│ No module call named "vmpassword" is declared in the root module.
╵
Now, It appears I have been extremely unlucky with examples online, in the above there are a lot of missing variables, the author doesnt specify if certain files should be used from a different example (there is a lot of code on the link apparently for different techniques for achieving the same goal). At the same time, I am new to this and wasn't sure if I can truly say there are missing files/details in the solution posted. I have had no luck with contacting the author, no github repo to check that I have the right setup, hence why I have turned here for help. All I want to do is improve my knowledge on terraform modules, I prefer to have explanations and examples so that I can also practice. I could look at git repos, but that would only be giving me solutions without explanation or learning opportunities.
As people in the comments have mentioned it looks like you are following an incomplete or out of date tutorial. I thought it might help you if I explain how to understand the errors produced so you can tackle this hurdle in future.
Error: Reference to undeclared resource
│
│ on vm_module_local_example.tf line 4, in module "windows_desktop_vm_using_local_module":
│ 4: resource_group_name = azurerm_resource_group.rg.name
│
│ A managed resource "azurerm_resource_group" "rg" has not been declared in the root module.
╵
In the above message Terraform is saying "On line 4 in the file vm_module_local_example.tf, you've set the resource_group_name to be the value of azurerm_resource_group.rg.name but when I tried to find a resource "azure_resource_group" with the name "rg" I couldn't find anything. To fix it you'd need to code up a resource like:
resource "azurerm_resource_group" "rg" {
name = "example"
location = "West Europe"
}
Once that's done your line 4 will set resource_group_name to be "example".
╷
│ Error: Reference to undeclared module
│
│ on vm_module_local_example.tf line 7, in module "windows_desktop_vm_using_local_module":
│ 7: vm_subnet_id = module.network.vnet_subnets[0]
│
│ No module call named "network" is declared in the root module.
This is a good one, on line 7 in the file named vm_module_local_example.tf there is a call to module.network.vnet_subnets[0]. So Terraform will look for a module called network before proceeding further. You'd need code like this:
module "network" {
# pass in whatever the module needs...
}
Based on the error it looks like the module is expected to contain a resource called vnet_subnets generated with count as it's relying on a zero based index (that's the [0] part).
Anyways, hope this helps!

Computed attributes cannot be set : org_id

I am learning how to use Terraform. My aim is to deploy an architecture on GCP so here's my main.tf so far :
terraform {
required_providers {
google = {
source = "hashicorp/google"
version = "3.85.0"
}
}
}
provider "google" {
credentials = file(var.credentials_file)
region = var.region
zone = var.zone
}
data "google_organization" "org" {
domain = var.organization.display_name
org_id = var.organization.id
directory_customer_id = var.organization.directory_customer_id
}
resource "google_folder" "shared" {
display_name = "Shared"
parent = google_organization.org_id
}
resource "google_folder" "ddm" {
display_name = "Data and Digital Marketing"
parent = google_folder.shared.name
}
resource "google_folder" "dtl" {
display_name = "DTL"
parent = google_folder.ddm.name
}
According to the documentation, org_id is within the Attributes Reference
But I get the following errors:
╷
│ Error: Computed attributes cannot be set
│
│ with data.google_organization.org,
│ on main.tf line 17, in data "google_organization" "org":
│ 17: org_id = var.organization.id
│
│ Computed attributes cannot be set, but a value was set for "org_id".
╵
╷
│ Error: Computed attributes cannot be set
│
│ with data.google_organization.org,
│ on main.tf line 18, in data "google_organization" "org":
│ 18: directory_customer_id = var.organization.directory_customer_id
│
│ Computed attributes cannot be set, but a value was set for "directory_customer_id".
╵
╷
│ Error: Reference to undeclared resource
│
│ on main.tf line 22, in resource "google_folder" "shared":
│ 22: parent = google_organization.org_id
│
│ A managed resource "google_organization" "org_id" has not been declared in the root module.
What am I doing wrong?
The organization is set as a data source, but in the previous code, it is used like a resource block.
What needs to be done to reference the organization is this :
data "google_organization" "org" {
organization = var.organization.id
}
org_id is an output, not an input. The only acceptable inputs are organization ordomain; they are mutually exclusive.
And use its outputs like this :
resource "google_folder" "shared" {
display_name = "Shared"
parent = data.google_organization.org.org_id
}
EDIT : This, although syntactically correct, it might not work because the account used must be organization administrator on the organization level. I do not recomment using the google_organization data sourcejust to fetch the ID and other info, I ended up writing those in a variable and just calling it this way :
resource "google_folder" "shared" {
display_name = "Shared"
parent = "organizations/${var.organization.id}"
}

Terraform aks module - get cluster name and resource group name via remote state

Hi I am trying to follow this offical guide to manage aks resources. There terraform_remote_state is used to get the resource_group_name and kubernetes_cluster_name.
data "terraform_remote_state" "aks" {
backend = "local"
config = {
path = "/path/to/base/project/terraform.tfstate"
}
}
# Retrieve AKS cluster information
provider "azurerm" {
features {}
}
data "azurerm_kubernetes_cluster" "cluster" {
name = data.terraform_remote_state.aks.outputs.kubernetes_cluster_name
resource_group_name = data.terraform_remote_state.aks.outputs.resource_group_name
}
I have created the inital aks cluster with the aks module. Looking at its output in the documentation, it doesnt export the resource group name or cluster name.
Now I wonder how I can get the information. I have tried the below in the base project.
module "aks" {
...
}
output "resource_group_name" {
value = module.aks.resource_group_name
}
output "kubernetes_cluster_name" {
value = module.aks.cluster_name
}
But I get erros when trying terraform plan
Error: Unsupported attribute
│
│ on main.tf line 59, in output "resource_group_name":
│ 59: value = module.aks.resource_group_name
│ ├────────────────
│ │ module.aks is a object, known only after apply
│
│ This object does not have an attribute named "resource_group_name".
╵
╷
│ Error: Unsupported attribute
│
│ on main.tf line 63, in output "kubernetes_cluster_name":
│ 63: value = module.aks.cluster_name
│ ├────────────────
│ │ module.aks is a object, known only after apply
│
│ This object does not have an attribute named "cluster_name".
Those are listed under inputs for that module though. Now I dont have an idea know how to get those values from the terraform_remote_state.
As the module itself doesn’t have name and resource group as output , we have to declare outputs there first and then call it while deploying or in remote state as well.
So we have to add 2 outputs in output.tf for aks module after doing terraform init.
output "kubernetes_cluster_name" {
value = azurerm_kubernetes_cluster.main.name
}
output "resource_group_name" {
value = azurerm_kubernetes_cluster.main.resource_group_name
}
Then call outputs in main.tf after defining the modules i.e. network and aks , you can see your Kubernetes cluster name in plan as well and after applying it.
output "kuberneteclustername" {
value = module.aks.kubernetes_cluster_name
}
output "resourcegroupname" {
value = module.aks.resource_group_name
}
Now lets test it from the remote state :
data "terraform_remote_state" "aks" {
backend = "local"
config = {
path = "path/to/terraform/aksmodule/terraform.tfstate"
}
}
# Retrieve AKS cluster information
provider "azurerm" {
features {}
}
data "azurerm_kubernetes_cluster" "cluster" {
name = data.terraform_remote_state.aks.outputs.kuberneteclustername
resource_group_name = data.terraform_remote_state.aks.outputs.resourcegroupname
}
output "aks" {
value = data.azurerm_kubernetes_cluster.cluster.name
}
output "rg" {
value = data.azurerm_kubernetes_cluster.cluster.resource_group_name
}

Resources