如何从 Terraform 输出传递值而不将其放入 tfvars 文件
How to pass values from the Terraform output without putting it in the tfvars file
一切都好吗?
我正在研究如何使用 terraform 进行 eks 部署,作为一种很好的做法,我将其分成模块以使其可重用,但是当我 运行 testcluster-vpc
中的计划时,生成的输出是 public 和私有子网以及 vpc_id ,如何使用这些参数而不需要将这些值放在 testcluster
文件夹内的 terraform.tfvars
中?
我考虑过使用 Data Sources ,但它没有用它仍然要求在计划期间传递值
我把结构搞懂了
├── testclusters
│ ├── config.tf
│ ├── main.tf
│ ├── output.tf
│ ├── terraform.tfvars
│ └── variables.tf
├── testclusters-vpc
│ ├── config.tf
│ ├── main.tf
│ ├── outputs.tf
│ ├── terraform.tfvars
│ └── variables.tf
├── modules
│ ├── cluster
│ │ ├── eks_control_plane.tf
│ │ ├── eks_workers.tf
│ │ ├── outputs.tf
│ │ └── variables.tf
│ ├── eks-control-plane
│ │ ├── iam.tf
│ │ ├── main.tf
│ │ ├── outputs.tf
│ │ ├── security-groups.tf
│ │ └── variables.tf
│ ├── eks-vpc
│ │ ├── main.tf
│ │ ├── outputs.tf
│ │ └── variables.tf
│ └── eks-workers
│ ├── authconfig.tf
│ ├── iam.tf
│ ├── main.tf
│ ├── outputs.tf
│ ├── security-groups.tf
│ ├── user-data.tf
│ └── variables.tf
└── terraform-state
├── config.tf
├── terraform-state-dynamodb.tf
├── terraform-state-s3.tf
├── terraform.tfstate
├── terraform.tfstate.backup
├── terraform.tfvars
└── variables.tf
module "testcluster" {
source = "../modules/cluster"
vpc_id = data.aws_vpc.vpc.id # var.vpc_id
public_subnets = data.aws_subnet_ids.public.ids # var.public_subnet_ids
private_subnets = data.aws_subnet_ids.private.ids # var.private_subnet_ids
cluster_full_name = "${var.clusters_name_prefix}-${terraform.workspace}"
cluster_version = var.cluster_version
workers_instance_type = var.workers_instance_type
workers_ami_id = data.aws_ssm_parameter.workers_ami_id.value
workers_number_min = var.workers_number_min
workers_number_max = var.workers_number_max
workers_storage_size = var.workers_storage_size
commom_tags = local.commom_tags
aws_region = var.aws_region
}
locals {
commom_tags = {
ManagedBy = "terraform"
ClusterName = "${var.clusters_name_prefix}-${terraform.workspace}"
}
}
这是生成用于创建集群的 VPC 参数的输出文件
output "vpc_id" {
value = module.vpc.eks_cluster_vpc_id
}
output "private_subnet_ids" {
value = module.vpc.eks_private_subnet_ids
}
output "public_subnets_ids" {
value = module.vpc.eks_public_subnet_ids
}
据我了解,您想使用从一个 terraform 文件夹到另一个文件夹的输出变量。使用 terraform_remote_state
实现此目的的一种方法
EX : 在 testcluster-vpc
文件夹中,在 map
中公开输出
output "testclusters_vpc_net" {
value = {
"vpc_id" = module.vpc.eks_cluster_vpc_id
"private_subnet_ids" = module.vpc.eks_private_subnet_ids
"public_subnets_ids" = module.vpc.eks_public_subnet_ids
}
}
在 testcluster
文件夹中引用 testcluster-vpc
文件夹中的地形状态文件,例如
data "terraform_remote_state" "testcluster-vpc" {
backend = "s3"
config = {
bucket = "<bucket-name>"
key = "<path-to-tfstate-file>"
region = "<region>"
}
}
现在您可以访问值
vpc_id = data.terraform_remote_state.testcluster-vpc.outputs.testclusters_vpc_net.vpc_id
注意:要实现这一点,testclusters_vpc_net
应始终在 testcluster
文件夹之前 运行 访问更新的远程状态及其输出
一切都好吗?
我正在研究如何使用 terraform 进行 eks 部署,作为一种很好的做法,我将其分成模块以使其可重用,但是当我 运行 testcluster-vpc
中的计划时,生成的输出是 public 和私有子网以及 vpc_id ,如何使用这些参数而不需要将这些值放在 testcluster
文件夹内的 terraform.tfvars
中?
我考虑过使用 Data Sources ,但它没有用它仍然要求在计划期间传递值
我把结构搞懂了
├── testclusters
│ ├── config.tf
│ ├── main.tf
│ ├── output.tf
│ ├── terraform.tfvars
│ └── variables.tf
├── testclusters-vpc
│ ├── config.tf
│ ├── main.tf
│ ├── outputs.tf
│ ├── terraform.tfvars
│ └── variables.tf
├── modules
│ ├── cluster
│ │ ├── eks_control_plane.tf
│ │ ├── eks_workers.tf
│ │ ├── outputs.tf
│ │ └── variables.tf
│ ├── eks-control-plane
│ │ ├── iam.tf
│ │ ├── main.tf
│ │ ├── outputs.tf
│ │ ├── security-groups.tf
│ │ └── variables.tf
│ ├── eks-vpc
│ │ ├── main.tf
│ │ ├── outputs.tf
│ │ └── variables.tf
│ └── eks-workers
│ ├── authconfig.tf
│ ├── iam.tf
│ ├── main.tf
│ ├── outputs.tf
│ ├── security-groups.tf
│ ├── user-data.tf
│ └── variables.tf
└── terraform-state
├── config.tf
├── terraform-state-dynamodb.tf
├── terraform-state-s3.tf
├── terraform.tfstate
├── terraform.tfstate.backup
├── terraform.tfvars
└── variables.tf
module "testcluster" {
source = "../modules/cluster"
vpc_id = data.aws_vpc.vpc.id # var.vpc_id
public_subnets = data.aws_subnet_ids.public.ids # var.public_subnet_ids
private_subnets = data.aws_subnet_ids.private.ids # var.private_subnet_ids
cluster_full_name = "${var.clusters_name_prefix}-${terraform.workspace}"
cluster_version = var.cluster_version
workers_instance_type = var.workers_instance_type
workers_ami_id = data.aws_ssm_parameter.workers_ami_id.value
workers_number_min = var.workers_number_min
workers_number_max = var.workers_number_max
workers_storage_size = var.workers_storage_size
commom_tags = local.commom_tags
aws_region = var.aws_region
}
locals {
commom_tags = {
ManagedBy = "terraform"
ClusterName = "${var.clusters_name_prefix}-${terraform.workspace}"
}
}
这是生成用于创建集群的 VPC 参数的输出文件
output "vpc_id" {
value = module.vpc.eks_cluster_vpc_id
}
output "private_subnet_ids" {
value = module.vpc.eks_private_subnet_ids
}
output "public_subnets_ids" {
value = module.vpc.eks_public_subnet_ids
}
据我了解,您想使用从一个 terraform 文件夹到另一个文件夹的输出变量。使用 terraform_remote_state
EX : 在 testcluster-vpc
文件夹中,在 map
output "testclusters_vpc_net" {
value = {
"vpc_id" = module.vpc.eks_cluster_vpc_id
"private_subnet_ids" = module.vpc.eks_private_subnet_ids
"public_subnets_ids" = module.vpc.eks_public_subnet_ids
}
}
在 testcluster
文件夹中引用 testcluster-vpc
文件夹中的地形状态文件,例如
data "terraform_remote_state" "testcluster-vpc" {
backend = "s3"
config = {
bucket = "<bucket-name>"
key = "<path-to-tfstate-file>"
region = "<region>"
}
}
现在您可以访问值
vpc_id = data.terraform_remote_state.testcluster-vpc.outputs.testclusters_vpc_net.vpc_id
注意:要实现这一点,testclusters_vpc_net
应始终在 testcluster
文件夹之前 运行 访问更新的远程状态及其输出