Skip to main content

Defining Output Values

1. Declaring Output Values

Property Description Example
output "name" Defines an output value with a specified name
output "instance_id" {
  value = aws_instance.example.id
}
value Expression to evaluate and return as output
output "public_ip" {
  value = aws_instance.web.public_ip
}
description Optional description for the output value
output "vpc_id" {
  description = "The ID of the VPC"
  value       = aws_vpc.main.id
}
sensitive Indicates whether the output value should be hidden in logs
output "admin_password" {
  value     = aws_instance.admin.password
  sensitive = true
}

Example: Basic Output Declaration

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

Example: Output with Description

output "public_dns" {
  description = "Public DNS name of the web server"
  value       = aws_instance.web.public_dns
}

2. Exposing Resource Attributes

Attribute Type Description Example
aws_instance.web_server.id string ID of the EC2 instance "i-1234567890abcdef0"
aws_instance.web_server.private_ip string Private IP address of the instance "10.0.1.10"
aws_instance.web_server.public_ip string Public IP address of the instance "54.123.45.67"
aws_s3_bucket.example.bucket_domain_name string Fully qualified domain name for the bucket "my-example-bucket.s3.amazonaws.com"

Example: Outputting Instance ID

resource "aws_instance" "web_server" {
  ami           = "ami-12345678"
  instance_type = "t2.micro"
}

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

Example: Outputting S3 Bucket Domain Name

resource "aws_s3_bucket" "example" {
  bucket = "my-example-bucket"
}

output "s3_domain_name" {
  value = aws_s3_bucket.example.bucket_domain_name
}

3. Adding Output Descriptions

Property Description Example
description Human-readable description of the output value "The public IP address of the instance"

Example: Defining Output with Description

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

4. Marking Outputs Sensitive

When defining outputs in Terraform, you can mark them as sensitive to prevent their values from being displayed in logs or output during plan/apply operations.

Property Description Values Notes
sensitive Indicates whether the output value should be treated as sensitive true or false Defaults to false. When set to true, the output value is masked in logs and CLI output.

Example: Defining a Sensitive Output

output "api_key" {
  value = aws_iam_access_key.main.secret
  sensitive = true
}
Note: Even with sensitive = true, the value is still visible in the state file if it's stored there. For enhanced security, consider using external data sources or other methods to avoid storing sensitive values in state.

5. Setting Output Dependencies

Output Type Description Dependencies
output.name string Name of the output value None
output.value any Value to be exported None
output.sensitive boolean Whether the output is sensitive (redacted from logs) None
output.description string Description of the output value None

Example: Defining Outputs with Dependencies

output "instance_public_ip" {
  value = aws_instance.example.public_ip
  description = "Public IP of the EC2 instance"
  sensitive = false
}

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

output "database_endpoint" {
  value = aws_db_instance.main.endpoint
  description = "Database endpoint URL"
  sensitive = true
}
Note: Outputs must reference values that are computed or resolved during the Terraform execution phase. Dependencies on resources or data sources must be explicitly declared in the configuration.

6. Viewing Output Values

Example: Output Declaration

output 'instance_public_ip' {
  value = aws_instance.example.public_ip
  description = 'The public IP of the instance'
}
Property Description Example
value The value to output aws_instance.example.public_ip
description Optional description of the output 'The public IP of the instance'
sensitive Whether the output value is sensitive (redacted from logs) true
Note: Outputs are displayed after a terraform apply command and can be used in other Terraform configurations.

Example: Viewing Outputs

terraform output

Example: Viewing Specific Output

terraform output instance_public_ip

7. Formatting Output

Property Description Example
format Specifies the format for output values. Valid options are string, number, bool, and list.
output "formatted_output" {
  value = format("The server is at %s:%d", var.server_ip, var.server_port)
  format = "string"
}
sensitive Indicates whether the output value is sensitive and should be redacted in logs. Defaults to false.
output "api_key" {
  value = aws_iam_access_key.main.secret
  sensitive = true
}
description Provides a description for the output value to improve readability in UIs and documentation.
output "instance_id" {
  value = aws_instance.main.id
  description = "The ID of the EC2 instance created"
}

Example: Using format function in output

output "server_info" {
  value = format("Server: %s, Port: %d, SSL: %t", var.server_name, var.port, var.enable_ssl)
  description = "Formatted server information"
}
Note: The format function allows for dynamic string formatting, useful when combining multiple values into a single output.

8. Using Outputs in Parent Modules

Outputs defined in a child module can be consumed by the parent module that calls it. This allows for modularizing infrastructure definitions while maintaining a clean interface.

Property Description Usage
output.name Name of the output value Used in parent module to reference the child's output
output.value Value to be exposed from the child module Referenced in parent using module.<name>.<output_name>

Example: Defining Output in Child Module

output "instance_id" {
  value = aws_instance.example.id
  description = "ID of the EC2 instance"
}

Example: Consuming Output in Parent Module

module "web_server" {
  source = "./web_server_module"
}

resource "aws_s3_bucket" "logs" {
  bucket = module.web_server.instance_id
}

9. Exporting State Data

Property Description Example
output.name Name of the output value instance_id
output.value Value to export (can be a reference) aws_instance.example.id
output.description Optional description for the output ID of the EC2 instance
output.sensitive Boolean to mark output as sensitive (not shown in logs) true

Example: Basic Output Definition

output &#34;instance_id&#34; {
  value = aws_instance.example.id
}

Example: Sensitive Output

output &#34;private_key&#34; {
  value = tls_private_key.example.private_key_pem
  sensitive = true
}
Note: Outputs are exported during terraform apply and can be accessed via terraform output or referenced in other configurations.

10. Using Preconditions in Outputs

Property Description Values Notes
condition Boolean expression to validate output value true or false Must evaluate to a boolean
error_message Message to display if condition fails String Optional but recommended for clarity

Example: Validating Output with Precondition

output "instance_public_ip" {
  value = aws_instance.example.public_ip
  condition = length(aws_instance.example) > 0
  error_message = "No instance found to get public IP"
}
Note: Precondition checks run during terraform plan and terraform apply, ensuring that output values meet specified criteria before deployment.

Was this article helpful?