terraform-aws-modules / terraform-aws-security-group

Terraform module to create AWS Security Group resources πŸ‡ΊπŸ‡¦
https://registry.terraform.io/modules/terraform-aws-modules/security-group/aws
Other
562 stars 1.08k forks source link

Circular dependency issue #291

Closed runtman closed 1 year ago

runtman commented 1 year ago

Description

I don't think this is a bug, if anything a misunderstanding?

I am following this example here:

https://github.com/terraform-aws-modules/terraform-aws-security-group/blob/master/examples/computed/main.tf

I have two files.

paris.tf

module "paris_sg" {
  count   = var.create_paris_sg ? 1 : 0
  source  = "terraform-aws-modules/security-group/aws"
  version = "v5.1.0"

  name        = "paris-sg-${var.environment}"
  description = "Restrict access to Paris Service"
  vpc_id      = var.vpc_id
....

And then I have webserver.tf

module "webserver_sg" {
  count   = var.create_webserver_sg ? 1 : 0
  source  = "terraform-aws-modules/security-group/aws"
  version = "v5.1.0"

  name        = "webserver-sg-${var.environment}"
  description = "Restrict access to Webservers"
  vpc_id      = var.vpc_id
...

Inside webserver.tf Iam trying to use computed_ingress_with_source_security_group_id to reference another security group made in this module.

  computed_ingress_with_source_security_group_id = [
    {
      from_port                = 51138
      to_port                  = 51138
      protocol                 = "tcp"
      description              = "paris-tcp"
      source_security_group_id = module.paris_sg.security_group_id
    },
  ]
  number_of_computed_ingress_with_source_security_group_id = 1

However on terraform validate I am faced with:

β•·
β”‚ Error: Unsupported attribute
β”‚
β”‚   on webserver.tf line 61, in module "webserver_sg":
β”‚   61:       source_security_group_id = module.paris_sg.security_group_id
β”‚     β”œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€
β”‚     β”‚ module.paris_sg is a list of object, known only after apply
β”‚
β”‚ Can't access attributes on a list of objects. Did you mean to access
β”‚ attribute "security_group_id" for a specific element of the list, or across
β”‚ all elements of the list?

I understand the error, but unsure how I get around it? It appears the example handles it fine, or am I misunderstanding something here?

Versions

runtman commented 1 year ago

count = var.create_webserver_sg ? 1 : 0 is causing my issue.

github-actions[bot] commented 1 year ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.