Day 96: Create EC2 Instance Using Terraform --> 100 days of DevOps

resource "tls_private_key" "xfusion-kp" {
  algorithm = "RSA"
  rsa_bits  = 4096
}
resource "aws_key_pair" "xfusion-kp"{
    key_name = "xfusion-kp"
    public_key = tls_private_key.xfusion-kp.public_key_openssh
}
data "aws_security_group" "default"{
    name = "default"
}
resource "aws_instance" "xfusion-ec2" {
  ami           = "ami-0c101f26f147fa7fd"
  instance_type = "t2.micro"
  key_name      = aws_key_pair.xfusion-kp.key_name
  security_groups = [data.aws_security_group.default.id]
  tags = {
    Name = "xfusion-ec2"
  }
}

above is my code

below is my error:

Resources have not been created using 'terraform'.

what I’m missing here? could you please help on this

refer to this

for solution

Hi @likithsai004

Ensure you name your Terraform file as main.tf in the required directory: /home/bob/terraform.

Your config looks fine, except for security_groups in the aws_instance resource block. That needs to be vpc_security_group_ids; that’s the one to use when creating an EC2 instance within a VPC.