# terraform / arguments / attributes

What You Give, What You Get: Arguments & Attributes

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.

$ terraform apply
amiargument · known before
public_ipattribute · known after
Input
Arguments
Output
Attributes
# the split

Input Side, Output Side

you provide

Arguments

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

provider returns

Attributes

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

# inputs

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.tfBASIC ARGUMENTS
1 2 3 4
resource "aws_instance" "web" {
  ami           = "ami-0123456789abcdef0"
  instance_type = "t3.micro"
}
main.tfFULLER EXAMPLE
1 2 3 4 5 6
resource "aws_instance" "web" {
  ami               = "ami-0123456789abcdef0"
  instance_type     = "t3.micro"
  subnet_id         = "subnet-123456"
  key_name          = "my-key"
  availability_zone = "ap-south-1a"
}
sent to the API

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.tfS3 BUCKET
1 2 3
resource "aws_s3_bucket" "logs" {
  bucket = "shan-logs"
}
main.tfTAGS ARGUMENT
1 2 3 4 5 6 7
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
# outputs

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.tfRESOURCE
1 2 3
resource "aws_instance" "web" {
  ami           = "ami-123456"
  instance_type = "t2.micro"
}
AFTER CREATION, AWS RETURNS

Instance ID, public IP, private IP, ARN, and public DNS — none of these existed until the instance was actually launched.

main.tfACCESSING AN ATTRIBUTE
1
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.tfRESOURCE REFERENCE
1 2 3 4 5 6
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"
}
attribute in the wild

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.tfOUTPUT BLOCK
1 2 3
output "instance_ip" {
  value = aws_instance.web.public_ip
}
terminalRESULT
$ terraform apply
instance_ip = 54.201.120.15
# the round trip

Input vs Output

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

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
# end to end

Complete Example

main.tfFULL CONFIG
1 2 3 4 5 6 7 8 9 10 11 12 13
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