如何为 Terraform S3 后端资源传递变量? [英] How to pass variables for Terraform S3 Backend resource?

查看:21
本文介绍了如何为 Terraform S3 后端资源传递变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

上面的bucket和key不能通过变量文件来提供值吗?

Is it not possible to provide values for bucket and key above through variables file?

因为当我尝试这样做时:

Because when I try doing the same like this:

terraform {
  backend "s3" {
    bucket = var.bucket
    key    = var.key
  }
}

,我收到以下错误:

Error: Variables not allowed

on main.tf line 3, in terraform:
3:      bucket = var.bucket

Variables may not be used here.

Error: Variables not allowed

on main.tf line 4, in terraform:
4:      key = key

Variables may not be used here.

推荐答案

创建一个名为 backend.tfvars 的文件,内容为:

Create a file named backend.tfvars with content:

bucket = "mybucket"
key    = "path/to/my/key"

terraform 命令的命令行选项中指定此文件名:

Specify this file name in a command line option to the terraform command:

terraform init -backend-config=backend.tfvars

您需要使用单独的后端配置文件而不是通常的 tfvars 文件的原因是,在您设置后端时会使用这些值.这意味着它们需要在您运行 terraform init 时提供,而不是稍后在您将后端与 terraform apply 等命令一起使用时提供.

The reason you need to use a separate backend config file instead of your usual tfvars file is that these values are used when you set up your backend. That means they need to be provided when you run terraform init, not later when you use the backend with commands like terraform apply.

请参阅 部分配置 了解更多详情.

See the terraform documentation on partial configuration for more details.

这篇关于如何为 Terraform S3 后端资源传递变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