Create EC2 Instance using Terraform

Create EC2 Instance using Terraform

·

3 min read

So, basically in this project, we are going to use Terraform as Infrastructure as Code and write a Terraform file to build an EC2 Instance on AWS. We can also do this using the AWS console, but for automation, we will use the AWS CLI and Terraform.

Prerequisites:-

AWS CLI: Since we are going to create the Virtual Network on the AWS platform, we will use the AWS CLI on our system. You can follow the official documentation for the installation of AWS CLI.

Once you have installed the AWS CLI, you can configure it by running the following command in a terminal window:

aws configure

This will prompt you to enter your AWS access key ID, secret access key, default region, and default output format. You can obtain your access key ID and secret access key from the AWS Management Console by navigating to the security credential section once logged in or creating a new one from there if needed.

Terraform: As we are using Terraform for infrastructure as code, we need to install it on our local system so that we can use Terraform commands on it.

Use the following commands to install Terraform in your local system:

sudo apt-get update
sudo apt-get install -y gnupg software-properties-common

Now, We will create a new folder in our local directory and we will write our terraform files in that directory only.

mkdir terraform-proj

cd terraform-proj

Now open this folder on your favourite code editor and create a new file provider.tf in that folder

So, the provider.tf file will be used to declare the provider we are using in our project, as there are different cloud providers like Azure, AWS, GCP, etc. In our project, we are going to use AWS, and we also need to declare the region, version, and some other parameters in the provider.tf file only.

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

provider "aws" {
  # Configuration options
  region = "us-east-1"
}

Now we will create a new file main.tf in which we will write the actual that will be going to run when we give the command Terraform apply.

Firstly we will create an AWS EC2 Instance on our main. tf file

resource "aws_instance" "example_server" {
  ami           = "ami-0261755bbcb8c4a84"
  instance_type = "t2.micro"

  tags = {
    Name = "KetanBlogExample"
  }
}

ami - AMI to use for the instance. Required unless launch_template is specified and the Launch Template specifies an AMI. If an AMI is specified in the Launch Template, setting ami will override the AMI specified in the Launch Template.

instance_type - Instance type to use for the instance. Required unless launch_template is specified and the Launch Template specifies an instance type. If an instance type is specified in the Launch Template,

tags - Map of tags to assign to the resource. Note that these tags apply to the instance and do not block storage devices. If configured with a provider default_tags configuration block present, tags with matching keys will overwrite those defined at the provider level

Now save all the files and after that use the command :

terraform init

This command is used to initialize terraform in your project and after you apply this command you will see some configured files in your directory.

now use the command:

terraform plan

This command is used to check if there is any error in your code.

Now at last we will use:

terraform apply

After this login to your AWS console, you will see EC2 instance will be created in your AWS account.

That's a wrap..............