How can I read environment variables from a .env file into a terraform script?

This is a pure TerraForm-only solution that parses the .env file into a native TerraForm map.

output "foo" {
  value = { for tuple in regexall("(.*?)=(.*)", file("/path/to/.env")) : tuple[0] => tuple[1] }
}

I have defined it as an output to do quick tests via CLI but you can always define it as a local or directly on an argument.

Edited to allow env variables to contain =

Leave a Comment