When creating an env0 Template using Terragrunt we should be mindful of how the state file is generated.
Background:
Typically, when using Terragrunt the backend configuration is generated dynamically like so:
remote_state {
backend = "s3"
config = {
bucket = "my-terraform-bucket"
region = "us-east-1"
key = "${path_relative_to_include()}/terraform.tfstate"
}
}
However, in env0, an env0 Template can be reused to create new env0 Environments. This means that the backend will collide when this template is used multiple times. To solve this we can simply modify how the key is generated.
remote_state {
backend = "s3"
config = {
bucket = "my-terraform-bucket"
region = "us-east-1"
key = "${join("/", [get_env("ENV0_ENVIRONMENT_ID"), path_relative_to_include()])}/terraform.tfstate"
}
}
In this example, we take advantage of env0's magic variable "ENV0_ENVIRONMENT_ID" and use that as part of the key to generate the backend.