Terraform Lookup Function – Complete Guide with Examples

Learning how to use Terraform Lookup will boost your Terraform Skill. If you’re an engineer, chances are you’re familiar with Terraform. For those of you who aren’t, Terraform is a tool used to provision, manage, and deploy infrastructure resources. One of the neat things about Terraform is that it supports variables. Variables can be used to store values that can be retrieved and used later on in your Terraform configuration.

One type of variable that you can use in Terraform is a map variable. Map variables are variables that store key-value pairs. The values in a map variable can be retrieved by using the LOOKUP function. In this blog post, we’ll take a look at how the LOOKUP function works and how you can use it to retrieve values from map variables.

How to declare a map on Terraform: Declaring a map on Terraform is similar to declaring a regular variable. All you have to do is use the keyword ‘map’ when you declare the variable. Here’s an example:


variable "example_map" { 
    type = map 
    default = { 
        key1 = "value1" 
        key2 = "value2" 
    } 
} 

Once you’ve declared the map, you can use Terraform’s LOOKUP function to retrieve its values.

How Does the Terraform LOOKUP Function Work?

The LOOKUP function takes two arguments: a map variable and a key. The map variable is the name of the map variable that you want to retrieve a value from. The key is the key in the map variable that corresponds to the value that you want to retrieve.

Terraform Lookup – Examples

For example, let’s say that you have a map variable named “colors” that contains the following key-value pairs:

red => #FF0000

green => #00FF00

blue => #0000FF

If you wanted to retrieve the value associated with the “red” key, you would use the following syntax:

lookup({red="FF0000", green="00FF00"}, "red", "blue")

This would return the value “#FF0000”.

You can also use interpolation syntax to retrieve values from map variables. For example, you could use the following syntax to retrieve the same value: “${colors[“red”]}”.

Some limitations

The limitations: Terraform’s LOOKUP function can only retrieve values from map variables. It cannot be used to retrieve values from other types of Terraform variables, such as lists and scalars.

In addition, Terraform’s LOOKUP function has a limitation regarding retrieving nested values. For example: if you had a map with nested key-value pairs, Terraform’s LOOKUP function would not be able to retrieve the inner values.

By understanding how it works and its limitations, If the key you are trying to lookup corresponds to a nested value, Terraform will only return the first level of the nested value. It will not return to any deeper levels.

How to get AWS Available Zones using Map and Lookup:

To get the available aws zones using Terraform’s LOOKUP function, you would first need to define a map variable that contains all of the available AWS zones for your VPC or Subnet.


variable “aws_zones” { 

    type = map 

    default = { 

        us-east-1 = ["us-east-1a", "us-east-1b"] 

        us-west-2 = ["us-west-2a", “us-west-2b”] 

    } 
}

You can then use Terraform’s LOOKUP function to retrieve the list of available aws zones for a particular region. Let’s say you wanted to retrieve the list of available AWS zones for us-east-1. You would use the following syntax:

lookup(aws_zones, "us-east-1")

This would return the list of available AWS zones for us-east-1: [“us-east-1a”, “us-east-1b”].

You can also use Terraform’s LOOKUP function to retrieve a single value from the list. For example, if you wanted to retrieve just us-east-1a, you would use the following syntax:

lookup(aws_zones["us-east-1"], 0)

This would return the value “us-east-1a”.

How to use default value with Terraform Lookup:

You can also use Terraform’s LOOKUP function to provide default values if the key you are trying to lookup does not exist. Let’s say that you want to retrieve the value associated with a “blue” key. However, the “blue” key does not exist in the map variable you are trying to lookup. In this case, Terraform’s LOOKUP function can provide a default value using the following syntax:

lookup(colors, "blue", "default value")

This would return the default value, “default value”.

Terraform lookup with a condition:

You can also use Terraform’s LOOKUP function to provide different values based on a condition. This is useful when you have multiple values associated with a single key. For example, let’s say that you have a map variable named “colors” that contains the following key-value pairs:

blue => ["navy", "teal"]

If you wanted to retrieve the value associated with the “blue” key, but you wanted to retrieve the value based on a condition (e.g. if the first item in the list is navy, return it), you would use the following syntax:

lookup(colors["blue"], 0 == "navy", "navy", "teal")

This would return the value “navy”. If the first item in the list was not navy (e.g. it was teal), Terraform’s LOOKUP function would return the value “teal”.

Conclusion

The Terraform LOOKUP function is a handy way to retrieve values from map variables. By taking advantage of this function, you can simplify your Terraform configurations and make them more readable. Give it a try today!

Leave a Comment

Your email address will not be published. Required fields are marked *

Free PDF with a useful Mind Map that illustrates everything you should know about AWS VPC in a single view.