"Plan: 1 to add, 0 to change, 1 to destroy." : I want Terraform only to keep adding and destroy nothing

Hello,

While executing ‘plan/apply’ in Terraform, I don’t want Terraform to destroy any existing resource while applying.

for example; We will get the below message while executing Terraform plan/apply. I want Terraform only to keep adding and destroy nothing.

“Plan: 1 to add, 0 to change, 1 to destroy.”

Could you please give me some information in this regards.

Thank you,
Arshed Hussain

Sorry, but you only select resources to apply changes using -target argument, and only this can filter the resource.

I checked the last version of the documentation, and this not changed yet.

Apply process with objective maintain state is core part of philosophy of the terraform, and i think if there necessary this usage, you have other problem in your use case. If you explain a use case more, i will help you better.

Without seeing what you modified, its hard to tell.

Basically, what is in your terraform files is what terraform will try and do. So if you want terraform to add and not delete, then you need to add to the file. If you modify something, it MAY need to be deleted and created.

For example, if I have :

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"
}

and I want to add, I simply add a resource to the TF file :

resource "aws_instance" "web" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"
}
resource "aws_instance" "web-1" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"
}

Terraform will look at the desired state or “resources in the file” ( web and web-1) compare it to the current state ( web ), and plan the actions to bring the current state to the desired state ( create web-1 ). Each resources has its own “rules” on when to update and when to delete/create. One jkey element often overlooked is the name of the resource is the “index in the state file”.

So if I decide to make things consistent, and change my file to :

resource "aws_instance" "web-0" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"
}
resource "aws_instance" "web-1" {
  ami           = data.aws_ami.ubuntu.id
  instance_type = "t3.micro"
}

The desired state is now - web-0 and web-1, but the current state is - web and web-1. So “web” will be deleted and “web-0” will be created.

1 Like

@claytonsilva @alain

Thank you very much for responding me. Ill present my scenario. I wrote the below code to create ‘Lambda layer’ only. The Lambda layer has this option of versioning, for example we create a lambda layer called ‘xyz’ with runtime python3.8 and it will be created as ‘version 1’. I can update the runtime to python3.9 for the same Lambda layer ‘xyz’ and it becomes ‘version 2’. In AWS Lambda layer I get to see both the version 1 and 2, depending on my test case I have the option to call any of the layers in my Lambda Function.

The issue I am facing is with the below terraform code, the existing layer is getting destroyed. In the above example terraform is destroying version 1 and creating version 2.

#######################

File 1 : pytz.zip

#######################

module “jfrog_download” {

source = “terraform.prd.xxxxxxx.net/xxxxxxx/artifact/xxxxxx
version = “0.3.4”
#version = “1.0.0”
common = local.common
repository = “XXXX-YYYYYY-TEST-DEV”
artifact = “xxxxxxx/pytz.zip”
extract = false
}

resource “aws_lambda_layer_version” “lambda_layer” {
filename = module.jfrog_download.file
layer_name = “terra_code_layer_test_pytz”
compatible_runtimes = [“python3.9”]
/*
lifecycle {
prevent_destroy = true
create_before_destroy = true
}
*/
}

If I run the above code I am getting this status “Plan: 1 to add, 0 to change, 1 to destroy.”. I am finding a way to overcome this problem.

There is a big note about setting skip_destroy to true on the resource documentation for the provider.

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_layer_version

As well they describe what changes trigger the provider to recreate the resource ( 1 add, 1 destroy) - basically what you are describing.

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_layer_version#skip_destroy

Hope this helps.

1 Like

@alain

Thank you very much. It got my issues resolved, really appreciate your assistance.

Thank you,
Arshed

2 Likes