1. aws.tf
Provider tells Terraform which service it’s going to use to set up resources
ec2 instance is a resource which is maintained by amazone.
So, basically it establishes connections.

provider "aws" {
    region = "${var.aws_region}"
}

2. vpc.tf
VPC: Allows secretly launch/access your aws resources/services.
It provides virtual space where all your aws services/resources can be present at one place and providing complete control.
AWS has default VPC, here we are creating a new VPC name vnetwork

 resource "aws_vpc" "vnetwork" {
  cidr_block = "10.0.0.0/16"
  enable_dns_hostnames = true
  enable_dns_support = true
  tags = {
    Name = "vnetwork"
  }
}

3. vars.tf
determine the OS
-ami_name: used as a tag
-ami_id: to find the instance to be launched
-ami_key_pair_name: used to input the key name while creating the EC2 instance.

 variable "aws_access_key" {}
 variable "aws_secret_key" {}
 variable "aws_region" {}
 variable "ami_name" {}
 variable "ami_id" {}
 variable "ami_key_pair_name" {}

4.subnets.tf
vpc_id is used to attach the subnet to the VPC,previously created

 resource "aws_subnet" "subnet-uno" {
  cidr_block = "${cidrsubnet(aws_vpc.test-env.cidr_block, 3, 1)}"
  vpc_id = "${aws_vpc.test-env.id}"
  availability_zone = "us-east-1a"
}

5. security.tf

 resource "aws_security_group" "ingress-all-test" {
name = "allow-all-sg"
vpc_id = "${aws_vpc.vnetwork.id}"
ingress {
    cidr_blocks = [
      "0.0.0.0/0"
    ]
from_port = 22
    to_port = 22
    protocol = "tcp"
  }
// Terraform removes the default rule
  egress {
   from_port = 0
   to_port = 0
   protocol = "-1"
   cidr_blocks = ["0.0.0.0/0"]
 }
}

6. instance.tf

 resource "aws_instance" "test-ec2-instance" {
  ami = "${var.ami_id}"
  instance_type = "t2.micro"
  security_groups = ["${aws_security_group.ingress-all-test.id}"]
  tags = {
    Name = "${var.ami_name}"
  }
 subnet_id = "${aws_subnet.subnet-uno.id}"
 }

7. update vpc.tf:

 resource "aws_vpc" "vnetwork" {
  cidr_block = "10.0.0.0/16"
  enable_dns_hostnames = true
  enable_dns_support = true
  tags = {
    Name = "vnetwork"
  }
}
resource "aws_eip" "ip-vnetwork" {
  instance = "${aws_instance.test-ec2-instance.id}"
  vpc      = true
}

8. gateway.tf

 resource "aws_internet_gateway" "vnetwork-gw" {
  vpc_id = "${aws_vpc.vnetwork.id}"
tags = {
    Name = "vnetwork-gw"
  }
}

9. Update - subnets.tf

 resource "aws_subnet" "subnet-uno" {
  cidr_block = "${cidrsubnet(aws_vpc.vnetwork.cidr_block, 3, 1)}"
  vpc_id = "${aws_vpc.vnetwork.id}"
  availability_zone = "us-east-1a"
}
resource "aws_route_table" "route-table-vnetwork" {
  vpc_id = "${aws_vpc.vnetwork.id}"
route {
    cidr_block = "0.0.0.0/0"
    gateway_id = "${aws_internet_gateway.vnetwork-gw.id}"
  }
tags = {
    Name = "vnetwork-route-table"
  }
}
resource "aws_route_table_association" "subnet-association" {
  subnet_id      = "${aws_subnet.subnet-uno.id}"
  route_table_id = "${aws_route_table.route-table-vnetwork.id}"
}