> ## Documentation Index
> Fetch the complete documentation index at: https://notes.kodekloud.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Output Variables

> This article explains how to define, view, and use output variables in OpenTofu configurations.

Output variables make it easy to expose values from your OpenTofu configuration—such as resource attributes—to users, scripts, or other tools. In this guide, you’ll learn how to define, view, and leverage output variables effectively.

## Table of Contents

* [Defining an Output Variable](#defining-an-output-variable)
* [Generic Output Syntax](#generic-output-syntax)
* [Output Block Arguments](#output-block-arguments)
* [Complete Example](#complete-example)
* [Viewing Outputs](#viewing-outputs)
* [Use Cases](#use-cases)
* [Links and References](#links-and-references)

***

## Defining an Output Variable

Use the `output` block with a unique name and the `value` argument set to the expression you want to expose. You can also include optional arguments like `description` and `sensitive`:

```hcl theme={null}
output "pub_ip" {
  value       = aws_instance.cerberus.public_ip
  description = "The public IPv4 address of the EC2 instance"
}
```

<Callout icon="lightbulb" color="#1CB2FE">
  Output names must be unique within a module. Use descriptive names to make them easy to reference in other modules or scripts.
</Callout>

***

## Generic Output Syntax

Below is the general form of an output block in OpenTofu:

```hcl theme={null}
output "<NAME>" {
  value       = <EXPRESSION>
  description = "<DESCRIPTION>"      # optional
  sensitive   = true | false        # optional
}
```

* **NAME**: The identifier for this output.
* **EXPRESSION**: Any valid expression or reference, such as `aws_instance.foo.id`.
* **description**: A human-readable summary (optional).
* **sensitive**: When set to `true`, the value is omitted from CLI output (optional).

***

## Output Block Arguments

| Argument    | Description                                              | Required |
| ----------- | -------------------------------------------------------- | -------- |
| value       | Expression or reference whose result you want to expose. | Yes      |
| description | A short human-readable explanation.                      | No       |
| sensitive   | Hides the value in CLI output when set to `true`.        | No       |

<Callout icon="triangle-alert" color="#FF6B6B">
  Mark any output containing secrets or credentials as `sensitive = true` to prevent leaking them in logs or console output.
</Callout>

***

## Complete Example

This minimal configuration creates an EC2 instance and then outputs its public IPv4 address:

```hcl theme={null}
resource "aws_instance" "cerberus" {
  ami           = var.ami
  instance_type = var.instance_type
}

output "pub_ip" {
  value       = aws_instance.cerberus.public_ip
  description = "The public IPv4 address of the EC2 instance"
}

variable "ami" {
  default = "ami-06178cf087598769c"
}

variable "instance_type" {
  default = "m5.large"
}

variable "region" {
  default = "eu-west-2"
}
```

***

## Viewing Outputs

After running `tofu apply`, OpenTofu automatically displays all configured outputs:

```bash theme={null}
$ tofu apply
...
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

Outputs:
pub_ip = 54.214.145.69
```

You can also list or fetch outputs at any time:

```bash theme={null}
# List all outputs
$ tofu output
pub_ip = 54.214.145.69

# Fetch a single output value
$ tofu output pub_ip
54.214.145.69
```

***

## Use Cases

* Quickly inspect provisioned resource attributes on-screen.
* Pass output values into other IaC tools, ad-hoc scripts, Ansible playbooks, or testing frameworks.
* Expose dynamic data for remote execution contexts or CI/CD pipelines.

***

## Links and References

* [OpenTofu Documentation](https://opentofu.io/)
* [Terraform Output Variables](https://www.terraform.io/language/values/outputs)
* [AWS Provider for Terraform](https://registry.terraform.io/providers/hashicorp/aws/latest/docs)

<CardGroup>
  <Card title="Watch Video" icon="video" cta="Learn more" href="https://learn.kodekloud.com/user/courses/opentofu-a-beginners-guide-to-a-terraform-fork-including-migration-from-terraform/module/c3586b29-e450-4c95-bad9-91bdf332eb24/lesson/b646ce21-3806-4df8-9e7e-8395f67da219" />
</CardGroup>
