Terraform 从地形云中获取变量
我正在尝试免费的计划 我已经在UI中设置了一些变量 现在,我想在我的地形代码中使用这些变量 首先,我设置为后端Terraform 从地形云中获取变量,terraform,Terraform,我正在尝试免费的计划 我已经在UI中设置了一些变量 现在,我想在我的地形代码中使用这些变量 首先,我设置为后端 terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 2.70" } } backend "remote" { organization = "m
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 2.70"
}
}
backend "remote" {
organization = "mycompany"
workspaces {
name = "DEV"
}
}
}
我也设置了一个数据源
data "terraform_remote_state" "mycompany" {
backend = "remote"
config = {
organization = "mycompany"
workspaces = {
name = "DEV"
}
}
}
现在我尝试使用这些变量
provider "aws" {
region = var.region
access_key = data.terraform_remote_state.mycomany.aws_access_key
secret_key = data.terraform_remote_state.mycomany.aws_secret_key
}
当我运行terraform应用程序时,我得到
Error: Unsupported attribute
on main.tf line 3, in provider "aws":
3: access_key = data.terraform_remote_state.mycomany.aws_access_key
This object has no argument, nested block, or exported attribute named
"aws_access_key"
我读了,还有一些文件
我不能相信这。。。(来自)
查找变量名
Terraform Cloud无法自动从工作区的Terraform代码中发现变量名称。您必须通过阅读代码或文档来发现必要的变量名,然后手动输入它们
如果缺少必需的输入变量,则工作区中的地形平面图将失败,并在日志中打印解释
所以,我的客户是
如何从地形云中同时使用法线和环境变量?我不需要数据源 我需要将变量(不带值)注册到variables.tf中
#############################
# FROM WORKSPACE
#############################
variable "aws_access_key" {}
variable "aws_secret_key" {}
现在,您可以像往常一样使用变量了
provider "aws" {
region = var.region
access_key = var.aws_access_key
secret_key = var.aws_secret_key
}
要在工作区之间更改(重用代码),我们可以使用前缀而不是名称更改远程配置。然后terraform CLI要求我们提供工作空间,或者您可以使用terraform workspace select进行选择
在这种情况下,我的工作区是mycompanydev和mycompanypro
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 2.70"
}
}
backend "remote" {
organization = "mycompany"
workspaces {
prefix = "mycompany-"
}
}
}