Feature Request - resource self reference

A very common scenario in Terraform is defining one-off references that contain a lot of duplication, ie:

resource "aws_lambda_function" "test_lambda" {
  filename      = "test_lambda.zip"
  function_name = "test_lambda"
  ...
}

While a local or a for_each can help with deduplication, that adds indirection and verbosity. A simple solution that I often wish I had available would be the ability for resources to reference their own name, ie:

resource "aws_lambda_function" "test_lambda" {
  filename      = "${self.name}.zip"
  function_name = self.name
  ...
}

If functionality like this existed, it would decrease the potential for mistakes, increase readability, and make it faster to cut and paste resources that are similar, but not similar enough to do for_each. An object like self could also extended in the future for things like self.attributes.function_name, etc, but just the ability to reference a resource name would be huge.

Rob

4 Likes