簡體   English   中英

Terraform AWS EKS 安全組問題

[英]Terraform AWS EKS security group issue

我正在使用 terraform 腳本部署 AWS EKS 集群。 一切都部署得很好。 但是我遇到了安全組的問題。 我添加了兩個端口以允許進入我的應用程序 URL 的流量。

但問題是,在完成 EKS 集群部署后,創建了兩個安全組,一個是我創建的,另一個是 EKS 自己創建的。

所以在這里我必須在 EKS 創建的安全組中手動添加端口,才能在瀏覽器上訪問我的應用程序的 URL。

在這里,我如何在 EKS 創建的安全組中添加我的特定端口。

此值可作為 vpc_config.cluster_security_group_id 下eks_cluster資源的屬性訪問。

使用此值,您可以創建一個security_group_rule資源並傳入您從上述屬性中檢索回來的 ID。

這可以使用以下代碼解決,添加數據塊以導入由 AWS EKS 創建的安全組,並添加其他資源塊以定義您想要實施的規則。

請記住,您必須為入口出口創建單獨的規則,並且不能將這些資源與內聯規則定義結合使用。


    # SG created by EKS
    data "aws_security_group" "imported_sg" {
      id = "sg-123456"
    }
    
    # SG Rule which you would like to add
    resource "aws_security_group_rule" "example" {
      type              = "ingress"
      from_port         = 0
      to_port           = 65535
      protocol          = "tcp"
      cidr_blocks       = ["10.0.0.0/16"]
    
      security_group_id = aws_security_group.imported_sg.id
    }

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/security_group_rule https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/security_group

這是適當的答案。 If you scroll down the page in the terraform docs, it gives a list of attributes (which are exportable): https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/eks_cluster . 您會注意到 vpc_config 屬性有一個成員 cluster_security_group_id:

vpc_config 屬性 cluster_security_group_id - 由 Amazon EKS 為集群創建的集群安全組。 托管節點組使用此安全組進行控制平面到數據平面的通信。

鑒於 vpc_config 是一個列表,要實際訪問此屬性,您需要按如下方式訪問它:

 aws_eks_cluster.cluster.vpc_config[0].cluster_security_group_id

如果您未指定集群安全組,則 AWS 將自動生成一個集群安全組,其中包含允許集群和集群節點組通信的規則。 因此,像這樣導出此屬性是一種常見的模式:

output "cluster_security_group_id" {
    value   = aws_eks_cluster.cluster.vpc_config[0].cluster_security_group_id
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM