Terraform with AWS

Terraform with AWS

Prerequisites:

Create a EC2 Machine terrafrom file using Visual Studio Code:

Now Need to open Visual studio code. and install the hashicorp terraform extension in your visual studio code after that create a folder that is terraform-project and go the terraform-project folder after that create a another folder terraform-files.

Now go the mouse in terraform-files and right click, open the open in integrated terminal.

Now check we have terraform or not.

Now need o go the AWS Console and go to the IAM service create a user and provide the Administrator access and create the access key (click here).

Now I hope you have aws cli in window and ubuntu in your laptop, If not please install Aws Cli in window and ubuntu (Click here).

Now go the Visual Studio Code and open the terminal and type aws --version , You can see we have aws cil.

Now need to type aws configure and provide to access key with the user how to provide the access key just go the my blog and check (cleck here)

Now need the create a provider.tf file.

provider.tf:

terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "5.44.0"
    }
  }
}

provider "aws" {
  region = "ap-south-1"
}

Now need to create a main.tf file need to create ec2 machine.

resource "aws_instance" "my_instances" {
  ami           = "ami-007020fd9c84e18c7"
  instance_type = "t2.micro"

  tags = {
    Name = "Terraform-Ec2-Machine"
  }
}

Now go the terminal and run the command terraform init

Now need to run the command terraform plan.

Now need to run the command terraform apply.

You need to go the the aws with ap-south-1 region and you can check automatically ec2 macine created by using terraform.

Now go the the visual studio code and create a ssh-keygen, Why we need to create because ec2 machine need to key to access this machine

Now open your terminal and type ssh-keygen

Now when you enter ssh file automatically created

Now you need to create a folder and upload both file terraform-key and terraform-key.pub.

Now go the main.tf add key resource. Now for pub key copy to terraform-key.pub.


resource "aws_key_pair" "my_key" {
  key_name   = "terraform-key"
  public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCq+bAE9RxTMzpagA/jPpTO+G5OaTXdTo43ugcJdzU2vK7qzXJdOIf/+Bb6ZYda5xqHwhN7KSvyUzgWN03nQeDVpW4pOJLHBIQ/3H8VCkHiTYUR9274o5UR0hrIcsU0+lBb0pXvCaXJ/uUAOW06z0K5DpK5+2jw++ib8DrDBG4t3X2LuYQ9Z7EaarASkyohyZ/ONRB//MD9oCcBina3kt8SXAVDudRLYHi51m3v0IGl73YG5bRZgXmial6yO/SSkLeDrnW5NnuwPfE11I+SXNMDlKmTBNsLa5dAeRp2EzbKeYXVcXa4BgnDsFlmM1Z5jTexRqDYAC6JW2aWwIBualsakzkRgw2xmjMY86XjVX6h2L+p2pHFFT3yZNekg/6lEJmZlNZcGSjTdStBTPQm2csPBXkIQ/vZpCviPGiFJA/f7Oj25xGcpWh60m8xo4EoBHmhkXewQBBd+dED0snNx1tZ77TsNxiCLd7AXLaRlUraZ/BqgjJ86C6/LFaPvMmfw/E= udayyysharma@udayyysharma-HP-Laptop-15s-fq5xxx"
}
resource "aws_instance" "my_instances" {
  ami           = "ami-007020fd9c84e18c7"
  instance_type = "t2.micro"

  tags = {
    Name = "Terraform-Ec2-Machine"
  }
}

Now go the visual studio terminal and type the terraform init.

Now need to run terraform plan command

Now need to run terraform apply command.

Now go the aws and check we have ec2 machine with key that is terraform key

Now need to create security group so go the the main.tf and add resources.


resource "aws_default_vpc" "default" {

}


 resource "aws_security_group" "my_security_gorup" {
  vpc_id = aws_default_vpc.default.id
  name = "my-proper-security"

  ingress {
    description = "To connect ssh"
    from_port = 22
    to_port   = 22
    protocol = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  ingress {
    description = "To connect HTTP"
    from_port = 80
    to_port   = 80
    protocol = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  ingress {
    description =  "To Connect HTTPS"
    from_port = 443
    to_port   = 443
    protocol = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  egress {
    description = "To connect out side traffic"
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_key_pair" "my_key" {
  key_name   = "terraform-key"
  public_key = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQCq+bAE9RxTMzpagA/jPpTO+G5OaTXdTo43ugcJdzU2vK7qzXJdOIf/+Bb6ZYda5xqHwhN7KSvyUzgWN03nQeDVpW4pOJLHBIQ/3H8VCkHiTYUR9274o5UR0hrIcsU0+lBb0pXvCaXJ/uUAOW06z0K5DpK5+2jw++ib8DrDBG4t3X2LuYQ9Z7EaarASkyohyZ/ONRB//MD9oCcBina3kt8SXAVDudRLYHi51m3v0IGl73YG5bRZgXmial6yO/SSkLeDrnW5NnuwPfE11I+SXNMDlKmTBNsLa5dAeRp2EzbKeYXVcXa4BgnDsFlmM1Z5jTexRqDYAC6JW2aWwIBualsakzkRgw2xmjMY86XjVX6h2L+p2pHFFT3yZNekg/6lEJmZlNZcGSjTdStBTPQm2csPBXkIQ/vZpCviPGiFJA/f7Oj25xGcpWh60m8xo4EoBHmhkXewQBBd+dED0snNx1tZ77TsNxiCLd7AXLaRlUraZ/BqgjJ86C6/LFaPvMmfw/E= udayyysharma@udayyysharma-HP-Laptop-15s-fq5xxx"
}
resource "aws_instance" "my_instances" {
  ami           = "ami-007020fd9c84e18c7"
  instance_type = "t2.micro"
  key_name = aws_key_pair.my_key.key_name
  security_groups = [aws_security_group.my_security_gorup.name]

  tags = {
    Name = "Terraform-Ec2-Machine"
  }
}

Now need to run the command terraform init,terraform plan and terraform apply from your terminal.

Now go the aws ec2 machine and connect the machine.

Now click to connect button.

Now click to connect.

Now you can see we have connect the ec2 machine successfully.