Skip to content
Novus Examples
tf1.1 KB

Terraform Locals, for Expressions, and dynamic Blocks

The expression-heavy half of the stack: for expressions producing both a map and a filtered list, conditional expressions selecting whole objects, and a dynamic block that generates two firewall rules from a local list.

Preview — first 48 linestf
locals {
  name_prefix = "novus-${var.environment}"

  is_production = var.environment == "prod"

  # for expression over a map, producing a map
  subnet_cidrs = { for key, value in var.subnets : key => value.cidr }

  # for expression with a filter, producing a list
  public_subnets = [for key, value in var.subnets : key if value.public]

  base_tags = {
    managed_by  = "terraform"
    environment = var.environment
  }

  sizing = local.is_production ? {
    instance_size = "large"
    replicas      = 6
  } : {
    instance_size = "small"
    replicas      = 2
  }

  ingress_rules = [
    { port = 80, protocol = "tcp", description = "http" },
    { port = 443, protocol = "tcp", description = "https" },
  ]
}

resource "examplecloud_firewall" "app" {
  name       = "${local.name_prefix}-fw"
  network_id = examplecloud_network.main.id

  dynamic "rule" {
    for_each = local.ingress_rules

    content {
      port        = rule.value.port
      protocol    = rule.value.protocol
      description = rule.value.description
      cidr_blocks = local.is_production ? ["10.20.0.0/16"] : ["0.0.0.0/0"]
    }
  }

  tags = merge(local.base_tags, var.tags)
}

Specifications

Locals
7
For Expressions
2
Conditional Expressions
2
Dynamic Blocks
1
Dynamic Iterations
2

Testing contract

Expected to pass
Scenario
Evaluate Terraform for expressions and expand a dynamic block into concrete nested blocks
Expected result
The dynamic rule block expands to exactly 2 rule blocks, and subnet_cidrs evaluates to a map keyed by subnet name

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 Locals, for Expressions, and dynamic Blocks” 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,167 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.