gruntwork-io / terragrunt

Terragrunt is a flexible orchestration tool that allows Infrastructure as Code written in OpenTofu/Terraform to scale.
https://terragrunt.gruntwork.io/
MIT License
8.04k stars 975 forks source link

Feature request: Generate file, overwrite if Terragrunt signature found, otherwise do nothing #1181

Open anlutro opened 4 years ago

anlutro commented 4 years ago

Currently there is the if_exists = "overwrite_terragrunt" option, which according to documentation "overwrites the existing file if it was generated by terragrunt; otherwise, error"

However, it would be nice to have another option which overwrites the existing file if it was generated by Terragrunt, otherwise do nothing, instead of erroring.

An example use case for me would be provider configuration - most of our Terraform configuration should run on the latest version of the provider, but there are exceptions which are affected by bugs, which we'd like to keep at an older version.

We can kind of have this working with if_exists = "skip", but it means we have to manually delete the generated file every time we want to upgrade the provider.

brikis98 commented 4 years ago

An example use case for me would be provider configuration - most of our Terraform configuration should run on the latest version of the provider, but there are exceptions which are affected by bugs, which we'd like to keep at an older version.

Trying to understand the use case more... For these exceptions, are you manually creating a provider.tf file that's checked into version control... But normally, no such file exists... So you want Terragrunt to create (and overwrite?) the file if it was originally written by Terragrunt, but otherwise, do nothing?

anlutro commented 4 years ago

Given the exame given in the Terragrunt documentation:

generate "provider" {
  path      = "provider.tf"
  if_exists = "overwrite"
  contents = <<EOF
provider "aws" {
  region              = "us-east-1"
  version             = "= 2.3.1"
  allowed_account_ids = ["1234567890"]
}
EOF
}

This'll generate terraform.tf with a Terragrunt signature. If I change the "contents" variable by upgrading to 2.5.0 for example, Terragrunt will update the provider.tf in all my directories next time I run terragrunt apply.

But let's say one of my directories is affected by a bug so I can't upgrade to 2.3.1. In that case I'd like to remove the Terragrunt signature from the file, downgrade to 2.3.1, and then Terragrunt won't replace the provider.tf file next time I run a command.

When the bug is fixed (maybe in 2.5.1) I'd be happy to remove the file and let Terragrunt generate it again.

brikis98 commented 4 years ago

I gotcha, thanks for the explanation. Would you be up for a PR to add a new if_exists setting to handle this use case?

anlutro commented 4 years ago

Sure thing, I can probably spend some time on it this week.

Any ideas on what the new setting should be called? Something like soft_overwrite comes to mind but I'm really not sure.

brikis98 commented 4 years ago

Thanks! I'm terrible at naming things. I'd probably pick something super verbose like overwrite_terragrunt_else_ignore, but feel free to toss out (hopefully better) ideas!

artemsablin commented 4 years ago

Would this help?

askainet commented 4 years ago

Would this help?

It helps indeed!

I'm using it to ignore the EKS managed tags from my VPC component in a provider-aws_override.tf file:

provider "aws" {
  ignore_tags {
    key_prefixes = ["kubernetes.io/cluster/"]
  }
}
gitPlatformTests commented 11 months ago

Was this implemented or about to be? I find it useful and the override feature of tf is not enough