gpt4 book ai didi

Terraform - 如何将 tfvars 与模块一起使用

转载 作者:行者123 更新时间:2023-12-04 20:30:57 24 4
gpt4 key购买 nike

对于三种不同的环境,我希望能够根据环境动态设置变量。在下面的示例中,假设实例类型在 dev 和 prod 之间不同。我无法引用 instance_type在模块内,除非我有 vars.tf在我的 terraform.tfvars 旁边存档.

我得到的错误是:
unknown variable referenced: 'instance_type'. define it with 'variable' blocks
如果是这种情况,那么这个文件不会是 modules/apollo/vars.tf 下的完全相同的文件吗? ?

我以为 modules/apollo/vars.tf定义模块所需的必要变量。我认为在 env-dev/services/apollo/ 下的“根”级别内没有必要这样做.如果有这样做的“更好”的方式,我全神贯注。

├── env-dev
│   └── services
│   └── apollo
│   ├── main.tf
│   ├── terraform.tfvars
│   └── vars.tf # Do i need this?
├── env-test
├── global
├── mgmt
└── modules
├── apollo
│   ├── main.tf
│   ├── user_data.tpl
│   └── vars.tf
└── defaults
└── main.tf

env-dev/services/apollo/terraform.tfvars
instance_type    = "t2.medium"

env-prod/services/apollo/terraform.tfvars
instance_type    = "t2.large"

模块/ Apollo /vars.tf
variable "instance_type" {
description = "EC2 Instance Type"
}

模块/ Apollo /main.tf
resource "aws_instance" "instance" {     
...
instance_type = "${var.instance_type}"
...
}

最佳答案

调整结构,这是我对你的应用的理解。

├── dev
│ └── apollo_terraform.tfvars
├── test
│ └── apollo_terraform.tfvars
├── global
│ └── apollo_terraform.tfvars
├── mgmt
│ └── apollo_terraform.tfvars
├── main.tf, vars.tf, output.tf, apollo.tf, default.tf, etc
└── modules
├── apollo
│ ├── main.tf
│ ├── user_data.tpl
│ └── vars.tf
└── defaults
└── main.tf
apollo.tf将有源模块代码来使用共享模块 apollo . default.tf 的设置相同

你的计划/应用命令应该是这样的:
terraform plan -var-file=${env}/apollo_terraform.tfvars

关于Terraform - 如何将 tfvars 与模块一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46657905/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com