Terraform Lab – Install, First scripts, Build infra on AWS

Author : Ahmed Raidhan

Introduction

This is an MFH Bitesize Lab, the aim of this document is to give the reader a quick and direct demonstration of the TERRAFORM technology. This is to supplement the online teaching sessions and to provide students with an online hands-on experience of taught technologies.

The Aim

At the end of this document, students would have:

  • Installed Terraform
  • Written and Executed some terraform scripts
  • Publish a Vm on AWS using Terraform
  • Built experience with IDE.

Terraform Installation

Pre -reqs

Windows PC – that you have admin rights to

Connected to the internet

AWS Account setup – with user access key

Terrform install

Goto to https://www.terraform.io/downloads – Download the correct Terraform zip file for your environment.

Extract Zip file to C:\Terraform – In c:\Terraform the file terraform.exe should be there!

Edit your Windows Environment variables and add c:\terraform to the path variable

                ATTENTION !!!!! Do not remove what is already there, only append

Installation Check

On the Command Prompt – terraform version

You should get the following:

 This indicates Terraform is installed successfully.

My First Terraform Scripts

Directory Setup

Mkdir terraform-scripts

Inside terraform-scripts mkdir Proj1

Create Scripts

Inside Proj1 create the two following scripts:

Main.tf

variable “myvariable” {
  type = string
  default = “Welome to Terraform”
}

Main2.tf

variable "myvariable1" {
  type = string
  default = "Welome to Terraform again"
}

Excute first script

At the command prompt, type: (inside Proj1 folder)

Terraform console

This takes you into terraform console and the following:

“Welcome to Terraform” should be displayed.

The type the following:

A screenshot of a computer

Description automatically generated with low confidence

“Welcome to Terraform again” should be displayed.

ATTENTION – This shows that terraform compiles all tf files in a folder!

My Second Terraform Script

Create Directory

Mkdir AWS-PROJ1

Create Scripts (inside AWS-PROJ1)

Create aws-connection.tf

provider "aws" {
  access_key = "GET AWS ACCESS KEY"
  secret_key = "GET AWS SECRET KEY"
  region = "us-east-1"
}

Create aws_instance.tf

resource "aws_instance" "Ubuntu" {
  ami = "ami-08d4ac5b634553e16"
  instance_type = "t2.micro"
  tags = {
    Name = "AWS_Ubuntu_box1"
  }
}

Create AWS VM

Inside the AWS-PROJ1 directory, type the following:

 Terraform init (output should be live below:

Text

Description automatically generated

Terraform deploy

This will connect to AWS and create the VM AWS_Ubuntu_box1.

Check instances on AWS.