Geavanceerd
Zoeken inHow-to-gidsen
Zo zoek je
  • Typ de taak in je eigen woorden (poort vinden, firewall openen, schijf vol) of plak een foutmelding tussen aanhalingstekens.
  • Kies "Snel antwoord" om alleen de commando's te zien, of een gebied om in één vakgebied te blijven.
  • "twee woorden" vindt precies die zin.
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken
Probeer:

How-to 158IaC · Terraform

How to write a Terraform module

The same S3 bucket setup (versioning, encryption, block public access) copied into ten projects drifts apart and nobody updates all ten. A module packs it once with inputs and outputs; every project calls it with its own values, and a fix reaches all of them through a version bump. This how-to writes a small module, uses it locally and from Git with a pinned version, validates inputs, and refactors existing code into it without destroying anything.

  • 5 Lessen
  • 3 Commando's

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

$ als gewone gebruiker# als root (of met sudo)

158.1

The quick answer

modules/s3-bucket/main.tf
resource "aws_s3_bucket" "this" {
  bucket = var.name
  tags   = var.tags
}
 
resource "aws_s3_bucket_versioning" "this" {
  bucket = aws_s3_bucket.this.id
  versioning_configuration { status = "Enabled" }
}
 
resource "aws_s3_bucket_public_access_block" "this" {
  bucket                  = aws_s3_bucket.this.id
  block_public_acls       = true
  block_public_policy     = true
  ignore_public_acls      = true
  restrict_public_buckets = true
}
main.tf (the caller)
module "logs" {
  source = "./modules/s3-bucket"
  name   = "acme-shop-logs"
  tags   = { team = "shop", env = "prod" }
}
terraform init
terraform plan

158.2

Step by step

  1. The files of a module:
FileHolds
main.tfThe resources.
variables.tfInputs (with types, descriptions, validation).
outputs.tfValues the caller needs (IDs, ARNs).
versions.tfrequired_providers with version ranges.
README.mdWhat it does, inputs, outputs, an example.
  1. Inputs with types and checks:
modules/s3-bucket/variables.tf
variable "name" {
  type        = string
  description = "Globally unique bucket name."
  validation {
    condition     = can(regex("^[a-z0-9.-]{3,63}$", var.name))
    error_message = "Use 3-63 lower-case letters, digits, dots or hyphens."
  }
}
 
variable "tags" {
  type    = map(string)
  default = {}
}
  1. Outputs:
modules/s3-bucket/outputs.tf
output "arn" { value = aws_s3_bucket.this.arn }
output "id"  { value = aws_s3_bucket.this.id }

The caller uses module.logs.arn.

  1. Share it with a pinned version — from Git (a tag) or a registry:
main.tf (part)
module "logs" {
  source = "git::https://github.com/ORG/tf-modules.git//s3-bucket?ref=v1.2.0"
  name   = "acme-shop-logs"
}

Never point at a branch (main) — a change there would change every project's plan.

  1. Move existing resources into the module without recreating them — moved blocks:
moved.tf
moved {
  from = aws_s3_bucket.logs
  to   = module.logs.aws_s3_bucket.this
}

terraform plan must show 0 to destroy.

  1. Keep modules small and focused — one purpose (a bucket, a network), no provider blocks inside (the caller configures providers).

158.3

Check that it worked

terraform validate passes, a wrong name fails with your error message, and terraform plan in the caller shows the expected resources (or no changes after a moved refactor).

158.4

Common problems

ProblemCauseFix
Module not installedNew or changed source.terraform init (or init -upgrade for a new version).
Plan wants to destroy and recreate after refactorAddresses changed.Add moved blocks from old to new addresses.
Unsupported argument in the module callVariable not declared in the module.Add it to variables.tf.
All projects changed after a module editSource pointed to a branch.Pin ?ref=vX.Y.Z tags; bump on purpose.
Provider configuration errors in the moduleprovider block inside the module.Remove it; pass providers from the caller (providers = {} if needed).
Outputs not visible in the rootNot re-exported.Add root output blocks that use module.NAME.OUTPUT.

158.5

Go further

1Why pin a module source to a tag instead of a branch?

Every project then gets exactly the tested version; a change on a branch would silently change all plans.

2How do you move resources into a module without recreating them?

With moved { from = OLD to = NEW } blocks; the plan must show 0 to destroy.

Alle commando's in dit hoofdstuk