Skip to content
Novus Examples
tf1.1 KB

Terraform Resources with count and for_each

The resource graph: a network, a for_each subnet set, a counted instance resource, a data source, and a bucket on an aliased provider. Both repetition meta-arguments appear, plus lifecycle and an explicit depends_on edge.

Preview — first 50 linestf
resource "examplecloud_network" "main" {
  name       = "novus-${var.environment}-net"
  cidr_block = "10.20.0.0/16"

  tags = merge(var.tags, {
    environment = var.environment
  })
}

resource "examplecloud_subnet" "app" {
  for_each = var.subnets

  network_id = examplecloud_network.main.id
  name       = each.key
  cidr_block = each.value.cidr
  zone       = each.value.zone
  public     = each.value.public
}

data "examplecloud_image" "base" {
  family      = "example-linux-1"
  most_recent = true
}

resource "examplecloud_instance" "app" {
  count = var.instance_count

  name      = format("app-%02d", count.index + 1)
  size      = var.instance_size
  image_id  = data.examplecloud_image.base.id
  subnet_id = examplecloud_subnet.app["app-a"].id

  metadata = {
    role  = "app"
    index = count.index
  }

  lifecycle {
    create_before_destroy = true
    ignore_changes        = [metadata["index"]]
  }

  depends_on = [examplecloud_network.main]
}

resource "examplecloud_bucket" "artifacts" {
  provider = examplecloud.secondary

  name          = "novus-${var.environment}-artifacts"
  versioning    = true
53 lines total — download for the full file.

Specifications

Resources
4
Data Sources
1
Uses For Each
true
Uses Count
true
Lifecycle Blocks
1
Explicit Depends On
1

Testing contract

Expected to pass
Scenario
Build a Terraform resource dependency graph across count, for_each, and an explicit depends_on
Expected result
Five graph nodes resolve, the subnet resource expands per for_each key, and the instance resource carries both an implicit and an explicit dependency on the network

What is a .tf file?

A .tf file is a Terraform configuration written in HCL. It declares infrastructure as blocks — terraform, provider, resource, data, variable, output, module, locals — whose bodies hold arguments and nested blocks, with values drawn from expressions, interpolation, and functions. Terraform reads every .tf file in a directory as one merged configuration, so ordering across files carries no meaning.

How to use this file

Use an example .tf file to test HCL parsers, infrastructure linters and policy engines, and configuration migrators, checking block-and-label parsing, heredoc strings, expression references between resources, and that a deprecated-syntax file is flagged rather than silently accepted.

How to use this file for testing

“Terraform Resources with count and for_each” is a deterministic Novus Examples fixture for Config testing, Config parsing, Syntax highlighting. TOML, INI, YAML, .env, and dotfile configuration samples with nested sections and typed values — for testing config parsers, loaders, and environment tooling.

Documented properties for this file: TF · 1,155 bytes. Compare results against paired or grouped companions on this page when present (clean↔damaged, searchable↔scanned, or format twins) so scores stay reproducible across runs.

Download the file once, keep the path stable in CI or local scripts, and treat the spec table as the contract: dimensions, seeds, field lists, and roles are intentional. Corrupt or invalid samples are labelled as such — expect parsers to fail loudly rather than silently accept them.

Pipeline and infrastructure fixtures are inert configuration: steps reference fictional images and scripts, and nothing here executes. Run your linter, schema validator, migrator, or policy engine against them, and expect the deprecated-syntax and intentionally invalid variants to be rejected.

Point your config loader at the file and assert it reads the documented sections and typed values, including any deliberately-tricky nesting or comments.

Generated by generation/pipelines.py. Free for any use, no attribution required — license.