ksharawi
ksharawi

Reputation: 41

Terraform resource lifecycle destroy_after_create?

Is there an option in terraform configuration that would automatically destroy the resource after its dependents have been created? I am thinking something like destroy_after_create lifecycle which doesn't exist.

I want to delete all Lambda archives (s3 objects) after the Lambda Functions get created. Obviously I can create a script to run "terraform destroy -target" after "apply" completes, however I am looking for something within terraform configuration itself.

Upvotes: 1

Views: 981

Answers (1)

Meir Gabay
Meir Gabay

Reputation: 3286

To hack your way in Terraform, you can use the following combination:

Like this

resource "aws_s3_bucket" "my_bucket" {
  bucket = "my-bucket"
}

resource "null_resource" "delete_lambda" {
  depends_on = [
    aws_s3_bucket.my_bucket,
    # other dependencies ...
  ]
  provisioner "local-exec" {
    command = "aws s3api delete-object --bucket my-bucket --key lambda.zip"
  }
}

Upvotes: 1

Related Questions