Security Group not found in VPC

July 24, 2018

I got the following error whilst creating an EC2 machine using terraform:

* aws_instance.drumcoder-web: Error launching source instance: InvalidGroup.NotFound: 
The security group 'admin-access-security-group' does not exist in VPC 'vpc-0d12345'
    status code: 400, request id: abc123

Looking at the AWS console, the security group did exist and was in the VPC in the error message.

The cause of this seemed to be that I was referencing my security group by name rather than id. This problem only manifested itself when I started adding a non-default VPC into the architecture.

I changed my terraform from:

resource "aws_instance" "bbr-web" {

    security_groups = [
        "${aws_security_group.web_traffic.name}",
        "${aws_security_group.admin_access.name}"
    ]
}

to

resource "aws_instance" "bbr-web" {

    security_groups = [
        "${aws_security_group.web_traffic.id}",
        "${aws_security_group.admin_access.id}"
    ]
}

and all was well.