我有一个Terraform代码库,它部署一个私有EKS集群、一个堡垒主机和其他AWS服务。我还增加了几个安全小组到在地形。其中一个安全组允许从my到bastion主机的入站通信量,以便我可以将SSH发送到该节点上。这个安全组被称为bastionSG,这也很好。
但是,最初我无法从我的堡垒主机运行kubectl,这是我用来针对EKS集群节点执行kubernetes开发的节点。原因是我的EKS集群是一个私有的,并且只允许来自同一个VPC中的节点的通信,我需要添加一个安全组,它允许从我的堡垒主机进行通信到cluster control plane,这正是我的安全组bastionSG出现的地方。
因此,我现在的例程是,一旦Terraform部署了所有东西,我就会找到自动生成的EKS安全组,并通过AWS控制台(UI)将我的bastionSG作为入站规则添加到其中,如下图所示。

我不想通过UI来完成这个任务,因为我已经在使用Terraform来部署我的整个基础设施。
我知道我可以像这样查询现有的安全组
data "aws_security_group" "selectedSG" {
id = var.security_group_id
}在这种情况下,假设selectedSG是由EKS创建的安全组,一旦terraform完成应用过程。然后,我想将bastionSG的入站规则添加到它,而不需要编写它自动添加的其他规则。
UPDATE:> EKS节点组
resource "aws_eks_node_group" "flmd_node_group" {
cluster_name = var.cluster_name
node_group_name = var.node_group_name
node_role_arn = var.node_pool_role_arn
subnet_ids = [var.flmd_private_subnet_id]
instance_types = ["t2.small"]
scaling_config {
desired_size = 3
max_size = 3
min_size = 3
}
update_config {
max_unavailable = 1
}
remote_access {
ec2_ssh_key = "MyPemFile"
source_security_group_ids = [
var.allow_tls_id,
var.allow_http_id,
var.allow_ssh_id,
var.bastionSG_id
]
}
tags = {
"Name" = "flmd-eks-node"
}
}如上所述,EKS节点组中包含bastionSG安全组。我希望允许从我的堡垒主机连接到EKS控制平面。
EKS集群
resource "aws_eks_cluster" "flmd_cluster" {
name = var.cluster_name
role_arn = var.role_arn
vpc_config {
subnet_ids =[var.flmd_private_subnet_id, var.flmd_public_subnet_id, var.flmd_public_subnet_2_id]
endpoint_private_access = true
endpoint_public_access = false
security_group_ids = [ var.bastionSG_id]
}
}bastionSG_id是下面创建的安全组的输出,它作为变量传递到上面的代码中。
BastionSG安全组
resource "aws_security_group" "bastionSG" {
name = "Home to bastion"
description = "Allow SSH - Home to Bastion"
vpc_id = var.vpc_id
ingress {
description = "Home to bastion"
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = []
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
ipv6_cidr_blocks = ["::/0"]
}
tags = {
Name = "Home to bastion"
}
}发布于 2022-10-24 21:50:26
您可以使用Terraform "aws_security_group_rule“扩展现有的安全组,它接受任意但必需的安全组ID。
https://serverfault.com/questions/1113890
复制相似问题