一切都好吗?
我正在研究如何使用terraform创建eks部署,作为一个好的实践,我将其分成模块以使其可重用,但是当我在testcluster-vpc中运行计划时,生成的输出是公共和私有子网以及vpc_id,我如何使用这些参数而不需要将这些值放在testcluster文件夹下的terraform.tfvars中?
我考虑过使用数据源,但它不起作用,它仍然要求在计划期间传递值
我把这个结构放在一起
├── 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.tfmodule "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
}发布于 2021-10-01 09:45:39
当我解压缩时,你想使用从一个terraform文件夹到另一个的输出变量。实现这一目标的一种方法是使用terraform_remote_state
例如:在testcluster-vpc文件夹中,在地图中显示输出
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注意:要实现此目的,应始终在运行testcluster文件夹之前运行testclusters_vpc_net,以访问更新后的远程状态及其输出
https://stackoverflow.com/questions/69398604
复制相似问题