I’m trying to create a security group and associate it with a VPC created in my code, but when I try to apply it, I get an error telling me that it doesn’t exist in my default VPC, and therefore will not create it. It is only supposed to be referring to the VPC defined in my code, but when I run terraform apply, it references the default VPC instead.
Error: creating EC2 Instance: InvalidGroup.NotFound: The security group 'allow_web_traffic' does not exist in VPC 'vpc-060078657ca8ea948'│ status code: 400, request id: 392e7004-4ed6-4f0d-ae72-5deb6d205c50
│
│ with aws_instance.windows,
│ on main.tf line 10, in resource "aws_instance" "windows":
│ 10: resource "aws_instance" "windows" {
│
With the assumption that this is the block I change-
resource "aws_instance" "windows" {
ami = "ami-0f9c44e98edf38a2b"
instance_type = "t2.micro"
# xlarge for later
# ami = "ami-0f9c44e98edf38a2b"
# instance_type = "g4dn.xlarge"
associate_public_ip_address = true
key_name = "terraform-key"
subnet_id = "subnet-0a248f3e680da4aef"
vpc_security_group_ids = ["vpc-0887cab5853d18527"]
tags = {
Name = "microTest"
}
}
I still get this same error-
Error: creating EC2 Instance: InvalidGroup.NotFound: The security group 'vpc-0887cab5853d18527' does not exist in VPC 'vpc-060078657ca8ea948'
│ status code: 400, request id: fc9f959f-10cc-4904-a0d2-e866ae82dbd2
│
│ with aws_instance.windows,
│ on main.tf line 10, in resource "aws_instance" "windows":
│ 10: resource "aws_instance" "windows" {
Without quotes, though, just for testing purposes, I get this error-
Error: Invalid reference
│
│ on main.tf line 19, in resource "aws_instance" "windows":
│ 19: vpc_security_group_ids = [vpc-0887cab5853d18527]
│
│ A reference to a resource type must be followed by at least one attribute access, specifying the resource name.
I’m still somewhat at a loss of what I could change here to make this work, or if I’m just changing things in the wrong places.
The ID vpc-0887cab5853d18527 looks like a VPC ID, not a security group ID. You should double check and make sure you have the right reference - it should look something like sg-0796c1a96949b5c07. You can look up the security group ID in the EC2 console under Network & Security > Security Groups.
My mistake on that one-- but now I’m getting a fun similar error where it’s conflicting with the default subnet that’s linked to the default VPC:
Error: creating EC2 Instance: InvalidParameter: Security group sg-02f1efa6466146146 and subnet subnet-0a248f3e680da4aef belong to different networks.
│ status code: 400, request id: 8a404c14-e0be-4384-906d-fb7f88531153
│
│ with aws_instance.windows,
│ on main.tf line 10, in resource "aws_instance" "windows":
│ 10: resource "aws_instance" "windows" {
I have private and public subnets instantiated already.
I apologize if these are weird problems with actually really simple solutions, this is my first time making anything from scratch, and I’ve run into some really awkward problems from some mistakes I’ve made in the AWS console.
As the error message says, your security group and subnet reside in different VPCs. Based on past messages in the thread, your subnet seems to be in vpc-060078657ca8ea948 but your security group might be in vpc-0887cab5853d18527 which is the wrong ID used instead of the security group ID by mistake. You should ensure that the security group is created in vpc-060078657ca8ea948.
This specific problem I believe has been the root cause of all of my issues- at one point, early into development, I accidentally deleted the default VPC that was already instantiated within the AWS console. It’s been my belief that because I’m creating a new VPC, that this is not something that should matter, as I’m not using the default VPC. Am I misunderstanding the way that Terraform and AWS interface with one another?
If both the security group and VPC are being made with Terraform, how am I supposed to link it to the default VPC made in the AWS console?
(Additionally, if I set the vpc_id to the default VPC ID, it returns that it does not exist in the default VPC.)
If everything is created in Terraform and you are using references to set dependent attribute values, there should be no issue. So I suspect something is wrong with your Terraform configuration. Could you please provide your full configuration for further troubleshooting?
(P.S. If you deleted the default VPC and need to recreate it, you can run the CLI command: aws ec2 create-default-vpc You can run this in Cloud Shell if you don’t have AWS CLI locally. But wait until your Terraform is resolved so you don’t add more variables to the situation.)
I think this forum allows you to attach/upload a file in a reply. You can redact any sensitive info you don’t want to share (just use *s or something) before you upload.
I am assuming that you just have one main.tf file or something similar for your Terraform configuration. If you have you multiple .tf, you can perhaps zip them up and upload the zip. Just make sure you strip any security sensitive info before doing so.
In your configuration you have the following hardcoded:
subnet_id in aws_instance.windows
vpc_security_group_ids in aws_instance.windows
vpc_id in aws_security_group.allow_web
They should be changed to the following (subnet I am just guessing):
aws_subnet.public_subnets[0].id
aws_security_group.allow_web.id
aws_vpc.main.id
Fixing these should unblock you, but I think you have bigger problems to deal with. Here are some issues I see:
You don’t have a NAT gateway set up and routing configured for the private subnets. It’s fine for now if you are not using it, but you’ll eventually need to configure them.
Your security group is wide open to the internet. Aside from the insecure HTTP port, you also have SSH opened. You need to lock it down before you go any further!
I don’t know what your AMI ID points to, but it seems to be a Windows AMI. So you’d probably need RDP instead of SSH access. I recommend that you investigate using SSM Session Manager instead of opening SSH or RDP ports if you are in doubt.
I would recommend manually using an Elastic IP for the EC2 instance over using associate_public_ip_address in aws_instance.
I actually tried to run your configuration but the EC2 instance wouldn’t start. The AMI seems to be for Windows so I am not sure if t2.micro is good enough to run it.
Oh yeah, fully setting up a gateway was the next step I was going to take before running into all of these problems. I had a security group written for RDP, but rewrote it into this to see if it fixed any of my problems, which it did not.
This was the security group I had written before, though I’m certain it doesn’t follows every best practice-
In general, never use 0.0.0.0/0 for any inbound rules unless there is a very valid reason to. If you must, set it to your home or work IP address (with /32 prefix for one IP). You can find your IP address by Googling “what is my IP” or asking IT for your company network’s external IP CIDRs. Hope this answers all your questions concerning this thread.