Menu

Terragrunt remote_state and generate blocks

DEV Community: terraform·Bartłomiej Danek·6 months ago
#rH9yssdp
#dev#class#code#highlight#terragrunt
Reading 0:00
15s threshold

Terragrunt remote_state and generate blocks

remote_state

Defines the backend once in root.hcl - Terragrunt auto-generates the backend config for every unit that includes it.

# root.hcl remote_state { backend = "s3" config = { bucket = "my-tf-state" key = "${path_relative_to_include()}/terraform.tfstate" region = "eu-west-1" encrypt = true dynamodb_table = "terraform-locks" } generate = { path = "backend.tf" if_exists = "overwrite_terragrunt" } }

path_relative_to_include() resolves to the path of the calling terragrunt.hcl relative to root.hcl - e.g. environments/prod/vpc - giving each unit a unique state key automatically.

generate block

Writes any file before running Terraform - typically used for provider.tf and backend.tf so they don't need to be duplicated in every module.

generate "provider" { path = "provider.tf" if_exists = "overwrite_terragrunt" contents = << EOF provider "aws" { region = "eu-west-1"

default_tags { tags = { ManagedBy = "terraform" Environment = "prod" } } } EOF }

if_exists…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.