地形版本
Terraform v0.12.1
Terraform 配置文件
main.tf
在我的根提供商中:
provider "google" {}
module "organisation_info" {
source = "../../modules/organisation-info"
top_level_domain = "smoothteam.fi"
region = "us-central1"
}
module "stack_info" {
source = "../../modules/stack-info"
organisation_info = "${module.organisation_info}"
}
这是模块“组织信息”:
variable "top_level_domain" {}
variable "region" {}
data "google_organization" "organization" {
domain = "${var.top_level_domain}"
}
locals {
organization_id = "${data.google_organization.organization.id}"
ns = "${replace("${var.top_level_domain}", ".", "-")}-"
}
output "organization_id" {
value = "${local.organization_id}"
}
output "ns" {
value = "${local.ns}"
}
然后模块“堆栈信息”:
variable "organisation_info" {
type = any
description = "The organisation-scope this environment exists in."
}
module "project_info" {
source = "../project-info"
organisation_info = "${var.organisation_info}"
name = "${local.project}"
}
locals {
# Use the 'default' workspace for the 'staging' stack.
name = "${terraform.workspace == "default" ? "staging" : terraform.workspace}"
# In the 'production' stack, target the production project. Otherwise, target the staging project.
project = "${local.name == "production" ? "production" : "staging"}"
}
output "project" {
value = "${module.project_info}" # COMMENTING THIS OUTPUT REMOVES THE CYCLE.
}
最后,“项目信息”模块:
variable "organisation_info" {
type = any
}
variable "name" {}
data "google_project" "project" {
project_id = "${local.project_id}"
}
locals {
project_id = "${var.organisation_info.ns}${var.name}"
}
output "org" {
value = "${var.organisation_info}"
}
调试输出
完成后terraform destroy -auto-approve
,我得到:
Error: Cycle: module.stack_info.module.project_info.local.project_id, module.stack_info.output.project, module.stack_info.module.project_info.data.google_project.project (destroy), module.organisation_info.data.google_organization.organization (destroy), module.stack_info.var.organisation_info, module.stack_info.module.project_info.var.organisation_info, module.stack_info.module.project_info.output.org
并terraform graph -verbose -draw-cycles -type=plan-destroy
给了我这张图:
来源:
digraph {
compound = "true"
newrank = "true"
subgraph "root" {
"[root] module.organisation_info.data.google_organization.organization" [label = "module.organisation_info.data.google_organization.organization", shape = "box"]
"[root] module.stack_info.module.project_info.data.google_project.project" [label = "module.stack_info.module.project_info.data.google_project.project", shape = "box"]
"[root] provider.google" [label = "provider.google", shape = "diamond"]
"[root] module.organisation_info.data.google_organization.organization" -> "[root] module.stack_info.module.project_info.data.google_project.project"
"[root] module.organisation_info.data.google_organization.organization" -> "[root] provider.google"
"[root] module.stack_info.module.project_info.data.google_project.project" -> "[root] provider.google"
}
}
预期行为
这个想法是在组织、项目和堆栈级别使用模块来设置可以在所有资源中重复使用的命名约定。Organisation-info 加载组织信息,关于项目的项目信息,堆栈信息根据当前工作空间确定要定位的项目。
我在模块中省略了一堆其他逻辑,以保持它们在这个问题上的清洁。
根据terraform
没有循环,destroy
应该可以正常工作。
实际行为
我们得到了我在上面发布的循环,即使 terraform 没有显示循环。
重现步骤
- 设置三个模块,
organisation-info
,project-info
, 和stack-info
如上所示。 - 如上所示设置根提供程序。
terraform init
terraform destroy
(如果你先申请似乎并不重要)
附加上下文
奇怪的是,如果我在 中注释掉这个输出stack-info
,循环就会停止:
output "project" {
value = "${module.project_info}" # IT'S THIS... COMMENTING THIS OUT REMOVES THE CYCLE.
}
这看起来真的很奇怪......我既不明白为什么输出变量会有所作为,也不明白为什么在没有循环的情况下会出现循环错误。
奇怪terraform plan -destroy
的是,不显示循环,只有terraform destroy
。
我的蜘蛛侠感觉告诉我邪恶正在发生。
感谢任何可以告诉我发生了什么,这是否是一个错误,以及如何解决的人。