Terraform: How to give a single security group for an aws instance ?

Created on 1 Jun 2015  ยท  6Comments  ยท  Source: hashicorp/terraform

I need to assign an existing security group to the aws instance. If you are using ''aws_instance" it should only supports security_groups ( ie multiple security groups ). So how to give an existing security group for an aws instance ?

Most helpful comment

@radeksimko security_groups list requires name instead of id.

All 6 comments

Hi,
aws_instance.security_groups supports a list of security groups. See the linked docs.

Example

resource "aws_instance" "web" {
    ami = "ami-1234"
    instance_type = "m1.small"
    security_groups = [
        "${aws_security_group.first.id}",
        "${aws_security_group.second.id}"
    ]
    tags {
        Name = "HelloWorld"
    }
}

@radeksimko security_groups list requires name instead of id.

@asimataurora using the Group ID worked for me

```resource "aws_instance" "mongodb" {
ami = "ami-0f9cf087c1f27d9b1"
instance_type = "t2.medium"
security_groups = [ "sg-07ef9418bd7d280d1" ]
tags = {
Name = "mongodb"
Owner = "suryaval"
Env = "dev"
}
}

> \* aws_instance.mongodb: Error launching instance, possible mismatch of Security Group IDs and Names. See AWS Instance docs here: https://terraform.io/docs/providers/aws/r/instance.html.

After i modified the security_groups to its name, it started working

resource "aws_instance" "mongodb" {
ami = "ami-0f9cf087c1f27d9b1"
instance_type = "t2.medium"
security_groups = [ "openToAll" ]
tags = {
Name = "mongodb"
Owner = "suryaval"
Env = "dev"
}
}
```

The doc is wrong, not even unclear. It says:
security_groups - (Optional, EC2-Classic and default VPC only) A list of security group names (EC2-Classic) or IDs (default VPC) to associate with.
But it should actually take group name, not ids.

Please update docs.

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.

Was this page helpful?
0 / 5 - 0 ratings