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:
- AWS Free Tier or Paid account. Check how to create an AWS free tier account
- 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
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.
Read: Terraform Interview Questions
2) Go to EC2 from the navigation menu present under Compute service.
Read: Terraform Variables
3) Go to Instances from the navigation menu on the left and click on Launch instances.
4) Search for Ubuntu and select Ubuntu Server 20.04 LTS (HTM).
5) Select Instance type as t2.micro and click on Next: Configure Instance Details.
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.
7) Add Custom TCP protocol to open 8080 port accessible from the internet and click on Review and Launch.
Also Check: Our blog post on Terraform Cheat Sheet. Click here
8) Review all the information and click on Launch.
9) When asked to select a key pair, select Proceed without a key pair and click on Launch Instances.
Read: Terraform Install
10) Click on View Instance to check the status of the Instance.
11) Once the instance is up and running, copy the Public IP.
12) Paste the IP in your browser followed by port 8080 to access the created webpage.
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.
2) Now let’s check for any human error in our script by using terraform plan command.
In the end, it will also show us the number of resources that will be added to our AWS account.
3) Now we will apply our code using terraform apply -auto-approve command. It will provide us the IP of our created VM.
Now, we can check if the Webpage has been created or not. Just copy the IP and check on 8080 port regarding the same.
You can also check if the EC2 instance is created in the AWS by going to the EC2 dashboard.
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
- Terraform Installation Overview
- HashiCorp Infrastructure Automation Certification: Terraform Associate
- Resource Manager In Oracle Cloud Infrastructure
- HashiCorp Certified Terraform Associate
- How to Provision AWS Infrastructure with Terraform?
- Terraform Cheat Sheet
- Why Choose Terraform Over Chef, Ansible, Puppet or CloudFormation?
Join FREE Class
🚀 Master Terraform & DevOps to get High-Paying Jobs! 🔥 Join our EXCLUSIVE Free class! 🚀
Get your hands dirty with lots of projects and labs based on Terraform and DevOps in our Program.Click on the below image to Register for Our FREE Class Now!
Leave a Reply