Quantcast
Channel: Cloud Training Program
Viewing all articles
Browse latest Browse all 1903

AWS Virtual Machine Using AWS Console | Terraform AWS Deployment Overview

$
0
0

In this blog, we will see how to create an AWS Virtual machine in two different ways, one using the AWS console and the other using terraform.

Pre-requisites:

  1. AWS Free Tier or Paid account. Check how to create an AWS free tier account
  2. Terraform should be installed. Check how to install terraform

Terraform is one of the most popular Infrastructure-as-code (IaC) tool used by DevOps teams to automate infrastructure tasks. It is used to automate the provisioning of your cloud resources. It is an open-source, cloud-agnostic provisioning tool developed by HashiCorp and written in GO language. It is currently the best tool to automate your infrastructure creation.

It supports multiple providers such as AWS, Azure, Oracle, GCP, and many more. It makes provisioning a large number of resources a piece of cake and what better way to show it by using a demo.

Architecture

To give a brief about what demo we will be performing, please refer to the below Architecture

AWS Virtual Machine deployment architecture

As we can check from the above image that we will be deploying an AWS Virtual Machine, open a port to access the webpage running on the VM, and then access it in our browser.

Check Out:Terraform Tips and Tricks

Create VM in AWS Using Terraform

There are a series of steps that you need to follow to create an AWS Virtual Machine and deploy a webpage on top of it.

Now, below are the steps that you can follow to create the above-shown architecture:

1) Log in to your AWS account.

AWS Console

Read: Terraform Interview Questions

2) Go to EC2 from the navigation menu present under Compute service.

AWS Management Console

Read: Terraform Variables

3) Go to Instances from the navigation menu on the left and click on Launch instances.

Launching EC2 Instance

4) Search for Ubuntu and select Ubuntu Server 20.04 LTS (HTM).

Choosing AMI in EC2

5) Select Instance type as t2.micro and click on Next: Configure Instance Details.

Instance Types

Read: Terraform Workflow

6) On Configure Instance Details page, scroll down and add the following code in User data to provision the web page after the instance is provisioned. Click on 6. Configure Security Group to skip Storage and Tags.

User data in EC2

7) Add Custom TCP protocol to open 8080 port accessible from the internet and click on Review and Launch.

Configure Security Group in EC2

Also Check: Our blog post on Terraform Cheat Sheet. Click here

8) Review all the information and click on Launch.

Launch EC2 Instance9) When asked to select a key pair, select Proceed without a key pair and click on Launch Instances.

Key Pair Alert

Read: Terraform Install

10) Click on View Instance to check the status of the Instance.

Running Instance Details

11) Once the instance is up and running, copy the Public IP.

Running Instances

12) Paste the IP in your browser followed by port 8080 to access the created webpage.

Accessing web page

Check Out: Our blog post on Terraform Workflow. Click here

Terraform AWS Deployment

We will use the below code to provision AWS Virtual Machine in our account. You can download the code by clicking here.

Note: Replace Access Key and Secret Key with your own credentials.

provider "aws" {
  region = "us-east-1"
  access_key = "<YOUR ACCESS KEY HERE>"
  secret_key = "<YOUR SECRET KEY HERE>"
}
resource "aws_instance" "tfvm" {
  ami = "ami-0885b1f6bd170450c"
  instance_type = "t2.micro"
  vpc_security_group_ids = [ aws_security_group.websg.id ]
  user_data = <<-EOF
                #!/bin/bash
                echo "I LOVE TERRAFORM" > index.html
                nohup busybox httpd -f -p 8080 &
                EOF
    tags = {
      Name = "WEB-demo"
    }
}
resource "aws_security_group" "websg" {
  name = "web-sg01"
  ingress {
    protocol = "tcp"
    from_port = 8080
    to_port = 8080
    cidr_blocks = [ "0.0.0.0/0" ]
  }
}
output "instance_ips" {
  value = aws_instance.tfvm.public_ip
}

Also Read: Our blog post on Terraform Interview Questions. Click here

Steps to create the VM from the code:

1) Use terraform init command in terminal to initialize terraform and download the configuration files.

Initializing Terraform

2) Now let’s check for any human error in our script by using terraform plan command.

terraform plan

In the end, it will also show us the number of resources that will be added to our AWS account.

terraform plan

3) Now we will apply our code using terraform apply -auto-approve command. It will provide us the IP of our created VM.

terraform apply

Now, we can check if the Webpage has been created or not. Just copy the IP and check on 8080 port regarding the same.

Accessing Web Page

You can also check if the EC2 instance is created in the AWS by going to the EC2 dashboard.

EC2 created via Terraform

This is how we can create an AWS Virtual Machine using terraform.

From both the demos shown above, we can conclude how easy and fast it is to create an AWS Virtual Machine using terraform. This was just one AWS Virtual Machine that we created, now imagine creating hundreds of AWS Virtual Machines and other resources using both console and terraform.

Related/References

Join FREE Masterclass

Join our FREE Masterclass to know more about Terraform and get access to all Hands-On labs that you must perform to clear the Terraform Certified Associate certification exam.

Click on the below image to Register Our FREE Masterclass Now!

The post AWS Virtual Machine Using AWS Console | Terraform AWS Deployment Overview appeared first on Cloud Training Program.


Viewing all articles
Browse latest Browse all 1903

Trending Articles