3

I am using terraform version 12.26 for AWS deployment. Whenever I try updating a lambda layer, the lambda layer version is incremented, but the older layer is deleted. Even though my current version of layer is for example 8, the older versions from 1 to 7 are automatically deleted.

I am using steps as mentioned here.

resource "aws_lambda_layer_version" "lambda_layer" {
  filename   = "lambda_layer_payload.zip"
  layer_name = "lambda_layer_name"
  source_code_hash    = filebase64sha256("lambda_layer_payload.zip")
  compatible_runtimes = ["nodejs12.x"]
}

How do I retain my older versions of layers?

2 Answers 2

3

Terraform's job is to maintain one remote object per resource block, either by updating that same object over time or by replacing it with a new object that meets new requirements.

If you wish to gradually accumulate new objects over time then Terraform is not the correct tool for the job. Your need would be better served by an application deployment tool, such as the Serverless framework.

1

You can tell terraform to dont care about older layer versions anymore.
This can be achieved by using the skip_destroy parameter and set it to true.
Keep in mind that layer versions are then intentionally dangling resources that are not managed anymore by terraform and may incur extra expense in your AWS account. See also the aws_lambda_layer_version documentation.

resource "aws_lambda_layer_version" "lambda_layer" {
  filename            = "lambda_layer_payload.zip"
  layer_name          = "lambda_layer_name"
  source_code_hash    = filebase64sha256("lambda_layer_payload.zip")
  skip_destroy        = true
  compatible_runtimes = ["nodejs12.x"]
}

As you reference a quite old terraform version in your question I want to point out that I'm currently referencing terraform version 1.3.9 and aws provider version 4.55.0 but I think the skip_destroy parameter is already a bit longer available.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.