terraform icon indicating copy to clipboard operation
terraform copied to clipboard

Allow variables in lifecycle block

Open jgardner04 opened this issue 4 years ago • 7 comments

Current Terraform Version

Terraform v0.12.28

Use-cases

I have a resource that gets deployed in a module. I want to allow the lifecycle of that resource to be determined by a variable provided to the module.

Attempted Solutions

This is the original attempt

lifecycle {
    prevent_destroy = var.hns_enabled == true ? true : false
  }

I also tried moving this up to a local and then using the local in the lifecycle block

locals {
  prevent_destroy = var.hns_enabled == true ? true : false
}

resource "" "" {
  lifecycle {
    prevent_destroy = local.prevent_destroy
  }
}

References

This is the same request #3116 from 2015 but that issues appears to have been left open and then closed from contribution from the public. The last activity was in 2018.

jgardner04 avatar Jul 09 '20 17:07 jgardner04

I would support this request as well. Would be good to get it working within a for_each

resource "azurerm_key_vault_secret" "secret" {
  for_each = try(var.keyvault.secrets, {})

  name            = each.value.name
  value           = try(each.value.is_value_filepath, false) ? base64encode(file(each.value.value)) : each.value.value
  key_vault_id    = azurerm_key_vault.keyvault.id
  content_type    = try(each.value.content_type, null)
  not_before_date = try(each.value.not_before_date, null)
  expiration_date = try(each.value.expiration_date, null)
  tags            = try(each.value.tags, null)

  lifecycle {
      ignore_changes = each.value.lifecycle.ignore_changes
  }
}

LaurentLesle avatar Sep 08 '20 13:09 LaurentLesle

A workaround for some use cases is to add an override.tf under certain circumstances that redefines the lifecycle block of the resource.

medains avatar Nov 22 '21 10:11 medains

image image image My variable is just a simple list, want to keep file short to read Could you please advice my approach? seems still not working

BinaryWorld2099 avatar Sep 20 '22 14:09 BinaryWorld2099

seems still not working

Yeah. The ticket is still open.

bby-bishopclark avatar Sep 20 '22 20:09 bby-bishopclark