Locals

locals 通常在 module 內用於重複使用,常常用來將 variable 的值拿來運算或者特殊處理。

語法

  • locals 包含多個 variable 一次餵給 resource。

# Define the common tags for all resources
locals {
  common_tags = {
    Component   = "awesome-app"
    Environment = "production"
  }
}

# Create a resource that blends the common tags with instance-specific tags.
resource "aws_instance" "server" {
  ami           = "ami-123456"
  instance_type = "t2.micro"

  tags = "${merge(
    local.common_tags,
    map(
      "Name", "awesome-app-server",
      "Role", "server"
    )
  )}"
}
  • locals 內可以包含 locals 進行多次處理後才給 resource。

Last updated

Was this helpful?