For system, network and cloud administrators

How to fix AWS EC2 error “No default VPC for this user” in Terraform

Sometimes, when trying to create an AWS EC2 instance in Terraform, you might come across an error similar to:

Error: creating EC2 Instance: VPCIdNotSpecified: No default VPC for this user. GroupName is only supported for EC2-Classic and default VPC.

The error refers to the user not having access to the proper AWS resources declared in the Terraform code. Like trying to access an EC2 instance in the wrong AWS zone region. So, basically Terraform will try to create an EC2 instance of an Ubuntu server using the region, AMI id and instance type provided in the code but it will not find those things thus the error.

How to fix AWS EC2 error "No default VPC for this user" in Terraform
AWS EC2 error when trying to create an instance

To make sure you’re providing the correct info about your AWS resources:

  • choose the region your project needs to be into (us-east-1, eu-west-1, etc.);
  • for choosing the proper AMI (Amazon Machine Images) with its proper id, browse to AWS Management Console > EC2 > Images > AMIs catalog;
  • then, depending on the requirement of your project, choose a proper instance type by going to AWS Management Console > Instances > Instance types.

For example:

provider "aws" {
  region = "eu-west-2"
resource "aws_instance" "ec2" {
  ami = "ami-0a6006bac3b9bb8d3"
  instance_type = "t2.micro"