> ## 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.

# Exposing Configuration Data with Output Values

> How Terraform output blocks expose and manage configuration values like IPs, DNS names, IDs and secrets, including examples, commands, sensitivity handling, and best practices.

Welcome to this lesson on the Terraform `output` block. Outputs let you expose important data produced by your Terraform runs—such as IP addresses, DNS names, resource IDs, and connection strings—so people, scripts, and other modules can consume them without digging through cloud consoles or re-running queries.

## Why use outputs?

* Capture useful attributes created by Terraform (VM IPs, DB connection strings, DNS names, etc.).
* Display important values immediately after `terraform apply` for quick testing.
* Persist outputs in state so they can be retrieved later with `terraform output`.
* Pass values between modules to enable composable infrastructure.
* Make CI/CD automation and scripts more reliable using `terraform output -json`.

## Example: Azure resources

These resource blocks create an Azure Resource Group and a Virtual Network. After creation, surface values such as the resource group name, VNet ID, or subnet IDs with outputs so other systems or modules can use them.

```hcl theme={null}
# Azure resource examples
resource "azurerm_resource_group" "prd" {
  name     = "example-resources"
  location = "West Europe"
}

resource "azurerm_virtual_network" "dv" {
  name                = "example-network"
  resource_group_name = azurerm_resource_group.prd.name
  location            = azurerm_resource_group.prd.location
  address_space       = ["10.0.0.0/16"]
}
```

Use outputs to export values that are consumed outside this configuration—root modules, CI pipelines, or manual testers.

## Basic output examples (AWS resources)

Below are typical outputs you might declare for an EC2 instance and a load balancer. These show simple attribute exposure, composed values, and how to produce a ready-to-use URL.

```hcl theme={null}
# Output Instance IP Address
output "instance_public_ip" {
  description = "Public IP of Server"
  value       = aws_instance.web.public_ip
}

# Output DNS Name for Load Balancer
output "website_dns" {
  description = "Website DNS Record"
  value       = aws_elb.web_app.dns_name
}

# Output Friendly URL of Website
output "website_url" {
  description = "Friendly URL for the website"
  value       = "https://${aws_alb.web.dns_name}"
}
```

* `instance_public_ip`: exposes a single resource attribute.
* `website_dns`: exports the load balancer's DNS name.
* `website_url`: composes a click-ready HTTPS URL around a resource attribute.

## Dissecting an `output` block

Each output block typically contains a few common elements:

| Field         | Purpose                                                                                | Example                      |
| ------------- | -------------------------------------------------------------------------------------- | ---------------------------- |
| `name`        | Identifier used to reference the output                                                | `instance_public_ip`         |
| `description` | Optional human-readable explanation                                                    | `"Public IP of Server"`      |
| `value`       | Expression that computes the output (resource attributes, maps, lists, or expressions) | `aws_instance.web.public_ip` |
| `sensitive`   | Hides the value in CLI and logs when set to `true`                                     | `sensitive = true`           |

Use `description` to document intent and `sensitive` to prevent accidental exposure in terminal output.

## Common `terraform output` commands

Use these commands locally or in automation:

| Command                        | What it does                                             |
| ------------------------------ | -------------------------------------------------------- |
| `terraform output`             | Show all outputs (non-sensitive values hidden if marked) |
| `terraform output website_url` | Show a single output by name                             |
| `terraform output -json`       | Emit all outputs in JSON for scripts and CI              |

## Sensitive outputs

Mark outputs that contain secrets so Terraform hides them in interactive CLI output and most logs.

```hcl theme={null}
output "db_password" {
  description = "RDS master password"
  value       = aws_db_instance.default.password
  sensitive   = true
}
```

<Callout icon="lightbulb" color="#1CB2FE">
  Mark outputs as `sensitive = true` for any secret or credential. Sensitive outputs are still recorded in the Terraform state (so secure your backend accordingly). Treat your state file as sensitive data.
</Callout>

## Best practices and important considerations

* Remember outputs are stored in state: protect your state backend and access control.
* Limit outputs to values that are useful externally (URLs, IPs, IDs required by other systems).
* Prefer secret managers for long-term secret storage; do not rely solely on outputs for sensitive secrets.
* Use outputs to pass data between modules: a child module declares outputs that the parent (or other modules) can read.
* Avoid exposing low-value or noisy attributes that are not consumed by other systems.

<Callout icon="warning" color="#FF6B6B">
  Do not store highly sensitive secrets only in outputs unless your state backend is secured and access is tightly controlled. Consider using dedicated secret management solutions for production secrets.
</Callout>

## Quick reference table

| Use case                  | Example output                                                                                 |
| ------------------------- | ---------------------------------------------------------------------------------------------- |
| Expose a single attribute | `output "instance_public_ip" { value = aws_instance.web.public_ip }`                           |
| Compose a URL             | `output "website_url" { value = "https://${aws_alb.web.dns_name}" }`                           |
| Return structured data    | `output "subnet_map" { value = { public = aws_subnet.pub.id, private = aws_subnet.priv.id } }` |
| Mark sensitive values     | `output "db_password" { value = var.db_password, sensitive = true }`                           |

## Summary

* `output` blocks provide a structured way to expose information from Terraform configurations.
* Outputs appear after `terraform apply`, are stored in state, and can be consumed by scripts, CI pipelines, and other modules.
* Use `description` and `sensitive` to make outputs easier and safer to use.
* Keep outputs focused on externally useful values and secure your state backend.

Further reading and references:

* [Terraform Outputs Documentation](https://www.terraform.io/docs/cli/commands/output.html)
* [Terraform State Concepts](https://www.terraform.io/docs/state/index.html)
* [Azure Provider Documentation](https://registry.terraform.io/providers/hashicorp/azurerm/latest/docs)
* [AWS Provider Documentation](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/hashicorp-certified-terraform-associate-004/module/d67e8dc8-0136-4296-966d-229a1d9f46bc/lesson/b170a16c-2fe0-4561-aca0-c37474762dce" />

  <Card title="Practice Lab" icon="flask-conical" cta="Learn more" href="https://learn.kodekloud.com/user/courses/hashicorp-certified-terraform-associate-004/module/d67e8dc8-0136-4296-966d-229a1d9f46bc/lesson/c8f283e2-b2d4-4ad5-a340-3d2ab32ab665" />
</CardGroup>
