Terraform: EC2 인스턴스에 여러 보안 그룹 연결

Terraform: EC2 인스턴스에 여러 보안 그룹 연결

EC2 생성 시 여러 보안 그룹을 연결하는 방법은 무엇입니까? 아래와 같이 모듈화했습니다.

**networking/main.tf**
# Web Server Security Group
resource "aws_security_group" "web_sg" {
  name        = "web_sg"
  description = "This security group will control the private Web Servers"
  vpc_id      = aws_vpc.perf_vpc.id
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

# Load Balancer Security Group
resource "aws_security_group" "alb_sg" {
  name        = "alb_sg"
  description = " This secruity group is for Application Load Balancer"
  vpc_id      = aws_vpc.perf_vpc.id
  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_security_group" "perf_pvt_sg" {
  name        = "perf_pvt_sg"
  description = "Aptean_Base-Perf_Pvt"
  vpc_id      = aws_vpc.perf_vpc.id
  depends_on  = [aws_security_group.bastion_sg]

  ingress {
    description = "kaspersky"
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["10.176.0.35/32"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

**networking/outputs.tf**
output "perf_pvt_sg" {
  value = aws_security_group.perf_pvt_sg.id
}

output "web_sg" {
  value = aws_security_group.web_sg.id
}

output "alb_sg" {
  value = aws_security_group.alb_sg.id
}

**root/main.tf**
# Deploy Web Servers
module "web_servers" {
  source          = "./web_servers"
  keyname         = module.domain_controllers.key_name
  public_key_path = var.public_key_path
  web_count       = var.web_count
  web_inst_type   = var.web_inst_type
  pvtsubnets      = module.networking.private_subnets
  webserver_sg    = [
    module.networking.web_sg,
    module.networking.perf_pvt_sg,
    module.networking.alb_sg
  ]
}

모든 것이 정상이며 예상대로 생성되고 Terraform 출력에도 리소스가 표시되지만 모든 EC2 인스턴스(이 경우 웹 서버를 가리키고 있음)에는 기본 보안 그룹이 연결되어 있습니다. 다른 모든 보안 그룹이 생성된 것을 알 수 있습니다. 아무것도 붙어 있지 않지만. web_sg를 id 및 name 속성으로 전환하려고 했습니다.

web_sg          = [
        module.networking.web_sg.id,
        module.networking.perf_pvt_sg.id,
        module.networking.alb_sg.id
        ]

오류가 발생합니다(*.id 및 *.name에 대해 동일한 오류).

Error: Unsupported attribute

  on main.tf line 46, in module "web_servers":
  46:     module.networking.web_sg.name,
    |----------------
    | module.networking.web_sg is "sg-008001301c71877a9"
This value does not have any attributes.


Error: Unsupported attribute

  on main.tf line 47, in module "web_servers":
  47:     module.networking.perf_pvt_sg.name,
    |----------------
    | module.networking.perf_pvt_sg is "sg-0a50f754aceaae6cd"

This value does not have any attributes.


Error: Unsupported attribute

  on main.tf line 48, in module "web_servers":
  48:     module.networking.alb_sg.name
    |----------------
    | module.networking.alb_sg is "sg-05c898e0b6873c411"

This value does not have any attributes.

내가 도대체 ​​뭘 잘못하고있는 겁니까?

AWS 콘솔: 여기에 이미지 설명을 입력하세요

여기에 이미지 설명을 입력하세요

편집1: web_servers/main.tf

#Web Server
resource "aws_instance" "web" {
  count         = var.web_count
  ami           = data.aws_ami.server_ami.id
  ebs_optimized = true
  instance_type = var.web_inst_type
  subnet_id     = element(var.pvtsubnets, count.index)
  credit_specification {
    cpu_credits = "standard"
  }
  root_block_device {
    volume_type = "gp2"
    volume_size = 80
    encrypted   = true
    kms_key_id  = "1d9ef127-cc8f-4dda-9bdf-abdad498ea6f"
  }
  ebs_block_device {
    device_name = "/dev/sdf"
    volume_type = "gp2"
    volume_size = 40
    encrypted   = true
    kms_key_id  = "1d9ef127-cc8f-4dda-9bdf-abdad498ea6f"
  }
  tags = {
    Name = "PerformanceWeb0${count.index + 1}"
  }
}

web_servers/variables.tf

variable "keyname" {}
variable "public_key_path" {}
variable "web_count" {}
variable "web_inst_type" {}
variable "pvtsubnets" {
  type = list(string)
}
variable "webserver_sg" {}

답변1

정리했지요. 이것이 유일한 방법인지 확실하지 않습니다. 코드를 검토해 보니 내가 댓글을 달았기 때문에 나타나는 것 같습니다.vpc_security_group_idsweb_servers 모듈에서는 VPC 기본 보안 그룹을 사용하고 있었습니다. 아래와 같이 몇 가지 변경 사항을 적용했습니다.

web_servers/main.tf

#Web_servers
resource "aws_instance" "web" {
  count         = var.web_count
  ami           = data.aws_ami.server_ami.id
  ebs_optimized = true
  instance_type = var.web_inst_type
  subnet_id     = element(var.pvtsubnets, count.index)
  vpc_security_group_ids = [
    var.web_sg,
    var.perf_pvt_sg,
    var.alb_traffic_sg
  ]

web_servers/variable.tf

variable "web_sg" {}
variable "perf_pvt_sg" {}
variable "alb_traffic_sg" {}

루트/main.tf

#Deploy Web Servers
module "web_servers" {
  source            = "./web_servers"
  keyname           = module.domain_controllers.key_name
  public_key_path   = var.public_key_path
  web_count     = var.web_count
  web_inst_type = var.web_inst_type
  pvtsubnets        = module.networking.private_subnets
  web_sg        = module.networking.web_sg
  perf_pvt_sg       = module.networking.perf_pvt_sg
  alb_traffic_sg       = module.networking.alb_traffic_sg
}

원하는 출력 여기에 이미지 설명을 입력하세요

관련 정보