Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create random resource name in Terraform

I'm using Terraform to create stuff in Azure,

In ARM I used to use uniqueString() to generate storage account names,

So is it possible to generate random name for storage account using Terraform?

like image 403
kagarlickij Avatar asked Sep 24 '17 15:09

kagarlickij


1 Answers

There are several random resources you can use in Terraform

https://www.terraform.io/docs/providers/random/index.html

Resources

random_id
random_pet
random_shuffle
random_string

Use random_id as a sample, and the official codes in resource azurerm_storage_account

You can define the resource azurerm_storage_account name easily.

resource "random_id" "storage_account" {
  byte_length = 8
}

resource "azurerm_storage_account" "testsa" {
  name                = "tfsta${lower(random_id.storage_account.hex)}"
  resource_group_name = "${azurerm_resource_group.testrg.name}"

  location     = "westus"
  account_type = "Standard_GRS"

  tags {
    environment = "staging"
  }
}
like image 104
BMW Avatar answered Sep 22 '22 03:09

BMW