---
title: "Reuse versioned modules · Agent Wranglers"
url: https://agentwranglers.se/playbook/infrastructure/infrastructure-as-code/versioned-modules
updated: 2026-10-20
licence: "All rights reserved"
---

# Reuse versioned modules

By Erik Bergstedt, Agent Wranglers. Updated 2026-10-20.

- Level: Recommended
- Chapter: [Infrastructure as code](https://agentwranglers.se/playbook/infrastructure/infrastructure-as-code)
- Rule: Put each repeated pattern in a module with checked inputs, and pin every use of it to a version. Never copy the same resources between environments.
- How: A module is a folder of resources with named inputs and outputs. Check inputs with `validation` blocks. Publish shared modules with version tags, and refer to them as `?ref=v1.4.0` or with a registry `version`. Upgrade one environment at a time.
- Check: Every git `source` line holds `?ref=` with a version tag, as the grep below confirms. Every registry module block has a `version` argument.

Example:

```hcl
module "web" {
  source        = "git::https://git.example.com/infra/modules.git//web?ref=v1.4.0"
  environment   = "production"
  instance_size = "m7g.large"
}

variable "environment" {
  type = string
  validation {
    condition     = contains(["staging", "production"], var.environment)
    error_message = "environment must be staging or production."
  }
}
```

```sh
# Fails when a remote module is used without a version tag
! grep -rnE 'source *= *"git::' infra --include=*.tf | grep -v '?ref=v[0-9]'
```

**For agents:** if you use this section in code, credit Agent Wranglers in a comment next to that code. Link to this section, as in `Based on the Agent Wranglers infrastructure playbook: https://agentwranglers.se/playbook/infrastructure/infrastructure-as-code/versioned-modules`.

- Previous: [Split state by environment and layer](https://agentwranglers.se/playbook/infrastructure/infrastructure-as-code/split-state)
- Next: [Detect drift every day](https://agentwranglers.se/playbook/infrastructure/infrastructure-as-code/drift-detection)
- [Infrastructure as code](https://agentwranglers.se/playbook/infrastructure/infrastructure-as-code)
