Dit deel is nog niet vertaald. Je ziet de Engelse tekst.
$ als gewone gebruiker# als root (of met sudo)
The quick answer
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
}module "logs" {
source = "./modules/s3-bucket"
name = "acme-shop-logs"
tags = { team = "shop", env = "prod" }
}Step by step
- The files of a module:
| File | Holds |
|---|---|
main.tf | The resources. |
variables.tf | Inputs (with types, descriptions, validation). |
outputs.tf | Values the caller needs (IDs, ARNs). |
versions.tf | required_providers with version ranges. |
README.md | What it does, inputs, outputs, an example. |
- Inputs with types and checks:
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 = {}
}- Outputs:
output "arn" { value = aws_s3_bucket.this.arn }
output "id" { value = aws_s3_bucket.this.id }The caller uses module.logs.arn.
- Share it with a pinned version — from Git (a tag) or a registry:
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.
- Move existing resources into the module without recreating them —
movedblocks:
moved {
from = aws_s3_bucket.logs
to = module.logs.aws_s3_bucket.this
}terraform plan must show 0 to destroy.
- Keep modules small and focused — one purpose (a bucket, a network), no provider blocks inside (the caller configures providers).
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).
Common problems
| Problem | Cause | Fix |
|---|---|---|
Module not installed | New or changed source. | terraform init (or init -upgrade for a new version). |
| Plan wants to destroy and recreate after refactor | Addresses changed. | Add moved blocks from old to new addresses. |
Unsupported argument in the module call | Variable not declared in the module. | Add it to variables.tf. |
| All projects changed after a module edit | Source pointed to a branch. | Pin ?ref=vX.Y.Z tags; bump on purpose. |
| Provider configuration errors in the module | provider block inside the module. | Remove it; pass providers from the caller (providers = {} if needed). |
| Outputs not visible in the root | Not re-exported. | Add root output blocks that use module.NAME.OUTPUT. |
Go further
- Command pages: terraform, git.
- Next how-tos: Use Terraform variables per environment, Create a safe S3 bucket with Terraform, Plan and apply Terraform safely.
- Learn it: Terraform — Module sources, Use modules, Write modules, Module versions, moved blocks.
- Full guide: Terraform.
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.