Create dynamic expressions
The Terraform configuration language supports complex expressions to allow you to compute or generate values for your infrastructure configuration. Expressions can be simple string or integer values, or more complex values to make your configuration more dynamic.
In this tutorial, you will use expressions to configure and deploy EC2 instances and a load balancer. You will use conditionals to determine whether to provision multiple instances for high-availability and the splat
expression to return multiple private IPs in your network.
Prerequisites
You can complete this tutorial using the same workflow with either Terraform Community Edition or HCP Terraform. HCP Terraform is a platform that you can use to manage and execute your Terraform projects. It includes features like remote state and execution, structured plan output, workspace resource summaries, and more.
Select the HCP Terraform tab to complete this tutorial using HCP Terraform.
This tutorial assumes that you are familiar with the Terraform workflow. If you are new to Terraform, complete the Get Started tutorials first.
In order to complete this tutorial, you will need the following:
- Terraform v1.1+ installed locally.
- An AWS account with local credentials configured for use with Terraform.
Clone example repository
Clone the Learn Terraform Expressions repository, which contains configuration for AWS network components, an EC2 instance, and load balancer. You will modify the configuration of these resources using Terraform expressions.
Navigate to the repository in your terminal.
Use a conditional expression
Conditional expressions select a value based on whether the expression evaluates to true or false.
In this configuration, you will use the locals
block to create a resource name based on a conditional value and capture that name in a map of resource tags.
Open your main.tf
file and paste in the following code snippet.
The syntax of a conditional expression first defines the condition, then the outcomes for true and false evaluations. In this example, if var.name
is not empty (!= ""
), local.name
is set to the var.name
value; otherwise, the name is the random_id
.
Condition | ? | true value | : | false value |
---|---|---|---|---|
If the name variable is NOT empty | then | Assign the var.name value to the local value | else | Assign random_id.id.hex value to the local value |
Next, update the network resources of your configuration and add the local.common_tags
expression to your tags
attribute.
Note
Use local values with caution. While reusing a local value simplifies your configuration, it can add complexity to your resource lifecycle.
Update your ELB resource with the tags.
Finally, update your aws_instance
resource.
Save your changes.
Create a new file called outputs.tf
and add the values for your instance tags.
Create infrastructure
Initialize this configuration.
Run terraform apply
. Respond yes
to the prompt to confirm the operation.
Tip
This tutorial shows the output for Terraform commands run with the Terraform CLI. If you are following the HCP Terraform workflow, the output may differ slightly but the results will be the same.
If you use HCP Terraform to provision your resources, your workspace now displays the list of all of the resources it manages.
The Terraform outputs contain the formatted resource tags.
Create a conditional count criteria
Open variables.tf
and add a new boolean variable for high availability.
Save this file.
Next, open main.tf
and update the aws_instance
resource to use the new
high_availability
variable.
First, update the count parameter with a conditional expression based on the
value of var.high_availability
. Then update the associate_public_ip_address
parameter so that only the first instance is assigned a public IP address.
Finally, merge the tags for the new instances.
Condition | ? | true value | : | false value |
---|---|---|---|---|
If var.high_availability is set to true | then | Create three aws_instance resources | else | Create one aws_instance resource |
If count.index is 0 | then | Assign public IP | else | Do not assign public IP |
Save your changes.
Use a splat
expression
The aws_instance
resource could now have a count value of 3
. To return the
private IP addresses of all of the instances, you will use a splat *
expression to create an output value.
The splat
expression captures all objects in a list that share an attribute. The special *
symbol iterates over all of the elements of a given list and returns information based on the shared attribute you define.
Without the splat expression, Terraform would not be able to output the entire array of your instances and would only return the first item in the array.
Create a splat expression
Edit the outputs.tf
file to add the new private_addresses
output. This output will return the private DNS of all instances created by the aws_instance.ubuntu
resource.
This expression mirrors capturing a specific element in an array. If you only wanted to return the third instance IP in the array of instances, you could do that by replacing the *
with 2
.
The current tags
output will error because there are multiple instances.
Replace the tags
output block with the following first_tags
output, which will return the first instance's tags.
Open main.tf
to add the new instances to the ELB configuration.
Save your changes.
Apply your changes
Run terraform apply
to provision the new instances and update your load balancer configuration. Respond yes
to the prompt to confirm the operation.
Terraform's output now contains the entire array of private addresses for all three EC2 instances.
Clean up resources
After verifying that the resources were deployed successfully, run terraform destroy
to destroy them. Respond yes
to the confirmation prompt to confirm the action.
If you used HCP Terraform for this tutorial, after destroying your resources, delete the learn-terraform-expressions
workspace from your HCP Terraform organization.
Next steps
To learn more about how to create more complex, dynamic configuration, review the following resources:
- Learn how to use local values to simplify your configuration.
- Review how to use functions for computations in your configuration.
- Explore how to customize your configuration using input variables.