You are on page 1of 3

Variables

Inputs & Output

In Terraform, variables and outputs are used to parameterize your infrastructure as code,
making your configurations more flexible, reusable, and informative. They serve different
purposes in the Terraform workflow:
Variables:
1. Purpose:
• Variables are used to parameterize your Terraform configurations. They allow
you to define values that can be passed into your Terraform modules and
configurations. Variables are typically used for dynamic and configurable parts
of your infrastructure, such as resource names, instance counts, IP addresses,
and other values that can vary based on the environment or use case.
2. Declaration:
• You define variables using the variable block in your Terraform
configuration. You can specify details like the variable name, type,
description, and default value.
Example of variable declaration:

Usage:
• You can reference variables within your Terraform configuration using the var prefix.
For example, to use the instance_count variable:
Input Sources:
• Variables can be populated from various sources, such as command-line flags,
environment variables, or variable files. This makes it easy to customize your
configuration for different environments without modifying the code itself.
Outputs:
1. Purpose:
• Outputs are used to define values that should be made available for inspection
and use after running Terraform. Outputs are a way to expose information
about the infrastructure you've created, such as IP addresses, DNS names, or
other relevant details.
2. Declaration:
• You define outputs using the output block in your Terraform configuration.
You specify the name and value to be exposed.
Example of output declaration:

Usage:
• Once defined, outputs can be accessed using the output command or via the
Terraform state file. Outputs provide a way to retrieve critical information about the
resources you've created.
Example of accessing an output:
Documentation:
• Outputs also serve as a form of documentation for your Terraform
configuration, providing insight into the key values that are produced by your
infrastructure.

Variables and outputs are essential for creating flexible, reusable, and informative Terraform
configurations. They allow you to parameterize your infrastructure code, making it easier to
adapt to different environments, and provide a means to access and share critical information
about the resources you manage. Proper use of variables and outputs enhances the
maintainability and scalability of your Terraform projects.

You might also like