So I am working with Terraform and I feel like there must be a faster or, at the very least, a prettier way to perform the operation I'm attempting.

For some boilerplate, I have a data block that looks like the following:

data "aws_subnet_ids" "lambda_subnet_ids" {
  count  = length(var.lambda_subnet_tags)
  vpc_id = ...

From the results of this data block, I want to retrieve the first value from each set and combine it into another list. Here's how my team approached it before me. I wrapped it in a locals block to made it easier to see the use-case:

locals {
  lambda_subnet_ids = flatten(
      for subnets in data.aws_subnet_ids.lambda_subnet_ids :
          element(tolist(subnets.ids), 1)

Now, this works. It just looks awful. I feel like there has to be a nicer way to perform this same operation, but I am not familiar with HCL well enough to know what to look for.

Side note, this really should have the tags HCL and Terraform, but I do not have the reputation required to create them.



Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.