How to Setup Terraform step by step on ubuntu machine

You might have worked with various Cloud Provider such as AWS or Azure or Oracle cloud. Every cloud provider provides you accounts to setup your infrastructure. If you work with lots of account it may require lots of re work such as creating AWS RDS on 10 different accounts. It will take tons of hour and efforts to build that.

There are lots of automation tools and scripts available for this and one of the finest tool to automate your infrastructure is Terraform which is also known as Infrastructure as code.

In this tutorial you’ll install and use Terraform on Ubuntu 18.04. You’ll then work with Terraform to create a service on Amazon Managed Service. So lets get started.

Table of Content

  • What is Terraform?
  • Prerequisites
  • How to Install Terraform on Ubuntu 18.04 LTS
  • How to setup terraform configuration files and run terraform code?
  • Conclusion

What is Terraform?

Terraform is a tool for building , versioning and changing the infrastructure. Terraform is Written in GO Language and the syntax language of configuration files is hcl which stands for HashiCorp configuration language which is much easier than yaml or json.

Terraform has been in use for quite a while now . I would say its an amazing tool to build , change the infrastructure in very effective and simpler way. It’s used with variety of cloud provider such as Amazon AWS, Oracle, Microsoft Azure , Google cloud and many more. I hope you would love to learn it and utilize it.

Prerequisites

  • Ubuntu machine preferably 18.04 version + , if you don’t have any machine you can create a ec2 instance on AWS account
  • Recommended to have 4GB RAM
  • At least 5GB of drive space
  • Ubuntu machine should have IAM role attached with AWS EC2 instance creation which we will use later in tutorial using terraform

You may incur a small charge for creating an EC2 instance on Amazon Managed Web Service.

How to Install Terraform on Ubuntu 18.04 LTS

  • Update your already existing system packages.
sudo apt update
  • Download the latest version of terraform in opt directory
wget https://releases.hashicorp.com/terraform/0.14.8/terraform_0.14.8_linux_amd64.zip
  • Install zip package which will be required to unzip
sudo apt-get install zip -y
  • unzip the Terraform download zip file
unzip terraform*.zip
  • Move the executable to executable directory
sudo mv terraform /usr/local/bin
  • Verify the terraform by checking terraform command and version of terraform
terraform               # To check if terraform is installed 

terraform -version      # To check the terraform version  
  • This confirms that terraform has been successfully installed on ubuntu 18.04 machine.

How to Setup terraform configuration files and run terraform code

  • Code in terraform language which is hasicorp language is stored in .tf format files or .tf.json and these are also known as configuration files.
  • This tutorial is get you started in setting up terraform configuration files and run terraform code we. Let us first understand terraform configuration files before running Terraform commands.
    • main.tf : This file contains code that create or import other AWS resources.
    • vars.tf : This file defines variable types and optionally set the values.
    • output.tf: This file helps in generating of the output of AWS resources .The output is generated after the terraform apply command is executed.
    • terraform.tfvars: This file contains the actual values of variables which we created in vars.tf
    • provider.tf: This file is very important . You need to provide the details of providers such as AWS , Oracle or Google etc. so that terraform can make the communication with the same provider and then work with resources.

By Now, you have understanding of all the terraform configurations files , Lets now all these files one by one to provision AWS EC2 instance.

  • Create a folder in opt directory and name it as terraform-demo 
mkdir /opt/terraform-demo
cd /opt/terraform-demo
  • Create main.tf file under terraform-demo folder and paste the content below.


resource "aws_instance" "my-machine" {          # This is Resource block where we define what we need to create

  ami = var.ami                                 # ami is required as we need ami in order to create an instance
  instance_type = var.instance_type             # Similarly we need instance_type
}
  • Create vars.tf file under terraform-demo folder and paste the content below
variable "ami" {                       # We are declaring the variable ami here which we used in main.tf
  type = string      
}

variable "instance_type" {             # We are declaring the variable instance_type here which we used in main.tf
  type = string 
}
  • Create output.tf file under terraform-demo folder and paste the content below.
output "ec2_arn" {
  value = aws_instance.my-machine.arn     # Value depends on resource name and type ( same as that of main.tf)
}  
  • Create provider.tf file under terraform-demo folder and paste the content below.
provider "aws" {      # Defining the Provider Amazon  as we need to run this on AWS   
  region = "us-east-1"
}
  • Create terraform.tfvars file under terraform-demo folder and paste the content below.
ami = "ami-013f17f36f8b1fefb" 
instance_type = "t2.micro"
  • Now your files and code are ready for execution . Initialize the terraform using the init command
terraform init
  • Terraform initialized successfully , now its time to see the plan which is kind of blueprint before deployment. We generally use plan to confirm if correct resources is going to provisioned or deleted.
terraform plan
  • After verification , now its time to actually deploy the code using apply.
terraform apply

Great Job, terraform commands execution was done successfully. Now we should have ec2 instance launched in AWS.

It generally take a minute or so to launch a instance and yes we can see that the instance is successfully launched now in us-east-1 region as expected.

Conclusion

In this tutorial you learnt What is terraform , how to Install and Setup Terraform on Ubuntu Machine and launched an ec2 instance on AWS account using terraform. Keep Terraforming !!

Hope this tutorial will helps you in understanding and setting up Terraform on ubuntu machine. Please share with your friends.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s