Mim*_*imi 4 terraform terraform-provider-aws amazon-eks
我遇到了 Terraform EKS 标记问题,似乎没有找到可行的解决方案来在创建新集群时标记所有 VPC 子网。
提供一些背景信息:我们有一个 AWS VPC,我们将多个 EKS 集群部署到子网中。我们不创建 VPC 或子网是 EKS 集群创建的一部分。因此,创建集群的 terraform 代码无法标记现有子网和 VPC。虽然 EKS 会添加所需的标签,但下次我们在 VPC 上运行 terraform apply 时,它们会自动删除。
我的解决方法是在 VPC 中提供一个 terraform.tfvars 文件,如下所示:
eks_tags =
[
"kubernetes.io/cluster/${var.cluster-1}", "shared",
"kubernetes.io/cluster/${var.cluster-2}", "shared",
"kubernetes.io/cluster/${var.cluster-2}", "shared",
]
Run Code Online (Sandbox Code Playgroud)
然后在 VPC 和子网资源中,我们执行类似的操作
resource "aws_vpc" "demo" {
cidr_block = "10.0.0.0/16"
tags = "${
map(
${var.eks_tags}
)
}"
}
Run Code Online (Sandbox Code Playgroud)
但是,上面的方法似乎不起作用。我已经尝试了https://www.terraform.io/docs/configuration-0-11/interpolation.html 中的各种 Terraform 0.11 功能,但没有任何帮助。
有没有人能够解决这个问题?
我们总是为每个 EKS 集群创建新的 VPC 和子网的想法是错误的。显然,必须是一种使用 Terraform 标记现有 VPC 和子网资源的方法吗?
您现在可以使用 aws 提供程序ignore_tags属性,以便aws_ec2_tag下次应用 VPC 模块时不会删除使用资源制作的标签。
例如提供者变成:
provider "aws" {
profile = "terraform"
region = "us-west-1"
// This is necessary so that tags required for eks can be applied to the vpc without changes to the vpc wiping them out.
// https://registry.terraform.io/providers/hashicorp/aws/latest/docs/guides/resource-tagging
ignore_tags {
key_prefixes = ["kubernetes.io/"]
}
}
Run Code Online (Sandbox Code Playgroud)
添加之后,您可以aws_ec2_tag像这样在 EKS 模块中利用资源,而不必担心下次应用 VPC 模块时标签会被删除。
/*
Start of resource tagging logic to update the provided vpc and its subnets with the necessary tags for eks to work
The toset() function is actually multiplexing the resource block, one for every item in the set. It is what allows
for setting a tag on each of the subnets in the vpc.
*/
resource "aws_ec2_tag" "vpc_tag" {
resource_id = data.terraform_remote_state.vpc.outputs.vpc_id
key = "kubernetes.io/cluster/${var.cluster_name}"
value = "shared"
}
resource "aws_ec2_tag" "private_subnet_tag" {
for_each = toset(data.terraform_remote_state.vpc.outputs.private_subnets)
resource_id = each.value
key = "kubernetes.io/role/elb"
value = "1"
}
resource "aws_ec2_tag" "private_subnet_cluster_tag" {
for_each = toset(data.terraform_remote_state.vpc.outputs.private_subnets)
resource_id = each.value
key = "kubernetes.io/cluster/${var.cluster_name}"
value = "shared"
}
resource "aws_ec2_tag" "public_subnet_tag" {
for_each = toset(data.terraform_remote_state.vpc.outputs.public_subnets)
resource_id = each.value
key = "kubernetes.io/role/elb"
value = "1"
}
resource "aws_ec2_tag" "public_subnet_cluster_tag" {
for_each = toset(data.terraform_remote_state.vpc.outputs.public_subnets)
resource_id = each.value
key = "kubernetes.io/cluster/${var.cluster_name}"
value = "shared"
}
Run Code Online (Sandbox Code Playgroud)
小智 1
当有两段具有不同状态文件的代码试图作用于同一资源时,这个问题将始终存在。
解决此问题的一种方法是每次应用 EKS terraform 代码时将 VPC 资源重新导入到 VPC 状态文件中。这也会导入您的标签。子网也是如此,但从长远来看,这是一个手动且乏味的过程。
terraform import aws_vpc.test_vpc vpc-a01106c2
参考: https: //www.terraform.io/docs/providers/aws/r/vpc.html
干杯!
| 归档时间: |
|
| 查看次数: |
1902 次 |
| 最近记录: |