vendredi 5 février 2021

How can I interpolate a random octet into a CIDR block in Terraform?

I have the following random_test.tf Terraform file, which I've successfully initialized:

resource "random_integer" "octet" {
  min     = 0
  max     = 255
}

variable "base_cidr_block" {
  description = "Class A CIDR block in RFC 1918 range"
  default = "10.0.0.0/8"
}

provider "null" {
  base_cidr_block = "10.${random_integer.octet.result}.0.0/16"
}

output "ip_block" {
  value = var.base_cidr_block
}

I'm using the null provider as a placeholder to test defining a 10.0.0.0/16 CIDR block with a random second octet. However, base_cidr_block is always 10.0.0.0/8 even though I'm expecting it to be assigned something like 10.100.0.0/16, which would then be shown on standard output as ip_block. Instead, I always get the default:

$ terraform plan

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # random_integer.octet will be created
  + resource "random_integer" "octet" {
      + id     = (known after apply)
      + max    = 255
      + min    = 0
      + result = (known after apply)
    }

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

Changes to Outputs:
  + ip_block = "10.0.0.0/8"

Running terraform apply then always sends ip_block = "10.0.0.0/8" to the console. What am I doing wrong?




Aucun commentaire:

Enregistrer un commentaire