Skip to main content
Welcome to this hands-on tutorial where you’ll explore key OpenTofu functions and conditional expressions. We’ll run various examples in the OpenTofu console to see how they transform values and control resource creation in Terraform configurations.

Table of Contents

  1. Floor Function
  2. Title Function
  3. Lookup Function
  4. Common Functions Overview
  5. Splitting a Colon-Separated String for IAM Users
  6. Querying a Resource by Index
  7. Finding the Index of a List Element
  8. Uploading Files to S3 with for_each
  9. Conditional Expressions in an EC2 Resource
  10. Links and References

1. Floor Function

Use the floor function to round a number down to the nearest integer. What does floor(10.9) return?
The image shows a coding lab interface with a question about the floor(10.9) function, offering multiple-choice answers. On the right, there's a Visual Studio Code editor with a welcome message and a terminal open.
Run in the console:
The floor function always returns an integer by discarding any fractional part.

2. Title Function

The title function capitalizes the first letter of each word in a string:

3. Lookup Function

Retrieve a value from a map by key, with a fallback default:
The first argument must be a map. If the key isn’t found, lookup returns the provided default.

4. Common Functions Overview


5. Splitting a Colon-Separated String for IAM Users

In your project directory:
variables.tf contains:
Without modifying variables.tf, split cloud_users into a list and create an IAM user for each name. Add this to main.tf:
Initialize and apply:
You should see seven IAM users created.

6. Querying a Resource by Index

To inspect a specific IAM user, launch the console:

7. Finding the Index of a List Element

Given this variable in variables.tf:
Locate the index of "rashid":
The image shows a split screen with a coding task on the left and a code editor on the right. The code editor displays a Terraform configuration file and a terminal output related to AWS IAM user creation.

8. Uploading Files to S3 with for_each

Your set of media paths is defined in var.media. An S3 bucket is declared in main.tf:
Add this resource to upload each file:
  • for_each = var.media iterates over each path.
  • key removes the leading /media/ (offset 7).
  • source points to the original file.
Apply the changes:
All files will be uploaded to your S3 bucket.
The image shows a coding environment with instructions on creating an S3 bucket using Terraform. The code editor displays a Terraform configuration file, and the terminal shows the output of a successful resource creation.
You can verify uploads by checking the S3 console or using aws s3 ls s3://<bucket-name>/.

9. Conditional Expressions in an EC2 Resource

Switch to the project-mario directory:
variables.tf includes:
Define an EC2 instance that chooses its size based on var.name:
Initialize and plan with -var:
This completes our walkthrough of OpenTofu functions and conditional expressions. Great job!
The image shows a coding environment with instructions to create an EC2 instance using Terraform, alongside a code editor displaying Terraform configuration files and a terminal with output messages.

Watch Video

Practice Lab