You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行terraform apply预生成计划时,是否需含Backend的完整初始化?

Terraform Init -backend=false 与离线Plan/Apply行为解析

问题背景

查看GitHub Actions工作流时发现,仅执行terraform init -backend=false而非完整初始化的情况下,terraform apply terraform.tfplan仍能正常运行。结合对terraform init/plan命令的研究以及解压tfplan文件的发现,有以下疑问:

  1. -backend=false选项的具体含义是什么?是否要求配置已执行过完整init?为何要再次运行该命令?
  2. tfplan文件包含.terraform.lock.hcl、S3后端配置等内容,但官方文档未提及这一特性,是否应该避免依赖此行为?

核心解答

1. -backend=false的作用与使用原因

禁用此配置的后端或Terraform Cloud初始化,改用之前已初始化的内容。

这个选项的本质是跳过后端初始化步骤,同时告知Terraform:当前工作目录已存在有效的初始化状态(比如.terraform目录的插件、模块缓存,或其他方式提供的依赖)。在GitHub Actions场景中:

  • terraform-plan阶段已完成完整init(含下载插件、配置后端),并生成了包含所有依赖信息的terraform.tfplan
  • terraform-apply阶段用-backend=false,是为了避免重复配置后端、下载插件——tfplan文件已包含执行apply所需的所有关键信息,且工作流手动上传了.terraform.lock.hcl保证依赖一致性
  • 运行terraform init -backend=false的核心目的是让Terraform初始化本地工作目录的基础结构(如创建必要缓存目录、加载锁文件),无需再次连接后端服务或下载依赖

2. tfplan文件的隐式内容与依赖风险

从解压结果可见,terraform.tfplan是压缩包,包含tfstate、配置文件、锁文件等内容。这是Terraform的内部实现细节,而非官方承诺的稳定特性:

  • Terraform设计-out参数生成计划文件,初衷就是保证plan和apply的环境一致性,因此会打包必要依赖信息
  • 但官方文档未明确说明这一点,意味着未来版本可能调整文件结构或内容
  • 若依赖该隐式行为,一旦Terraform版本更新导致tfplan格式变化,工作流可能直接失效

3. 是否应该依赖此行为?

建议尽量遵循官方文档的推荐流程,但当前工作流的做法在特定场景下是可行的:

  • 若想保持工作流稳定性,更稳妥的方式是在apply阶段执行完整init(与plan阶段用相同后端配置),同时上传/下载.terraform.lock.hcl锁定依赖版本
  • 若继续使用当前方式,需固定Terraform版本(如工作流中指定的1.5.7),避免版本升级带来的兼容性问题
  • 另外,工作流已手动上传.terraform.lock.hcl,进一步保证了依赖一致性,降低了风险

附:GitHub Actions工作流原文

....

  terraform-plan:
    name: terraform-plan
    runs-on: ubuntu-latest
    timeout-minutes: 30
    defaults:
      run:
        working-directory: terraform
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-region: eu-west-1
          role-to-assume: arn:aws:iam::729187411107:role/github-oidc-role
          role-duration-seconds: 3600
          role-session-name: github-${{ github.sha }}
      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v3
        with:
          terraform_version: 1.5.7
      - name: Terraform Init
        run: |
          terraform init \
          -backend-config="bucket=terraform-approve-before-apply-tfstate" \
          -backend-config="key=terraform.tfstate" \
          -backend-config="region=eu-west-1"
      - name: Terraform Plan
        run: terraform plan -out=terraform.tfplan
      - name: Upload Terraform Plan
        uses: actions/upload-artifact@v4
        with:
          name: tfplan
          path: |
            terraform/terraform.tfplan
            terraform/.terraform.lock.hcl
          if-no-files-found: error
    needs: terraform-validate

  terraform-apply:
    name: terraform-apply
    runs-on: ubuntu-latest
    timeout-minutes: 30
    environment: deploy
    defaults:
      run:
        working-directory: terraform
    steps:
      - name: Checkout
        uses: actions/checkout@v4
        with:
          submodules: recursive
      - name: Configure AWS credentials
        uses: aws-actions/configure-aws-credentials@v4
        with:
          aws-region: eu-west-1
          role-to-assume: arn:aws:iam::729187411107:role/github-oidc-role
          role-duration-seconds: 3600
          role-session-name: github-${{ github.sha }}
      - name: Setup Terraform
        uses: hashicorp/setup-terraform@v3
        with:
          terraform_version: 1.5.7
      - name: Terraform Init
        run: terraform init -backend=false
      - name: Download Terraform Plan
        uses: actions/download-artifact@v4
        with:
          name: tfplan
          path: terraform
      - name: Terraform Apply
        run: terraform apply terraform.tfplan
    needs: terraform-plan

内容的提问来源于stack exchange,提问作者Shuzheng

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.26 07:17:10