Blocks that depend on other blocks

A block field lets one block use another block’s output. You select a block in the same scope. InfraBuffet passes the selected module’s output as an input, and Terraform orders the work. You do not have to apply blocks in a particular sequence.

Example modules

These examples use terraform_data, which is built into Terraform 1.4 and later. They store values in state and create no cloud resources, so you can try them safely.

Store modules/network/main.tf:

variable "cidr" {
  type = string
}

resource "terraform_data" "network" {
  input = var.cidr
}

output "network" {
  value = {
    id   = terraform_data.network.id
    cidr = terraform_data.network.output
  }
}

Store modules/server/main.tf:

variable "hostname" {
  type = string
}

variable "network_id" {
  type = string
}

resource "terraform_data" "server" {
  input = {
    hostname   = var.hostname
    network_id = var.network_id
  }
}

output "id" {
  value = terraform_data.server.id
}
  1. Create template network for modules/network, with a required string field cidr. Assume its ID is 20.
- name: cidr
  type: string
  mode: required
  1. Create template server for modules/server:
- name: hostname
  type: string
  mode: required

- name: network_id
  type: block
  mode: required
  block_template: 20  # id of the "network" block template
  block_accessor: network.id

Create dependent blocks

In a scope, create a network block with CIDR 10.0.0.0/24. Create a server block named app01 and select that network block. InfraBuffet uses each block’s name as its Terraform module name. If the network block is named networkdev, the server module internally receives:

network_id = module.networkdev.network.id

Calculate a diff for the whole scope. Terraform creates the network before the server.

Accessors

  • For Block accessor, enter the output name directly. Use id for an output named id, or network.id for the id attribute of output network.
  • Each dot-separated part must be a plain name: letters, digits, and underscores, not starting with a digit. Indexes and brackets are not supported.
  • The output must exist in the module, and its type must match the receiving variable.

What InfraBuffet checks

  • The selected block must exist in the same scope.
  • A block cannot depend on itself, directly or through a chain of blocks. Circular dependencies are rejected, and so are circular template dependencies.
  • An owner-managed block can depend only on other owner-managed blocks in the same scope.

Changes and deletion

  • If a referenced output changes, dependent blocks show changes in the next plan. Keep outputs stable, or update the dependent blocks together with the output.
  • A targeted plan for a block includes the blocks it depends on. A targeted plan for the network does not include the server. After targeted work, review a whole-scope diff.
  • Deleting a block also deletes every block that depends on it, directly or through other blocks. The deletion preview lists them. Nothing changes in Terraform until you calculate and apply a diff. If you want to keep the dependent blocks, change their fields to point at another block before you delete.