'how to delete resources with terraform?

set up - a lambda project ( see file structure below) in terraform . in my gitlab-ci, in a build step , lambda code is zipped up at the root of the directory. everything works fine except, when i try to delete all resources in stg_destroy stage at the end , it complains that zip file is not there. how to run the destroy stage ?

foo
- tf
  - main.tf
  - backend.tf
  - output.tf
  - lambda
    - main.tf
    ...
- code
  - lambda
    - app.js

foo/tf/lambda/main.tf

resource "aws_lambda_function" "lambda" {
  filename                       = "lambda.zip"
  handler                        = "main.handler" 
  runtime                        = "python3.6" 
  source_code_hash               = source_code_hash = filebase64sha256(lambda.zip)
}

backend.tf

terraform {
  backend "s3" {
    bucket = "mybucket"
    key    = "path/to/my/key"
    region = "us-east-1"
  }
}

gitlab-ci

buildStage:
  stage: build
  script:
    - cd code/lambda zip ../../lambda.zip app.js
  

validateStage:
  stage: validate
  script:
    - echo "validation ..."

planStage:
  stage: plan
  script:
    - echo "Generating terraform plan and output to file 'plan' "
    - terraform plan --out plan
  artifacts:
    paths:
      - plan

applyStage:
  stage: apply
  script:
    - terraform apply --auto-approve plan
  artifacts:
    paths:
      - terraform.tfstate

destroyStage:
  stage: destroy
  script:
    - terraform destroy --auto-approve
  when: manual
  ...


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source