Terraform & IaC

What You Give, What You Get: Arguments & Attributes

Lesson 4 of 14 · 9 min · Intermediate

Every resource block has two sides. Arguments are the values you hand Terraform before anything exists. Attributes are the values the provider hands back once the real thing has been created. Confusing the two is the most common source of "why doesn't this exist yet" errors.

Input Side, Output Side

Arguments

ami, instance_type, subnet_id, tags — written into the config, sent to the API.

→

Attributes

id, public_ip, private_ip, arn — discovered only after the resource is real.

1. Arguments

An argument is a configuration value that tells Terraform how to create or configure a resource — think of it as the input you hand over before anything exists.

main.tf
resource "aws_instance" "web" {
  ami           = "ami-0123456789abcdef0"
  instance_type = "t3.micro"
}
main.tf
resource "aws_instance" "web" {
  ami               = "ami-0123456789abcdef0"
  instance_type     = "t3.micro"
  subnet_id         = "subnet-123456"
  key_name          = "my-key"
  availability_zone = "ap-south-1a"
}

Five Arguments, One Instance

ami, instance_type, subnet_id, key_name, and availability_zone are all arguments — every one of these values is sent to the AWS API to create the EC2 instance.

main.tf
resource "aws_s3_bucket" "logs" {
  bucket = "shan-logs"
}
main.tf
resource "aws_instance" "web" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
  tags = {
    Name = "WebServer"
    Env  = "Production"
  }
}

Argument Types

STRING
instance_type = "t3.micro"
NUMBER
volume_size = 30
BOOLEAN
monitoring = true
LIST
security_groups = [
  "sg-111111",
  "sg-222222"
]
MAP
tags = {
  Name = "WebServer"
  Env  = "Prod"
}

Where Arguments Go

  • ami = "ami-123" · instance_type = "t3.micro"
  • ↓
  • Arguments
  • ↓
  • AWS API
  • ↓
  • Creates EC2

2. Attributes

An attribute is a value Terraform receives back from the provider after the resource is created. You don't write these values — Terraform discovers them.

main.tf
resource "aws_instance" "web" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
}
main.tf
aws_instance.web.public_ip

Reading the Reference

  • aws_instance
  • ↓
  • web
  • ↓
  • public_ip

More Attribute Examples

  • .id aws_instance.web.id returns i-0d12345abcd6789
  • .arn aws_instance.web.arn returns arn:aws:ec2:ap-south-1:123456789012:instance/i-12345
  • .private_ip aws_instance.web.private_ip returns 10.0.1.15
main.tf
resource "aws_vpc" "main" {
  cidr_block = "10.0.0.0/16"
}

resource "aws_subnet" "public" {
  vpc_id     = aws_vpc.main.id
  cidr_block = "10.0.1.0/24"
}

aws_vpc.main.id

Terraform gets the VPC ID only after the VPC is created, then automatically passes it into the subnet's vpc_id argument — an attribute feeding straight into the next resource's argument.

main.tf
output "instance_ip" {
  value = aws_instance.web.public_ip
}
terminal
terminalRESULT

$ terraform apply
instance_ip = 54.201.120.15

Input vs Output

  • ami · instance_type · subnet_id · tags
  • ↓ Arguments
  • Terraform creates EC2
  • ↓ AWS returns
  • Instance ID · Public IP · Private IP · ARN
  • ↓
  • Attributes

Arguments vs Attributes

ArgumentsAttributes
Input valuesOutput values
Defined by youGenerated by the provider
Used to configure resourcesUsed to retrieve information about resources
Known before creationOften known only after creation
Example: ami, instance_type, tagsExample: id, public_ip, private_ip, arn

Complete Example

main.tf
resource "aws_instance" "web" {
  ami           = "ami-0123456789abcdef0"
  instance_type = "t3.micro"
  tags = {
    Name = "WebServer"
  }
}

output "public_ip" {
  value = aws_instance.web.public_ip
}

output "instance_id" {
  value = aws_instance.web.id
}

The Flow

  • Arguments: ami · instance_type · tags
  • ↓
  • Terraform
  • ↓
  • AWS Provider
  • ↓
  • AWS Creates EC2
  • ↓
  • Attributes: id · public_ip · private_ip · arn
  • ↓
  • Terraform Outputs