SkillByAIOpen interactive version →

Lesson 4 / 25

Resources and Their Arguments

The things Terraform manages.

type, name, arguments, attributes

A resource block has a type (from a provider, such as local_file or aws_s3_bucket) and a local name; together they form its address (local_file.config). Arguments inside the block describe the desired configuration; after creation the resource exposes attributes (ids, computed hashes, URLs) that other resources can reference, which also tells Terraform the order to create things in. Each provider's documentation lists the arguments and attributes for every resource type.

Resources, inputs, values, functions

HCL describes resources and connects them with variables, locals, outputs and expressions.

Figure 2.1 — Resources, variables, outputs and expressions.

The demo configuration

A variable with validation, locals, one resource and an output.

variable "env" {
  type        = string
  description = "Deployment environment"
  default     = "dev"

  validation {
    condition     = contains(["dev", "staging", "prod"], var.env)
    error_message = "env must be dev, staging or prod."
  }
}

locals {
  app_name = "shop"
  settings = "app=${local.app_name}\nenv=${var.env}\n"
}

resource "local_file" "config" {
  filename = "${path.module}/out/${local.app_name}-${var.env}.conf"
  content  = local.settings
}

output "config_path" {
  value = local_file.config.filename
}

Let references define order

Reference attributes (aws_subnet.a.id) instead of using depends_on; Terraform then knows the dependency automatically.

Quick check: What is the address of resource "local_file" "config"?

  • config.local_file
  • local_file.config
  • local_file/config
  • resource.config
Answer

local_file.config — type.name