-
Notifications
You must be signed in to change notification settings - Fork 105
Splitting up the "Set configuration parameters" page #1170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
rkoron007
wants to merge
10
commits into
main
Choose a base branch
from
RK/separate-variables-page
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+400
−262
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
4959050
First try on splitting the pages adding to 1.12 for now
rkoron007 9f2efe7
Copy paste typo
rkoron007 acfffa3
Fix links in 1.12
rkoron007 36afc52
small tweaks
rkoron007 27c3904
small tweak for article title
rkoron007 c21dc9d
Add Judith feedback and handle redirects
rkoron007 084c3bd
Merge branch 'main' into RK/separate-variables-page
rkoron007 846c808
Tweak intro
rkoron007 03e331d
Adam feedback
rkoron007 f54a7f7
Fix the intro if variable
rkoron007 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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> | ||
|
|
||
rkoron007 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 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). | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.