Created and maintained by Hashicorp, Terraform is an open-source utility tool that is popularly regarded as Infrastructure As A Code. It allows users to easily build and provision cloud resources such as virtual machines on various popular cloud platforms such as Google Cloud and AWS. Let’s now install Terraform. This guide works on both Ubuntu 18.04 (Bionic Beaver) and Ubuntu 20.04 (Focal Fossa).
Download and Install Terraform
Firstly, we need to download the Terraform compressed file from Terraform’s download page. Terraform 0.14.6 is the most recent version by the time of publishing this tutorial.
To download Terraform, use the wget command to grab the Linux installation zipped file
$ sudo wget https://releases.hashicorp.com/terraform/0.14.6/terraform_0.14.6_linux_amd64.zip
Once the download is complete, confirm that the zipped file exists using the ls command as shown.
$ ls | grep terraform
Next, unzip the file to the /usr/local/bin path where non-system programs are usually located.
$ sudo unzip terraform_0.14.6_linux_amd64.zip -d /usr/local/bin
Once uncompressed, the installation of Terraform is complete. To verify the version of Terraform installed issue the command”
$ terraform version
Perfect! Let’s now see Terraform in action. We are going to demonstrate how we can use Terraform to provision an ec2 instance on AWS cloud platform
Use Terraform to launch an EC2 instance on AWS
As we mentioned earlier, Terraform can be used to deploy on-premise and cloud resources on popular cloud platforms such as AWS and Google cloud to mention a few.
To start off, we are going to create a separate directory for our AWS project and navigate into it.
$ mkdir aws && cd aws
We will create a Terraform configuration file bearing a .tf extension.
$ sudo vim config.tf
Now, here comes the most important part. We are going to define details about our cloud provide – In this case AWS.
- Cloud provider’s name – In this case, AWS.
- Access key & secret keys – These are encrypted keys that provide access to our AWS resources
- Region – the location where the cloud infrastructure will be provisioned. In our case, this will be in us-east-2
- ami – This is a unique ID required to deploy an EC2 instance.
Copy and paste the following content
#Specify the access & secret keys and region provider "aws" { access_key = "Your-own-access-key" secret_key = "Your-own-secret-key" region = "us-east-2" } #Specify details of the ec2 instance resource "aws_instance" "instance1" { ami = "ami-0dd9f0e7df0f0a138" instance_type = "t2.micro" tags = { Name = "ubuntu-20.04" } }
To start working with Terraform and running command, we are going to initialize it as shown.
$ terraform init
To perform a simulation of how the command will run, invoke the terraform plan command as shown. Note that this is only a simulation that is meant to see how the whole thing will play out.
$ terraform plan
Finally, to provision the ec2 instance, run:
$ terraform apply
A flurry of information will be printed on the terminal displaying the various actions that will be performed by Terraform as shown.
When prompted whether to proceed with the execution of the actions, simply type ‘Yes’ and hit ENTER.
Within a few seconds, the creation of your ec2 instance will be complete and if all went well you should get the output below.
And just to sure that your instance was created, head over to our AWS account and navigate to the EC2 instances Below is a screenshot of the newly created ec2 instance.
This draws the curtains on this guide. We have walked you through the installation of Terraform and demonstrated its effectiveness in provisioning cloud resources.