Create AWS instance

Hi, I am new to Terraform and I am trying to deploy an EC2 to an existing VPC which is not the default VPC.

I have copied the codes from the URL below, but I am getting an error "creating EC2 Instance: VPCIDNotSpecified: No default VPC for this user. After I add “vpc_id” in the codes, I received another error “Error: Unsupported argument. An argument named “vpc_id” is not expected here.”

resource “aws_instance” “web” {
vpc_id = “vpc-0b89dffeadebdf035”
ami = “ami-003c463c8207b4dfa”
instance_type = “t2.micro”

tags = {
Name = “HelloWorld”
}
}

https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/instance

I think you are mixing up the different resources from the examples in the documentation. There’s no argument named vpc_id for the aws_instance resource. You typically provide a subnet_id for a subnet that belongs to the VPC in which you want to deploy the EC2 instance.

It is surprisingly difficult to find a blog post online that goes beyond the most basic scenario with default VPC. But this blog post might give you a better idea on how to creating an EC2 instance in a VPC that’s created via Terraform.

Hope this helps and best of luck with your continued Terraform learning.