Table of Contents

Output Variables

Output variables are used to return values from a Terraform configuration. They are useful for passing information to other configurations or tools.

output "instance_id" {
  description = "The ID of the EC2 instance"
  value       = aws_instance.example.id
}
 
output "instance_public_ip" {
  description = "The public IP address of the EC2 instance"
  value       = aws_instance.example.public_ip
}

Examples

Basic Example

variable "instance_type" {
  description = "Type of EC2 instance"
  type        = string
  default     = "t2.micro"
}
 
resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = var.instance_type
}

List and Map Example

variable "availability_zones" {
  description = "List of availability zones"
  type        = list(string)
  default     = ["us-west-2a", "us-west-2b"]
}
 
variable "tags" {
  description = "Map of tags to assign to resources"
  type        = map(string)
  default     = {
    Environment = "dev"
    Project     = "terraform"
  }
}
 
resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"
  availability_zone = element(var.availability_zones, 0)
 
  tags = var.tags
}