terraform icon indicating copy to clipboard operation
terraform copied to clipboard

Add support for lifecycle meta-argument in modules

Open elliott-weston-cko opened this issue 4 years ago • 81 comments

Current Terraform Version

Terraform v0.14.3

Use-cases

Terraform currently only allows the lifecycle meta-argument to be used within the declaration of a resource. It would be really useful if users were able to specify lifecycle blocks in modules that can then be applicable to some/all of the resources within that module.

The main use-case I have is being able to use the ignore_changes to instruct terraform to ignore changes to resources or particular attributes of resources.

Proposal

For example, lets assume I create a terraform module to be used in AWS, and as part of that module I create a dynamodb table. DynamoDB tables (among other resources) have the ability to autoscale, the autoscaling configuration is defined by another resource. Consequently, a lifecycle block must be used to prevent the resource that creates the dynamodb table from modifying the read/write capacity.

In this scenario I currently have to choose to either to support autoscaling or to not support autoscaling, as I cannot pass define a lifecycle block with the ignore_changes argument. Ideally, I'd like to be able to do something like this:

module "my-module" {
  source = "./my-module/"
  name = "foo-service"

  hash_key = "FooID"
  attributes = [
    {
      name = "FooID"
      type = "S"
    }
  ]
  lifecycle {
    ignore_changes = [
      aws_dynamodb_table.table.read_capacity,
      aws_dynamodb_table.table.write_capacity
    ]
  }
}

Being able to apply lifecycle blocks similarly to the way shown above, would enable me to manage the attributes of this resource outside of this module (whether that's via some automated process, or another resource/module definition), and would allow more people to use this module as it would be usable for a wider range of use-cases.

The documentation states that the lifecycle block can only support literal values, I'm unsure if my proposal would fall under that, as its referring to resources (and possibly attributes) that are created within the module itself 🤔

References

  • https://github.com/hashicorp/terraform/issues/4149 (Possibly)

elliott-weston-cko avatar Dec 24 '20 09:12 elliott-weston-cko

I am also interested by such feature though it would be to use a prevent_destroy lifecycle directive.

jleloup avatar Feb 24 '21 15:02 jleloup

It will be very useful with AWD RDS module.

module "db" {
  source = "terraform-aws-modules/rds/aws"
  ...
  snapshot_identifier = "..."
  password = "..."
  
  lifecycle {
    ignore_changes = [
      snapshot_identifier,
      password
    ]
  }
  ...
 }

jaceklabuda avatar Mar 09 '21 12:03 jaceklabuda

This would be a great feature, especially now that Terraform Modules support for_each

ibacalu avatar Apr 15 '21 06:04 ibacalu

My main use case is prevent_destroy on DDB and S3, both persistent end-user data that I want to preserve against the accidental replacement of objects

rjcoelho avatar Apr 16 '21 20:04 rjcoelho

Good addition as more and more people are starting to use modules like resources so being able to use the lifecycle block on the module level would be amazing

Shocktrooper avatar May 03 '21 04:05 Shocktrooper

It feels like having lifecycle blocks support dynamic configuration in general would be better than adding support for lifecycle blocks I modules. It would mean modules wouldn't need special support for this, and instead vars and custom logic could be used to set different lifecycle options on resources inside the module (ensuring you can encapsulate the logic, which the approach suggested in this ticket doesn't allow for).

chancez avatar May 05 '21 14:05 chancez

Hi @antonbabenko, any possibility considering below request? https://github.com/hashicorp/terraform/issues/28913

nitmatgeo avatar Jun 09 '21 13:06 nitmatgeo

This would also be incredibly helpful for preventing things from ever being part of a destroy

jbcom avatar Jun 17 '21 22:06 jbcom

Please add this functionality. Modules are severely limited if you can't use lifecycle metadata when calling them.

ChristianPresley avatar Jun 18 '21 15:06 ChristianPresley

This will be a very useful feature instead of editing the module definition to support lifecycle ignores

rumeshbandara avatar Aug 16 '21 12:08 rumeshbandara

I also ran into issues with this using aminueza/terraform-provider-minio

openPablo avatar Aug 20 '21 10:08 openPablo

+1 was really shocked to discover this isn't a module level thing.

DevOpsJon avatar Aug 27 '21 02:08 DevOpsJon

+1 super key feature for resources like KMS

movergan avatar Aug 31 '21 11:08 movergan

+1, absolutely necessary feature, especially to prevent deletion of certain resources, as others have mentioned above.

devpikachu avatar Aug 31 '21 16:08 devpikachu

+1 just ran into this and also shocked it's not here. If I had the time, I'd see about contributing this change. My use case is just like @jaceklabuda has but for the engine_version since I have auto update on.

module "rds" {
  source = "terraform-aws-modules/rds/aws"
  ...
  engine_version = "5.7.33"
  
  lifecycle {
    ignore_changes = [
      engine_version
    ]
  }
  ...
 }

OGProgrammer avatar Sep 01 '21 00:09 OGProgrammer

@OGProgrammer You can set engine_version = "5.7" instead of "5.7.33" in the RDS module you are using. This will prevent it from showing a diff every time the patch version is updated. aws_db_instance docs for engine_version

