Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
126 changes: 63 additions & 63 deletions content/terraform-docs-common/redirects.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -544,69 +544,69 @@
"permanent": true
},
// values → parameterize
{
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Commenting these out for now until we know the new paths.

"source": "/terraform/language/values",
"destination": "/terraform/language/parameterize",
"permanent": true
},
{
"source": "/terraform/language/v:version(1\\.(?:[1-9]|1[01])\\.x)/parameterize",
"destination": "/terraform/language/v:version/values",
"permanent": true
},
{
"source": "/terraform/language/v:version((?:1\\.(?:1[2-9]|[2-9]\\d)|[2-9]\\d*\\.\\d+)\\.x)/values",
"destination": "/terraform/language/v:version/parameterize",
"permanent": true
},
// values/locals → block/locals
{
"source": "/terraform/language/values/locals",
"destination": "/terraform/language/block/locals",
"permanent": true
},
{
"source": "/terraform/language/v:version(1\\.(?:[1-9]|1[01])\\.x)/block/locals",
"destination": "/terraform/language/v:version/values/locals",
"permanent": true
},
{
"source": "/terraform/language/v:version((?:1\\.(?:1[2-9]|[2-9]\\d)|[2-9]\\d*\\.\\d+)\\.x)/values/locals",
"destination": "/terraform/language/v:version/block/locals",
"permanent": true
},
// values/variables → block/variable
{
"source": "/terraform/language/values/variables",
"destination": "/terraform/language/block/variable",
"permanent": true
},
{
"source": "/terraform/language/v:version(1\\.(?:[1-9]|1[01])\\.x)/block/variable",
"destination": "/terraform/language/v:version/values/variables",
"permanent": true
},
{
"source": "/terraform/language/v:version((?:1\\.(?:1[2-9]|[2-9]\\d)|[2-9]\\d*\\.\\d+)\\.x)/values/variables",
"destination": "/terraform/language/v:version/block/variable",
"permanent": true
},
// values/outputs → block/output
{
"source": "/terraform/language/values/outputs",
"destination": "/terraform/language/block/output",
"permanent": true
},
{
"source": "/terraform/language/v:version(1\\.(?:[1-9]|1[01])\\.x)/block/output",
"destination": "/terraform/language/v:version/values/outputs",
"permanent": true
},
{
"source": "/terraform/language/v:version((?:1\\.(?:1[2-9]|[2-9]\\d)|[2-9]\\d*\\.\\d+)\\.x)/values/outputs",
"destination": "/terraform/language/v:version/block/output",
"permanent": true
},
// {
// "source": "/terraform/language/values",
// "destination": "/terraform/language/parameterize",
// "permanent": true
// },
// {
// "source": "/terraform/language/v:version(1\\.(?:[1-9]|1[01])\\.x)/parameterize",
// "destination": "/terraform/language/v:version/values",
// "permanent": true
// },
// {
// "source": "/terraform/language/v:version((?:1\\.(?:1[2-9]|[2-9]\\d)|[2-9]\\d*\\.\\d+)\\.x)/values",
// "destination": "/terraform/language/v:version/parameterize",
// "permanent": true
// },
// // values/locals → block/locals
// {
// "source": "/terraform/language/values/locals",
// "destination": "/terraform/language/block/locals",
// "permanent": true
// },
// {
// "source": "/terraform/language/v:version(1\\.(?:[1-9]|1[01])\\.x)/block/locals",
// "destination": "/terraform/language/v:version/values/locals",
// "permanent": true
// },
// {
// "source": "/terraform/language/v:version((?:1\\.(?:1[2-9]|[2-9]\\d)|[2-9]\\d*\\.\\d+)\\.x)/values/locals",
// "destination": "/terraform/language/v:version/block/locals",
// "permanent": true
// },
// // values/variables → block/variable
// {
// "source": "/terraform/language/values/variables",
// "destination": "/terraform/language/block/variable",
// "permanent": true
// },
// {
// "source": "/terraform/language/v:version(1\\.(?:[1-9]|1[01])\\.x)/block/variable",
// "destination": "/terraform/language/v:version/values/variables",
// "permanent": true
// },
// {
// "source": "/terraform/language/v:version((?:1\\.(?:1[2-9]|[2-9]\\d)|[2-9]\\d*\\.\\d+)\\.x)/values/variables",
// "destination": "/terraform/language/v:version/block/variable",
// "permanent": true
// },
// // values/outputs → block/output
// {
// "source": "/terraform/language/values/outputs",
// "destination": "/terraform/language/block/output",
// "permanent": true
// },
// {
// "source": "/terraform/language/v:version(1\\.(?:[1-9]|1[01])\\.x)/block/output",
// "destination": "/terraform/language/v:version/values/outputs",
// "permanent": true
// },
// {
// "source": "/terraform/language/v:version((?:1\\.(?:1[2-9]|[2-9]\\d)|[2-9]\\d*\\.\\d+)\\.x)/values/outputs",
// "destination": "/terraform/language/v:version/block/output",
// "permanent": true
// },
// /terraform → block/terraform
{
"source": "/terraform/language/terraform",
Expand Down
23 changes: 20 additions & 3 deletions content/terraform/v1.12.x/data/language-nav-data.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,26 @@
]
},
{
"title": "Set configuration parameters",
"path": "parameterize",
"alias": "variables, outputs, locals"
"title": "Manage values in modules",
"alias": "variables, outputs, locals",
"routes": [
{
"title": "Overview",
"path": "values"
},
{
"title": "Use variables",
"path": "values/variables"
},
{
"title": "Use locals",
"path": "values/locals"
},
{
"title": "Use outputs",
"path": "values/outputs"
}
]
},
{
"title": "Manage sensitive data",
Expand Down
2 changes: 1 addition & 1 deletion content/terraform/v1.12.x/docs/language/block/locals.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ The `locals` block is similar to function-scoped variables in other programming

You can define local values in any module. Local values can be any valid Terraform expression, and can reference variables, resource attributes, function outputs, or other local values to transform or combine data.

To learn more about using `locals` blocks in your configurations, refer to [Define locals to reuse expressions](/terraform/language/parameterize#define-locals-to-reuse-expressions).
To learn more about using `locals` blocks in your configurations, refer to [Use locals to reuse expressions](/terraform/language/values/locals).

## Configuration model

Expand Down
2 changes: 1 addition & 1 deletion content/terraform/v1.12.x/docs/language/block/output.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ The value of an `output` block is similar to a return value in other programming

In HCP Terraform, your `output` block values appear in the UI after Terraform applies your configuration. You can [mark outputs as sensitive in HCP Terraform](/terraform/cloud-docs/workspaces/variables/managing-variables#sensitive-values) to hide their values in the UI and in API responses.

To learn more about using `output` blocks, refer to [Define outputs to expose module data](/terraform/language/parameterize#define-outputs-to-expose-module-data).
To learn more about using `output` blocks, refer to [Use outputs to expose module data](/terraform/language/values/outputs).

## Configuration model

Expand Down
4 changes: 2 additions & 2 deletions content/terraform/v1.12.x/docs/language/block/variable.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ description: Use variables to parameterize your configuration, making your modul

# `variable` block reference

Use the `variable` block to parameterize your configuration, making your modules dynamic, reusable, and flexible by letting them customize behavior with different values at run time.
Variables define the interface of your module by specifying the values your module accepts as arguments. Use the `variable` block to let module consumers pass in values to customize behavior at runtime.

> **Hands-on:** Try the [Customize Terraform Configuration with Variables](/terraform/tutorials/configuration-language/variables) tutorial.

Expand All @@ -17,7 +17,7 @@ You can define variables in root modules, or in child modules:
- In the root modules, you can set variable values using CLI options, environment variables, variable definition files, or through an HCP Terraform workspace.
- In child modules, the parent module passes values to child modules as arguments to the `module` block.

To learn more about the different ways to use and set values for variables, refer to [Define module input arguments with variables](/terraform/language/parameterize#define-module-input-arguments-with-variables).
To learn more about the different ways to use and set values for variables, refer to [Use variables to input module arguments](/terraform/language/values/variables).

## Configuration model

Expand Down
95 changes: 95 additions & 0 deletions content/terraform/v1.12.x/docs/language/values/index.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
---
page_title: Manage values in modules
description: Learn how to make Terraform modules flexible, composable, and reusable by defining input variables, local values, and output values for your module.
---

# Manage values in modules

Make your Terraform modules flexible, composable, and reusable by defining input variable values, local values, and output values.

## Background

Terraform modules communicate with each other through the interface of inputs and outputs. Variables add parameters to your module, letting users input values at runtime. Outputs expose data from a module, letting you export information about your infrastructure. Locals let you define and reuse expressions within a module.

Defining clear interfaces for your modules creates boundaries between them, letting you compose infrastructure from reusable components while keeping each module's implementation independent.

## Define variables to input module arguments

Variables define the input interface of your module by specifying the values your module accepts as arguments.

For example, the following `variable` block defines an input variable named `instance_type`, letting a module consumer specify an EC2 instance type for `web` at runtime:

<CodeBlockConfig highlight="1,8" hideClipboard>

```hcl
variable "instance_type" {
type = string
description = "EC2 instance type for the web server"
default = "t2.micro"
}

resource "aws_instance" "web" {
instance_type = var.instance_type
# ...
}
```

</CodeBlockConfig>


To learn more about defining variables, assigning values to them, and managing variables in HCP Terraform, refer to [Use variables to input module arguments](/terraform/language/values/variables). To learn more about the `variable` block and its arguments, refer to the [`variable` block reference](/terraform/language/block/variable).

## Define locals to reuse expressions

Locals define temporary values scoped to your module, letting you name and reuse expressions in your configuration. For example, the following `locals` block defines a local value for the name of an application:

<CodeBlockConfig highlight="1,8">

```hcl
locals {
app_name = "${var.project_name}-${var.environment}"
}

resource "aws_instance" "example" {
# ...
tags = {
Name = local.app_name
}
}

resource "aws_instance" "example2" {
# ...
tags = {
Name = local.app_name
}
}
```

</CodeBlockConfig>

You can reference locals in other parts of your configuration to avoid retyping an expression multiple times.

To learn more about when to use local values, refer to [Use locals to reuse expressions](/terraform/language/values/locals). To learn more about the `locals` block and its arguments, refer to the [`locals` block reference](/terraform/language/block/locals).

## Define outputs to expose module data

Outputs expose data from a module, making module values available in the Terraform CLI, HCP Terraform, and other parts of your configuration.

In the following example, the `instance_ip` output exports the private IP address of the `web` EC2 instance:

<CodeBlockConfig highlight="1-4" hideClipboard>

```hcl
output "instance_ip" {
description = "Private IP address of the EC2 instance"
value = aws_instance.web.private_ip
}

resource "aws_instance" "web" {
# …
}
```

</CodeBlockConfig>

To learn more about exposing data from modules and accessing those values, refer to [Use outputs to expose module data](/terraform/language/values/outputs). To learn more about the `output` block and its arguments, refer to the [`output` block reference](/terraform/language/block/output).
80 changes: 80 additions & 0 deletions content/terraform/v1.12.x/docs/language/values/locals.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
---
page_title: Use locals to reuse expressions
description: Learn how to use local values to assign names to expressions, letting you name and reuse expressions in your configuration.
---

# Use locals to reuse expressions

> **Hands-on:** Try the [Simplify Terraform Configuration with Locals](/terraform/tutorials/configuration-language/locals?utm_source=WEBSITE&utm_medium=WEB_IO&utm_offer=ARTICLE_PAGE&utm_content=DOCS) tutorial.

Local values are similar to function-scoped variables in other programming languages. Local values assign names to expressions, letting you use the name multiple times within a module instead of repeating that expression.

## Define locals

Add a `locals` block when you want to reuse an expression throughout your configuration. You can define the `locals` block in any module. The value you assign can be any valid Terraform expression and can reference the following:

- Variables
- Resource attributes
- Function outputs
- Other local values

For example, you could define a `locals` block to create a consistent naming convention, identify your primary subnet, and to set aside environmental configuration settings:

<CodeBlockConfig hideClipboard>

```hcl
locals {
# Naming convention
resource_name = "${var.project_name}-${var.environment}"

# Process the subnet list
primary_public_subnet = var.subnet_ids[0]
subnet_count = length(var.subnet_ids)

# Environmental deployment settings
is_production = var.environment == "prod"
monitoring_enabled = var.monitoring || local.is_production
}
```

</CodeBlockConfig>

Refer to the [`locals` block reference](/terraform/language/block/locals) for more information about configuring local variables.

## Reference local variables

Use the `local.<NAME>` syntax to reference values from a `locals` block in your configuration. The `locals` block defines local values, but you must use the singular `local` keyword to reference the individual values.

For example, in your configuration you can reference `local.resource_name` to name resources, and use `local.primary_public_subnet` with `local.monitoring_enabled` to configure a web server:

<CodeBlockConfig highlight="4,5,8,14,17" hideClipboard>

```hcl
resource "aws_instance" "web" {
ami = data.aws_ami.ubuntu.id
instance_type = var.instance_type
subnet_id = local.primary_public_subnet
monitoring = local.monitoring_enabled

tags = {
Name = local.resource_name
Environment = var.environment
}
}

resource "aws_security_group" "web" {
name = "${local.resource_name}-sg"

tags = {
Name = "${local.resource_name}-security-group"
}
}
```

</CodeBlockConfig>

You can access local values in the module where you define them, but not in other modules. However, you can pass a local value to a child module as an [argument](/terraform/language/modules/syntax#calling-a-child-module).

Local values help avoid repetition and give a meaningful name to the value of an expression. However, they can make configuration harder to read because they obscure where values originate. Use local values in situations where you either reuse a single value in many places to let you change a value in a single place or when the value is the result of a complex expression.

To learn more about the `locals` block, refer to the [`locals` block reference](/terraform/language/block/locals).
Loading
Loading