OVERVIEW OF THE TERRAFORM LOOKUP FUNCTION:

Terraform’s lookup function allows you to retrieve values from a map or list based on a specified key. This function provides a convenient way to access and use data within your Terraform configuration. By using the lookup function, you can dynamically retrieve values and use them to define resources or configuration settings. 

USING THE LOOKUP FUNCTION PROVIDES THE FOLLOWING BENEFITS:

  • Dynamic Data Retrieval: The lookup function enables you to fetch values from maps or lists dynamically based on a key or index. This allows you to reference and use data in a flexible manner.
  • Configuration Flexibility: By retrieving data with the lookup function, you can parameterize your configurations and make them more flexible. This makes it easier to manage and modify your infrastructure based on varying requirements.
  • Error Handling: The lookup function allows you to handle missing or null values by specifying a default value. This helps prevent errors and ensures smooth execution of your configuration.

EXAMPLE USAGE:

Let’s consider an example where you want to create multiple AWS S3 buckets using the lookup function to retrieve their names from a map. Here’s how you can accomplish this: 

1. Defining a Map:

Create a map that holds the names of the S3 buckets you want to create, along with any associated configuration. For example: 

variable “s3_buckets” {
  type = map(object({
    region = string
    acl    = string
  }))
 
  default = {
    bucket1 = {
      region = us-west-2
      acl    = private
    }
 
    bucket2 = {
      region = eu-west-1
      acl    = public-read
    }
  }
}
 

This variable defines a map with two S3 buckets, each containing a region and an access control list (ACL) setting. 

2. Using the lookup function:

Within your resource definition, utilize the lookup function to retrieve values from the s3_buckets variable. For example: 

resource “aws_s3_bucket” “s3_bucket” {
  for_each = var.s3_buckets
  bucket = each.key
  region = lookup(each.value, region, null)
  acl    = lookup(each.value, acl, null)
  # other S3 bucket configuration…
}

 

This configuration uses the lookup function to retrieve the region and ACL values from the s3_buckets variable. The function looks for the specified keys within each item’s value and provides a default value of null if the key is not found. 

3. Applying Changes:

To apply the changes and create the S3 buckets defined in the configuration, run the terraform apply command: 

$ terraform apply

 

This command applies the changes defined in the configuration, creating multiple S3 buckets based on the values provided in the s3_buckets variable. 

4. Modifying Bucket Names:

To modify the name of a specific bucket, update the corresponding key in the s3_buckets variable. For example, to change the name of “bucket1” to “new_bucket1”: 

variable “s3_buckets” {
  type = map(object({
    region = string
    acl    = string
  }))
 
  default = {
    new_bucket1 = {
      region = us-west-2
      acl    = private
    }
    bucket2 = {
      region = eu-west-1
      acl    = public-read
    }
  }
}

 

FINAL THOUGHTS:

By utilizing lookups, your organization can dynamically retrieve values and use them in your Terraform configuration. This gives your infrastructure as code additional flexibility if required.

We’ve helped a variety of organizations get started the right way on their journey into infrastructure as code. If you’re just getting started on your journey into infrastructure as code, start the right way, follow this link to get in contact with one of our experts.

For more technical how-to’s click here.