How to install AWS EC2 instance using Terraform

How to install AWS EC2 instance using Terraform

Terraform is an open source ‘infrastructure as code’ command line tool used to manage infrastructure in the cloud.

Terraform is supported by all major cloud providers like Amazon Cloud, Google Cloud, Alibaba Cloud and Microsoft Azure Cloud.

This article will cover the installation of Terraform on Ubuntu 20.04 LTS system and the launching AWS EC2 instance using Terraform.

Step 1: Installation of Terraform on Ubuntu 20.04

1: Run the following wget command.

carbon (30).png

2: Now, unzip the downloaded file.

carbon (31).png

3: This will output you a terraform file just move it to /usr/local/bin/ to execute the command.

carbon (32).png

Step 2: Launching AWS EC2 Instance Using Terraform

1: Let’s make a directory and configure Terraform inside it. Run following commands.

carbon (33).png

2: Now, make a configuration file. I am giving here the name as config.tf . You can give name as per your choice but remember the extension must be ‘tf’.

carbon (34).png

3: Add the following terms provider AWS, your access key, secret key and region where you are going to launch ec2 instance. Here, I am going to use my favorite Singapore region.

On the second block of the code define resource as ‘aws_instance’, ami (I have picked ami from Ubuntu Server 20.04 LTS. Give a instance type and also a tag of your choice.

carbon (35).png

4; Save & close the file.

Step 3: Now, initialize your configuration by executing terraform command.

1. First, initialize Terraform project, and pull all the necessary dependencies

carbon (36).png

2. Once Terraform has initialized, see what is going to happen by executing command.

carbon (37).png

3. Now, execute your terraform code.

carbon (38).png

Type yes, and within 2 minutes your AWS resources will be allocated. If you go to your AWS console, you will see that EC2 instances is up and running.

It’s simple and easy to provision infrastructure in cloud using the terraform. Hope you like the article. If you found any difficulty, please do comment me. Thank You:)