antonbabenko avatar Sep 01 '21 09:09 antonbabenko

Just sharing my experience here, in case it helps :) if you do not set the complete version (major + minor and patch number), AWS always offers the latest patch number. That means, if the version is set to 5.7 and at the time of deployment, latest version offered by aws is 5.7.30 and that is installed, the next time you deploy the same package and if the AWS offering is 5.7.35 (like new patches published), Terraform will show a diff and and applying the changes usually leads to an outage, unless you have set scheduled maintenance windows (which prevents patch upgrades). So, also, I think setting exact versions are better than ignoring them via lifecycle block, because it can make troubleshooting easier. It is best that patch versions used in the code are updated on a regular maintenance periods.

ghost avatar Sep 01 '21 23:09 ghost

A barebones implementation of the prevent_destroy for modules should prevent destruction of the module itself (via a terraform destroy command), not destruction of resources inside it.

Additional work to allow resource specific lifecycles within the module, or to prevent all resources in the module from being destroyed would be nice as well, but I don't see them as immediately essential.

aidan-mundy avatar Sep 30 '21 21:09 aidan-mundy

In case it helps: This would also be helpful for blue/green deployments where there's a 50% chance of the primary listener having its default_action updated with the wrong target group (in the case of having two TGs). Namely in the terraform-aws-modules/alb/aws module. Using the module beats having to manage several different TF resources.

BHSDuncan avatar Oct 29 '21 15:10 BHSDuncan

For anyone who encounters this issue and wants to protect module resources, we were able to find a bit of a hacky but workable solution within a wrapper module using:

resource "null_resource" "prevent_destroy" {
  count = var.prevent_destroy ? 1 : 0

  depends_on = [
    module.s3_bucket ## this is the official aws s3 module
  ]

  triggers = {
    bucket_id = module.s3_bucket.s3_bucket_id
  }

  lifecycle {
    prevent_destroy = true
  }
}

So far it seems to be a 1 way flag which can't be turned off but works well to protect buckets where content recovery would be a lengthy & disruptive task.

stephenh1991 avatar Nov 02 '21 10:11 stephenh1991

We also could really do with this feature. We have a reasonably extensive library of terraform modules wrapping everything from EC2 instances to application stacks. Taking the EC2 module as an example we use a data source like the example from the docs to supply a "latest" ami at build time

data "aws_ami" "example" {
  most_recent = true

  owners = ["self"]
  tags = {
    Name   = "app-server"
    Tested = "true"
  }
}

Most infrastructure is immutable so a later AMI results in a recreation of any EC2 instances sourced from the module, but some infra we'd like to use ignore_changes for the AMI like you might with a resource. This proposal would make achieving that much easier.

nlitchfield avatar Nov 03 '21 09:11 nlitchfield

I want to parametrize the create_before_destroy value of an EC2 instance in a module. Modules for different use cases will have different create_before_destroy behavior.

speller avatar Dec 13 '21 09:12 speller

I currently stand up Azure infrastructure, namely vnets and subnets through a module. We have custom route tables that have Next Hop Ip set. With deployed NVA's like fortigates and F5's they change the next hop IP dependant on which device is currently active. When a pipeline is run it sees the changes and reverts. Not every scenario is like this and would rather place a lifecycle in the module pull for a specific repo rather than in the module for all.

boopzz avatar Apr 07 '22 10:04 boopzz

I just ran into this with a custom module that we're using to wrap AWS. We see a lot of noise around secret values, and it'd be nice to tell it not to worry about it.

reify-tanner-stirrat avatar May 04 '22 20:05 reify-tanner-stirrat

Has there been any movement or thought about this from the devs? I still can't find a workaround for the primary listener of an ALB having its default_action updated in-place with the wrong target group (in the case of having two TGs), using one of the AWS TF modules. (If anyone knows of a workaround I'd be very interested--I don't think preventing destruction works, though.)

BHSDuncan avatar Jun 03 '22 15:06 BHSDuncan

Hmm, I had to use some kind of workaround in order ec2 instances would not be replaced by dynamically provided AMI id, but I don't like it, the use of modules is very spread and lifecycle is pretty basic, I tried to use some public domain modules (terraform-aws-modules by @antonbabenko - Kudos for his hard work) but it seems it lacks some basics which we use on-premise, surely in our modules we use lifecycle inside a resource in a module and it is not provided as an argument to a module. I would like to see ignore_changes be provided dynamically through the module.

mldevpants avatar Jun 19 '22 09:06 mldevpants

It has been a while since this was opened, is there any workaround or ETA on getting this? My usecase is to be able to ignore desired_size config of eks managed node group scaling config so that it does not tear down my nodes as per desired size whenever there's an update to the infra config. While I can do it through https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eks_node_group#ignoring-changes-to-desired-size, there is still no way of doing this if am using aws module to create eks cluster.

khushboomittal34 avatar Jul 18 '22 08:07 khushboomittal34

Any news?

thiagolsfortunato avatar Jul 28 '22 12:07 thiagolsfortunato

It is necessary to extend the universal use of the modules.

gnokoheat avatar Aug 01 '22 14:08 gnokoheat

+1 this would make life so much better

zamialloy avatar Aug 05 '22 19:08 zamialloy