覆盖变量的一部分
Override part of a variable
我有一个基本模块,其中包含以下内容:
locals {
common_tags = {
Name = var.name
AssetID = var.AssetID
AssetName = var.AssetName
AssetGroup = var.AssetGroup
AssetPurpose = var.AssetPurpose
AssetProgram = var.AssetProgram
AssetSchedule = var.AssetSchedule
}
}
output "Tags" {
value = local.common_tags
}
在我的其他模块中,我通过导入模块并引用它来使用这些标签 - 即。
module "base" {
source = "../base"
}
resource "aws_ecs_cluster" "ecs_cluster" {
name = "my_cluster"
tags = module.base.Tags
}
现在对于其中一个资源,我需要做同样的事情并使用除一个之外的大部分标签,这将有所不同,即。
resource "aws_subnet" "private_subnet" {
count = length(var.aws_zones)
vpc_id = aws_vpc.vpc.id
availability_zone = element(var.aws_zones, count.index)
cidr_block = element(var.private_subnets, count.index)
tags = {
Name = "private-${var.name}-${element(var.aws_zones, count.index)}"
AssetID = var.AssetID
AssetName = var.AssetName
AssetGroup = var.AssetGroup
AssetPurpose = var.AssetPurpose
AssetProgram = var.AssetProgram
AssetSchedule = var.AssetSchedule
}
}
有没有一种方法可以做到这一点,而不必重建整个对象并且不必引用基本模块中的所有这些变量?
这是 merge function 的理想情况:
resource "aws_subnet" "private_subnet" {
count = length(var.aws_zones)
vpc_id = aws_vpc.vpc.id
availability_zone = element(var.aws_zones, count.index)
cidr_block = element(var.private_subnets, count.index)
tags = merge(module.base.tags, {
Name = "private-${var.name}-${element(var.aws_zones, count.index)}"
})
}
merge
takes an arbitrary number of maps and returns a single map that contains a merged set of elements from all of the maps.
我有一个基本模块,其中包含以下内容:
locals {
common_tags = {
Name = var.name
AssetID = var.AssetID
AssetName = var.AssetName
AssetGroup = var.AssetGroup
AssetPurpose = var.AssetPurpose
AssetProgram = var.AssetProgram
AssetSchedule = var.AssetSchedule
}
}
output "Tags" {
value = local.common_tags
}
在我的其他模块中,我通过导入模块并引用它来使用这些标签 - 即。
module "base" {
source = "../base"
}
resource "aws_ecs_cluster" "ecs_cluster" {
name = "my_cluster"
tags = module.base.Tags
}
现在对于其中一个资源,我需要做同样的事情并使用除一个之外的大部分标签,这将有所不同,即。
resource "aws_subnet" "private_subnet" {
count = length(var.aws_zones)
vpc_id = aws_vpc.vpc.id
availability_zone = element(var.aws_zones, count.index)
cidr_block = element(var.private_subnets, count.index)
tags = {
Name = "private-${var.name}-${element(var.aws_zones, count.index)}"
AssetID = var.AssetID
AssetName = var.AssetName
AssetGroup = var.AssetGroup
AssetPurpose = var.AssetPurpose
AssetProgram = var.AssetProgram
AssetSchedule = var.AssetSchedule
}
}
有没有一种方法可以做到这一点,而不必重建整个对象并且不必引用基本模块中的所有这些变量?
这是 merge function 的理想情况:
resource "aws_subnet" "private_subnet" {
count = length(var.aws_zones)
vpc_id = aws_vpc.vpc.id
availability_zone = element(var.aws_zones, count.index)
cidr_block = element(var.private_subnets, count.index)
tags = merge(module.base.tags, {
Name = "private-${var.name}-${element(var.aws_zones, count.index)}"
})
}
merge
takes an arbitrary number of maps and returns a single map that contains a merged set of elements from all of the maps.