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.
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.
resource "aws_instance" "web" {
ami = "ami-0123456789abcdef0"
instance_type = "t3.micro"
}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.
resource "aws_s3_bucket" "logs" {
bucket = "shan-logs"
}resource "aws_instance" "web" {
ami = "ami-123456"
instance_type = "t2.micro"
tags = {
Name = "WebServer"
Env = "Production"
}
}Argument Types
instance_type = "t3.micro"volume_size = 30monitoring = truesecurity_groups = [
"sg-111111",
"sg-222222"
]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.
resource "aws_instance" "web" {
ami = "ami-123456"
instance_type = "t2.micro"
}aws_instance.web.public_ipReading the Reference
- aws_instance
- ↓
- web
- ↓
- public_ip
More Attribute Examples
- .id
aws_instance.web.idreturnsi-0d12345abcd6789 - .arn
aws_instance.web.arnreturnsarn:aws:ec2:ap-south-1:123456789012:instance/i-12345 - .private_ip
aws_instance.web.private_ipreturns10.0.1.15
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.
output "instance_ip" {
value = aws_instance.web.public_ip
}terminalRESULT
$ terraform apply
instance_ip = 54.201.120.15Input 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
| Arguments | Attributes |
|---|---|
| Input values | Output values |
| Defined by you | Generated by the provider |
| Used to configure resources | Used to retrieve information about resources |
| Known before creation | Often known only after creation |
Example: ami, instance_type, tags | Example: id, public_ip, private_ip, arn |
Complete Example
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