如何删除gitlab中的terraform资源?

how to delete terraform resources in gitlab?

我通过gitlab创建资源,但是无法删除。一开始我是把plan输出到artifacts里面去apply的。然后创建资源,创建后我想删除它们。但是删除它们是行不通的,不可能像计划的那样把它们输出到out中,如果我只是键入destroy,就像在终端中那样,作业是运行成功,但是0资源被删除。 My.gitlab-ci.yaml是这样的

stages:
  - validate
  - plan
  - apply
  - destroy

before_script:
  - rm -rf .terraform
  - export AWS_ACCESS_KEY_ID
  - export AWS_SECRET_ACCESS_KEY  
  - terraform init

validate:
  stage: validate
  script:    
    - terraform validate
  tags:
    - shell-runner

plan:
  stage: plan
  script:
    - terraform plan -out "planfile"
  dependencies:
    - validate
  artifacts:
    paths:      
      - "planfile"      
  tags:
    - shell-runner
      
apply:
  stage: apply
  script:
    - terraform apply -input=false -auto-approve   
  dependencies:
     - plan
  tags:
    - shell-runner
  when: manual

destroy:
  stage: destroy
  script:
    - terraform destroy -state="planfile" -auto-approve
  tags:
    - shell-runner
  when: manual

你在混淆Terraform state with Terraform plan

Terraform 状态是一个集中式文件,用于存储有关所有基础架构的信息。

Terraform 计划是仅包含计划更改的临时文件。

请正确设置Terraform state location,销毁时不要将计划文件设置为状态文件。您需要任何后端类型,但 local.

我找到了一种解决方案 + 可以通过 s3 存储桶完成 https://docs.gitlab.com/ee/user/infrastructure/iac/terraform_state.html