0
votes

Getting started on Terraform. I am trying to provision an EC2 instance using the following .tf file. I have a default VPC already in my account in the AZ I am trying to provision the EC2 instance.

# Terraform Settings Block
 terraform {
      required_providers {
         aws = {
             source  = "hashicorp/aws"
             #version = "~> 3.21" # Optional but recommended in production
        }
     }
 }

  # Provider Block
  provider "aws" {
        profile = "default"
        region  = "us-east-1"
  }

  # Resource Block
  resource "aws_instance" "ec2demo" {
           ami           = "ami-c998b6b2" 
           instance_type = "t2.micro"
  }

I do the following Terraform commands.

  1. terraform init
  2. terraform plan
  3. terraform apply

aws_instance.ec2demo: Creating...

Error: Error launching source instance: VPCIdNotSpecified: No default VPC for this user. GroupName is only supported for EC2-Classic and default VPC. status code: 400, request id: 04274b8c-9fc2-47c0-8d51-5b627e6cf7cc

on ec2-instance.tf line 18, in resource "aws_instance" "ec2demo": 18: resource "aws_instance" "ec2demo" {

1

1 Answers

2
votes

As the error suggests, it doesn't find the default VPC in the us-east-1 region. You can provide the subnet_id within your VPC to create your instance as below.

 resource "aws_instance" "ec2demo" {
           ami           = "ami-c998b6b2" 
           instance_type = "t2.micro"
           subnet_id = "subnet-0b1250d733767bafe"
  }