How to provision cloud resources using Terraform

How to provision cloud resources using Terraform

So if you have ever created a virtual machine(or any other resources) using any of the public cloud providers you how difficult it is to go to their console fill in a ton of details go back and forth to provision your resources. So this is one of the many problems that Terraform fixes. It's a cool tool that saves a lot of time and helps people provision resources in the cloud easily and quickly.

So first of all let's understand what is terraform,

Terraform is a tool that helps you manage your computer infrastructure in a more efficient and automated way.

When you want to create some computer resources like servers, storage or networks, you can use Terraform to write down what you want in a special language called "Infrastructure as Code" (IaC). You can specify things like the size and number of servers you need, the type of storage you want, and how you want them all connected.

Once you've written down your specifications, Terraform will automatically create and configure those resources for you in the cloud. And if you ever need to make changes, you can simply update your specifications and Terraform will take care of the rest.

With Terraform, you can manage your infrastructure more easily, consistently and reliably, all while saving time and reducing the risk of errors.

Terraform sends the data entered by the user via the respective API of the Target cloud provider and based on that data the cloud provider provisions the required resources.

Now let's see an example of how to use Terraform to create an AWS EC2 instance:

So, the code that we are about to write is written in a declarative language called HashiCorp Configuration Language (HCL).

Some of the points to remember are:

  1. First, you need to install Terraform on your computer to do that follow these steps:
  • Go to the official Terraform website: terraform.io/downloads.html

  • Download the appropriate installer package for your operating system.

  • Extract the downloaded package to a directory on your computer.

  • Add the Terraform binary file to your system's PATH. For example, on Linux or macOS, you can add the following line to your ~/.bashrc or ~/.zshrc file:

    export PATH="$PATH:/path/to/terraform"

  • Replace /path/to/terraform with the actual path to the Terraform binary file.

  • Save and close the file, then run the following command to reload your shell:

  •       source ~/.bashrc
    
  • To verify that Terraform is installed correctly, run the following command:

  •       terraform --version
    
  • This should display the version number of Terraform that you installed.

  1. Next, create a new directory for your project and navigate into it.

  2. Next, create a new directory for your project and navigate into it.

  3. Create a new file called main.tf and add the following code:

  4.       provider "aws" {
            region = "us-west-2"
          access_key = ""
          secret_key = ""
          }
    
          resource "aws_instance" "example" {
            ami           = "ami-0c55b159cbfafe1f0"
            instance_type = "t2.micro"
            tags = {
              Name = "example-instance"
            }
          }
    

    This code tells Terraform to use the AWS provider, sets the region to us-west-2, and creates a new EC2 instance with the ami id and instance_type specified. It also adds a tag to the instance with the name "example-instance".

    Here the access_key and secret_key is a unique key generated for a cloud account in this case AWS. And it can be accessed and generated from the Security Credentials section of the AWS console.

  5. Next, run terraform init to initialize your project and download the necessary plugins.

  6. Run terraform plan to see what Terraform is going to do.

  7. Finally, run terraform apply to create the resources.

Terraform will create the EC2 instance for you in AWS with the specifications you defined in the code.

Hope that this blog was helpful for you to understand what is Terraform and how to allocate cloud resources using Terraform. Thank You!