What if you could change hundreds of server names with just one simple line of code?
Why For expressions for transformation in Terraform? - Purpose & Use Cases
Imagine you have a list of server names and you need to create a new list where each name is changed to uppercase or has a prefix added. Doing this by hand means editing each name one by one in your configuration files.
Manually changing each item is slow and easy to mess up. If you have many servers, it's tiring and you might forget to update some names. This leads to errors and inconsistent setups.
For expressions let you transform each item in a list automatically. You write a simple rule once, and Terraform applies it to every item. This saves time and avoids mistakes.
server_names = ["web1", "web2", "web3"] uppercase_names = ["WEB1", "WEB2", "WEB3"] # manually typed
uppercase_names = [for name in server_names : upper(name)]
You can quickly and safely create new lists or maps by transforming existing data, making your infrastructure code cleaner and easier to manage.
When deploying multiple virtual machines, you can automatically generate their hostnames with a common prefix or suffix without typing each one manually.
Manual edits for each item are slow and error-prone.
For expressions automate transformations on lists or maps.
This leads to cleaner, safer, and more maintainable infrastructure code.