azure-devopsterraformazure-pipelinesterragrunt

Wrong folders structure in Terragrunt s3 backend during the pipeline


I have a Terragrunt project in the following structure:

├── dev
│   ├── modules
│   │   ├── _backend.tf
│   │   ├── acm
│   │   ├── eks
│   │   ├── eks-resources
│   │   │   ├── efs
│   │   │   ├── irsa
│   │   │   ├── node-roles
│   │   │   ├── node-roles-win
│   │   │   ├── nodegroups
│   │   │   ├── routes
│   │   │   ├── storageclases
│   │   │   ├── vpc-endpoints
│   │   │   ├── vpc-endpoints-sg
│   │   ├── eks-vpc
│   │   ├── main_provider.tf
│   │   ├── route53
│   │   └── terragrunt.hcl
│   └── terragrunt.hcl
├── dev2
└── dev3

In dev2 and dev3 has the same folders.

My backend config is:

remote_state {
  backend = "s3"
  generate = {
    path      = "_backend.tf"
    if_exists = "overwrite"
  }
  config = {
    bucket  = "tfstate-${local.aws_region.locals.aws_region}-${local.account.locals.aws_account_name}-${local.account.locals.aws_account_id}"
    key     = "${path_relative_to_include()}/${local.env_common.locals.env_name}/terraform.tfstate"
  }
}

When I run terragrunt run-all plan/apply --terragrunt-include-external-dependencies --terragrunt-non-interactive from my local pc in the folder terragrunt/development/us-east-1/environments/dev it's working well, it goes to the s3 bucket, in the bucket I have acm, eks, etc. folders (like the in the folders structure), in each folder I have 3 folders: dev, dev2 & dev3, and inside each folder there is terraform.tfstate.

But, when I run the same command from Azure Pipelines, it's creating a new folder env: and there all the folders above with the dev/dev2/dev3 which cause a new completely state.

For example, in local I have the folder eks in my bucket's root:enter image description here

But in the pipeline it's created this:enter image description here->enter image description here->enter image description here

enter image description here

I verified I run it from the same folder as local: enter image description here

Do you have any idea why? or a way to debug it?


Solution

  • This is probably because you have the TF_WORKSPACE env var setup somehow in your pipeline. from documentation:

    https://developer.hashicorp.com/terraform/language/settings/backends/s3#workspace_key_prefix

    if you dont specify anything and use workspace vars, then it just creates everything under "env:/"

    EDIT: For all those using terragrunt, remember it wraps around Terraform, hence it also relies on its functionality and ENV_vars a lot of the times you end up looking at terraform rather than terragrunt :)

    hope it helps your issue :)