Home > Software design >  Get ID of AWS Security Group Terraform Module
Get ID of AWS Security Group Terraform Module

Time:12-10

I used this module to create a security group inside a VPC. One of the outputs is the security_group_id, but I'm getting this error:

│ Error: Unsupported attribute
│ 
│   on ecs.tf line 39, in resource "aws_ecs_service" "hello_world":
│   39:     security_groups = [module.app_security_group.security_group_id]
│     ├────────────────
│     │ module.app_security_group is a object, known only after apply
│ 
│ This object does not have an attribute named "security_group_id".

I need the security group for an ECS service:

resource "aws_ecs_service" "hello_world" {
  name            = "hello-world-service"
  cluster         = aws_ecs_cluster.container_service_cluster.id
  task_definition = aws_ecs_task_definition.hello_world.arn
  desired_count   = 1
  launch_type     = "FARGATE"

  network_configuration {
    security_groups = [module.app_security_group.security_group_id]
    subnets         = module.vpc.private_subnets
  }

  load_balancer {
    target_group_arn = aws_lb_target_group.loadbalancer_target_group.id
    container_name   = "hello-world-app"
    container_port   = 3000
  }

  depends_on = [aws_lb_listener.loadbalancer_listener, module.app_security_group]
}

I understand that I can only know the security group ID after it is created. That's why I added the depends_on part on the ECS stanza, but it kept returning the same error.

Update

I specified count as 1 on the app_security_group module and this is the error I'm getting now.

│ Error: Unsupported attribute
│ 
│   on ecs.tf line 39, in resource "aws_ecs_service" "hello_world":
│   39:     security_groups = module.app_security_group.security_group_id
│     ├────────────────
│     │ module.app_security_group is a list of object, known only after apply
│ 
│ Can't access attributes on a list of objects. Did you mean to access an attribute for a specific element of the list, or across all elements of the list?

Update II

This is the module declaration:

module "app_security_group" {
  source  = "terraform-aws-modules/security-group/aws//modules/web"
  version = "3.17.0"

  name        = "${var.project}-web-sg"
  description = "Security group for web-servers with HTTP ports open within VPC"
  vpc_id      = module.vpc.vpc_id

  #   ingress_cidr_blocks = module.vpc.public_subnets_cidr_blocks
  ingress_cidr_blocks = ["0.0.0.0/0"]
}

CodePudding user response:

I took a look at that module. The problem is that the version 3.17.0 of the module simply does not have the output of security_group_id. You are using a really old version.

The latest version from the site is 4.7.0, you would want to upgrade to this one. In fact, any version above 4.0.0 has the security_group_id, so you need to at least 4.0.0.

CodePudding user response:

As you are using count, please try below.

network_configuration {
    security_groups = [module.app_security_group[0].security_group_id]
    subnets         = module.vpc.private_subnets
  }
  • Related