我正在使用 terraform 设置一个 aws 资源,该资源依赖于具有自己状态的早期资源的输出。我正在利用 terraform 中的数据源来尝试实现此目标https://www.terraform.io/docs/language/data-sources/index.html
\n状态
\n我有两个独立的 terraform 初始化/状态。一networking
加一eks
。状态存储在 s3 上,我使用工作区。
s3://myorg-terraform-state/env:/prod/networking/terraform.tfstate\ns3://myorg-terraform-state/env:/prod/eks/terraform.tfstate\n
Run Code Online (Sandbox Code Playgroud)\n网络/输出.tf
\n我有一个输出以下内容的网络地形。
\noutput "vpc_id" {\n description = "The ID of the VPC"\n value = module.vpc.vpc_id\n}\n\noutput "private_subnets" {\n description = "List of IDs of private subnets"\n value = module.vpc.private_subnets\n}\n\noutput "worker_group_mgmt_one_id" {\n description = "The id of worker_group_mgmt_one"\n value = aws_security_group.worker_group_mgmt_one.id\n}\n\noutput "worker_group_mgmt_two_id" {\n description = "The id of worker_group_mgmt_two"\n value = aws_security_group.worker_group_mgmt_two.id\n}\n
Run Code Online (Sandbox Code Playgroud)\nEKS 需要上述变量,因此我创建一个数据源以便能够访问它们。
\neks/data.tf
\ndata "terraform_remote_state" "networking" {\n backend = "s3"\n config = {\n bucket = "myorg-terraform-state"\n key = "networking/terraform.tfstate"\n region = "eu-west-2"\n }\n}\n\n
Run Code Online (Sandbox Code Playgroud)\neks/main.tf
\n然后我使用这个数据源eks/main.tf
...\ndata.terraform_remote_state.networking.outputs.private_subnets\n...\n
Run Code Online (Sandbox Code Playgroud)\n问题
\n然而,当我运行以下命令时,尽管状态存在于 s3 上,但我收到错误,并且在检查后我可以看到输出。
\nterraform workspace select prod\nterraform plan -var-file=prod.tfvars\n
Run Code Online (Sandbox Code Playgroud)\n\n\n错误:无法找到远程状态
\n在 data.tf 第 1 行,数据“terraform_remote_state”“networking”中:
\n
\n1:数据“terraform_remote_state”“networking”{在给定后端中找不到给定工作区的存储状态。\n
\n
文件夹结构
\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 networking\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 backend.tf\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main.tf\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 output.tf\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 prod.tfvars\n\xe2\x94\x82 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 provider.tf\n\xe2\x94\x82 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 variables.tf\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 eks\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 backend.tf\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 data.tf\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 main.tf\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 output.tf\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 prod.tfvars\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 provider.tf\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 variables.tf\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 README.md\n\n\n
Run Code Online (Sandbox Code Playgroud)\n
我找到了解决方案。
网络的 terraform 状态存储在以下路径中env:/prod/networking/terraform.tfstate
您可以看到我的数据配置中缺少以下前缀。
"env:/prod"
Run Code Online (Sandbox Code Playgroud)
data "terraform_remote_state" "networking" {
backend = "s3"
config = {
bucket = "myorg-terraform-state"
key = "env://${local.workspace}/networking/terraform.tfstate"
region = "eu-west-2"
}
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
12550 次 |
最近记录: |