Menu

Virtual Geek

Tales from real IT system administrators world and non-production environment

Hashicorp Terraform map and object inside module and variable example

This is example of map and object functions used in variable and Terraform module. Here is my tree view of module folders and tf files.

terraform hashicorp hcl tree module map and object example microsoft azure cloud resource group storage account.png

In the module variable I am using two terraform functions object and map to define data. To use object, provide information in key value pair and dictionary (hashtable) data in map format. To utilize map in resource I am using for loop around it. Same thing I am doing to output the values.

Reference:  https://developer.hashicorp.com/terraform/language/expressions/type-constraints

#Location:  .\Modules\Resource_Group
#Resource group module variable.tf file
variable "location_info" {
  description = "The name of the object location in which resource group will be created"
  type = object({
    location = string
    #otheroption = string
  })
}

variable "rg_info" {
  type = map(object({
    name = string #"East Us"
    tags = object({
      environment = string
      Owner       = string
    })
  }))
}

#Resource group module main.tf file
resource "azurerm_resource_group" "example_rg" {
  for_each = {
    for key, sa in var.rg_info : sa.name => sa
  }
  name     = each.value.name
  tags     = each.value.tags
  location = var.location_info.location
}

#Resource group module output.tf file
output "rg_name" {
  value = [for s in var.rg_info : s.name]
}

output "location_name" {
  value = var.location_info.location
}

In below module in the root folder I have given examples of object and map, creating multiple resource groups in Azure Cloud.

Download this terraform script here or it is also available on github.com.

#Location: .\
#Root rg.tf file
# Configure the Microsoft Azure Provider
provider "azurerm" {
  features {}
}

module "Demo_Azure_Module_RG" {
  source = "./Modules/Resource_Group"
  location_info = {
    location = "West US"
  }

  rg_info = {
    rg1 = {
      name = "demo_RG1"
      tags = {
        environment = "DemoRG1"
        Owner       = "http://vcloud-lab.com"
      } #tags
    } #rg1
    rg2 = {
      name = "demo_RG2"
      tags = {
        environment = "DemoRG2"
        Owner       = "http://vcloud-lab.com"
      } #tags
    } #rg2
  } #rg_info
} #module

Below are the commands I used to run terraform project and deploy RGs on Azure.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
PS D:\Projects\Terraform\Module_map_and_object_example>
PS D:\Projects\Terraform\Module_map_and_object_example> terraform init

Initializing the backend...
Initializing modules...
- Demo_Azure_Module_RG in Modules\Resource_Group

Initializing provider plugins...
- Finding latest version of hashicorp/azurerm...
- Installing hashicorp/azurerm v3.66.0...
- Installed hashicorp/azurerm v3.66.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
PS D:\Projects\Terraform\Module_map_and_object_example> terraform plan
module.Demo_Azure_Module_RG.azurerm_resource_group.example_rg["demo_RG2"]: Refreshing state... [id=/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/demo_RG2]
module.Demo_Azure_Module_RG.azurerm_resource_group.example_rg["demo_RG1"]: Refreshing state... [id=/subscriptions/9e22xxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx/resourceGroups/demo_RG1]  

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # module.Demo_Azure_Module_RG.azurerm_resource_group.example_rg["demo_RG1"] will be created
  + resource "azurerm_resource_group" "example_rg" {
      + id       = (known after apply)
      + location = "westus"
      + name     = "demo_RG1"
      + tags     = {
          + "Owner"       = "http://vcloud-lab.com"
          + "environment" = "DemoRG1"
        }
    }

  # module.Demo_Azure_Module_RG.azurerm_resource_group.example_rg["demo_RG2"] will be created
  + resource "azurerm_resource_group" "example_rg" {
      + id       = (known after apply)
      + location = "westus"
      + name     = "demo_RG2"
      + tags     = {
          + "Owner"       = "http://vcloud-lab.com"
          + "environment" = "DemoRG2"
        }
    }

Plan: 2 to add, 0 to change, 0 to destroy.

──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────── 

Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform apply" now.
PS D:\Projects\Terraform\Module_map_and_object_example>

Hashicorp hcl terraform vmware vsphere vsphere azure cloud provider object map deployemnt apply plan destroy --auto-approve.png

Useful Articles
Create an Azure virtual machine scale set and load balancer using Terraform
Azure Terraform fixed Availibility Zones on Virtual Machine Scale Set
Writing and Using Terraform modules
Terraform Using one module variable in another module
Hashicorp Terraform dynamic block with example
Terraform for_each loop on map example
Terraform for_each loop on resource example
Terraform manage similar resources with for_each loop inside modules
Importing existing resources into Terraform - Step by Step
Importing already created module Infrastructure into Terraform and update state file
Conditionally create resources in Terraform

Go Back

Comment

Blog Search

Page Views

11391918

Follow me on Blogarama