Terraform
Introduction
Output

Terraform Output Examples

1. Basic Output Example

Configuration:

output "vpc_id" {
  value = aws_vpc.main.id
  description = "The ID of the VPC"
}

Explanation:

  • vpc_id: The name of the output variable.
  • value: Specifies the data to be returned. Here, it returns the ID of the VPC created in your configuration.
  • description: Provides a description of the output for clarity.

Usage: After running terraform apply, the output will display the VPC ID:

Outputs:
 
vpc_id = vpc-0123456789abcdef0

2. Complex Output Example

Configuration:

output "public_subnet_info" {
  value = {
    id   = aws_subnet.public.id
    cidr = aws_subnet.public.cidr_block
    az   = aws_subnet.public.availability_zone
  }
  description = "Information about the public subnet"
}

Explanation:

  • value: A map that includes multiple pieces of information about the public subnet.
    • id: Returns the ID of the public subnet.
    • cidr: Returns the CIDR block of the public subnet.
    • az: Returns the availability zone where the subnet is located.
  • description: Describes what information the output provides.

Usage: The output will display a structured map:

Outputs:
 
public_subnet_info = {
  "az" = "us-west-2a"
  "cidr" = "10.0.1.0/24"
  "id" = "subnet-0123456789abcdef0"
}

3. Output with Sensitive Information

Configuration:

output "db_password" {
  value     = aws_db_instance.example.master_password
  sensitive = true
  description = "The database password"
}

Explanation:

  • value: Returns the master password of the database instance.
  • sensitive: Marks the output as sensitive. Terraform will mask this output to avoid accidental exposure.
  • description: Describes what the output represents.

Usage: Sensitive outputs are masked in the Terraform output:

Outputs:
 
db_password = [sensitive]

4. Output Referencing Another Configuration

If you have multiple Terraform configurations, you can use outputs from one configuration in another using terraform_remote_state.

Configuration in network.tf:

output "vpc_id" {
  value = aws_vpc.main.id
}

Configuration in application.tf:

data "terraform_remote_state" "network" {
  backend = "s3"
  config = {
    bucket         = "my-terraform-state"
    key            = "network/terraform.tfstate"
    region         = "us-west-2"
  }
}
 
resource "aws_instance" "app" {
  vpc_security_group_ids = [data.terraform_remote_state.network.outputs.vpc_id]
  # other configuration
}

Explanation:

  • terraform_remote_state data source: Allows you to access outputs from another Terraform configuration stored in an S3 bucket.
  • data.terraform_remote_state.network.outputs.vpc_id: References the VPC ID from the network configuration.

5. Output for Module Integration

When using Terraform modules, you can define outputs in the module and reference them in the root module.

Configuration in module/main.tf:

output "subnet_id" {
  value = aws_subnet.public.id
}

Configuration in root/main.tf:

module "network" {
  source = "./module"
}
 
output "public_subnet_id" {
  value = module.network.subnet_id
}

Explanation:

  • module "network": References the module containing the VPC configuration.
  • module.network.subnet_id: Accesses the subnet_id output from the module.

6. Formatting Output

You can format outputs to make them more readable or useful.

Configuration:

output "formatted_ip" {
  value = format("The public IP address is: %s", aws_instance.web.public_ip)
  description = "Formatted public IP address"
}

Explanation:

  • format: Formats the output string with placeholders.

Usage:

Outputs:
 
formatted_ip = The public IP address is: 203.0.113.25

Conclusion

Terraform outputs are powerful tools for extracting and managing information from your infrastructure configuration. They are useful for sharing data between modules, integrating with other configurations, and displaying critical information about your resources. By using the examples above, you can tailor your outputs to fit your specific needs and workflows.

If you need any more help or additional examples, feel free to ask! 🚀


🧙 AI Wizard - Instant Page Insights

Click the button below to analyze this page.
Get an AI-generated summary and key insights in seconds.
Powered by Perplexity AI!