0
0
Terraformcloud~30 mins

Arguments and expressions in Terraform - Mini Project: Build & Apply

Choose your learning style9 modes available
Terraform Arguments and Expressions
📖 Scenario: You are setting up a simple cloud infrastructure using Terraform. You want to create a virtual machine with specific settings. To do this, you will use arguments and expressions in Terraform configuration files.
🎯 Goal: Build a Terraform configuration that defines a virtual machine resource with arguments and uses expressions to set values dynamically.
📋 What You'll Learn
Create a variable for the VM name
Define a resource block for a virtual machine
Use expressions to set the VM size based on a condition
Add a tag to the VM using an expression
💡 Why This Matters
🌍 Real World
Terraform is widely used to automate cloud infrastructure setup. Using arguments and expressions lets you create flexible and reusable configurations.
💼 Career
Understanding Terraform arguments and expressions is essential for cloud engineers and DevOps professionals to write efficient infrastructure as code.
Progress0 / 4 steps
1
Create a variable for the VM name
Create a Terraform variable called vm_name with the default value "my-vm".
Terraform
Need a hint?

Use the variable block with a default argument.

2
Define a resource block for a virtual machine
Add a resource block called azurerm_linux_virtual_machine named example. Set the name argument to use the variable vm_name.
Terraform
Need a hint?

Use resource "azurerm_linux_virtual_machine" "example" {} and set name = var.vm_name.

3
Use expressions to set the VM size based on a condition
Inside the azurerm_linux_virtual_machine.example resource, add an argument size that uses a conditional expression. If var.vm_name is equal to "my-vm", set size to "Standard_B1s", otherwise set it to "Standard_B2s".
Terraform
Need a hint?

Use the ternary operator: condition ? true_value : false_value.

4
Add a tag to the VM using an expression
Inside the azurerm_linux_virtual_machine.example resource, add a tags argument. Set the tag Environment to "Production" if var.vm_name contains the substring "prod", otherwise set it to "Development". Use the strcontains() function in the expression.
Terraform
Need a hint?

Use tags = { Environment = strcontains(var.vm_name, "prod") ? "Production" : "Development" }.