Terraform: Split To Get Values

2 minute read

Description:

As with many languages, you can perform a split function on a string in Terraform. Here is an example of how I can target to get specific values.

To Resolve:

  1. For example, let’s say I want to get some value from a Storage Account ID, I could do something like the following:
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
64
65
66
67
terraform {
  required_providers {

    azurerm = {
      source  = "hashicorp/azurerm"
      version = "~>3.10.0"
    }

  }
  required_version = "~>1.1.0"
}

provider "azurerm" {
  client_id                  = var.client_id
  client_secret              = var.client_secret
  subscription_id            = var.subscription_id
  tenant_id                  = var.tenant_id
  skip_provider_registration = true
  features {}
}

variable "tenant_id" {
  description = "(Required) Service Principal AD Tenant ID - Azure AD for terraform authentication."
  type        = string
}

variable "subscription_id" {
  description = "(Required) Azure Subscription Id used to connect to AzureRM provider."
  type        = string
}

variable "client_id" {
  description = "(Required) Service Principal App ID - Azure AD for terraform authentication."
  type        = string
}

variable "client_secret" {
  description = "(Required) Service Principal Client Secret - Azure AD for terraform authentication."
  type        = string
}

resource "azurerm_resource_group" "rg" {
  name     = "tx-storage-rg"
  location = "southcentralus"
}

resource "azurerm_storage_account" "example" {
  name                = "aatxstorage145"
  resource_group_name = azurerm_resource_group.rg.name

  location                 = azurerm_resource_group.rg.location
  account_tier             = "Standard"
  account_replication_type = "LRS"

}

output "data1" {
   value = azurerm_storage_account.example.id
}

output "data2" {
   value = split("/", azurerm_storage_account.example.id)[2]
}

output "data3" {
   value = split("/", azurerm_storage_account.example.id)
}
  1. When I run an apply, I get the following:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
> terraform apply -auto-approve -input=false ./tf.plan
Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
Outputs:
data1 = "/subscriptions/xxx-xxxx-xxx/resourceGroups/tx-storage-rg/providers/Microsoft.Storage/storageAccounts/aatxstorage145"
data2 = "xxx-xxxx-xxx"
Changes to Outputs:
+ data3 = [
      + "",
      + "subscriptions",
      + "xxx-xxxx-xxx",
      + "resourceGroups",
      + "tx-storage-rg",
      + "providers",
      + "Microsoft.Storage",
      + "storageAccounts",
      + "aatxstorage145",
    ]
  1. Now that we know the values for each index, we can simply pass them around as needed:

    • Using split("/", azurerm_storage_account.example.id)[2] will get us the subscription ID the Storage Account is in.
    • Using split("/", azurerm_storage_account.example.id)[4] will get us the Resource Group the Storage Account is in.
    • Using split("/", azurerm_storage_account.example.id)[8] will get us the Storage Account Name.
  2. Commonly, you will use these in locals for a simple value that can be passed to other places as needed:

1
2
3
4
5
locals {
   sa_sub_id = split("/", azurerm_storage_account.example.id)[2]
   sa_rg = split("/", azurerm_storage_account.example.id)[4]
   sa_name = split("/", azurerm_storage_account.example.id)[8]
}
  1. Note that it is better and preferred to get the output of a resource instead of relying on parsing the id. This was just an example of how you can take a string, split it, and store its values in other variables to be referenced in other places.

Comments