Search code examples
amazon-web-servicesterraformaws-security-groupterraform-modules

Terraform add new security group to an existing security group


I have created a new security group in the Autoscaling module in terraform. I want to add this security group to an existing security group in the module Database. The security group used in the database module is already created through the console. How to add a new security group to an existing security group in terraform?


Solution

  • If you want to change the SG created by console, you have to edit it manually.

    If you change the SG created by terraform, you can get the id of the existed SG manually and add it to a rule of SG

    EDIT:

    You need to output:

    output "security_group_id" {
      description = "Security group ID attached to the EKS workers."
      value       = module.auto_scaling_module.security_group_id
    }
    

    Than import by using data in data.tf

    data "terraform_remote_state" "autoscaling" {
      backend = "s3"
      config  = {
        region = "us-west-1"
        bucket = "wherestatelocate"
        key    = "path"
      }
    }
    

    Than use it in database module

      security_groups    = [data.terraform_remote_state.autoscaling.outputs.security_group_id
    ]
    

    This is what i am using