Terraform Providers

Terraform Providers

What are Terraform Providers?

  • As a developer, you might have worked with APIs before (but I didn't. Trust me🥲),

  • But with Terraform providers, you can easily configure and set up infrastructure without needing to write complex API calls.

  • Providers are like entities which are downloaded or installed all the necessary plugins which are required in order to interact with the cloud platforms once you perform terraform init command automatically.

  • Terraform is a powerful tool for managing resources across various cloud platforms.

  • By using it, developers can write code in a declarative way that describes the desired state of their infrastructure, and the tool handles the provisioning and configuring resources part on various cloud platforms like AWS, Azure, GCP, etc.

  • Terraform becomes like a Swiss Army Knife🔪 for cloud infrastructure, allowing developers to easily manage complex deployments without having to dive deep into the inner workings of each service's API.

  • It's like having a handy assistant that can talk to different cloud services on your behalf so you can focus on what really matters, building great software.

Example

Let's say you want to create a virtual machine on AWS. You would specify the AWS provider in your Terraform configuration, and then in your code, you would simply declare the specific resources (in this case, a virtual machine) you want to create.

Terraform then takes care of making the API calls to AWS to create the VM for you, without you needing to worry about how to specifically write those API calls.

Overall, Terraform providers make it simpler for developers to create and manage the infrastructure while abstracting away much of the complexity involved in interacting with APIs and different cloud services.

Comparing the Features, Services & Resources of T-P for various cloud platform

Terraform supports several cloud providers, including AWS (Amazon Web Services), Azure, Google Cloud Platform (GCP), and DigitalOcean, among others.

Click on the picture👇 to see all the cloud platforms supported by Terraform.

# Just for the sake of writing. Sorry:)
$ alias t-p="terraform provider"

AWS Provider

Let's start with AWS!

Till now we have seen many services created in Terraform like EC2 instances, S3 buckets, DynamoDB tables, etc.

The AWS provider for Terraform allows you to manage a variety of AWS resources such as EC2 instances, RDS databases, ELBs, and much more.

One of the key features of the AWS provider is Autoscaling, which allows you to define autoscaling groups and automatically scales resources based on predefined metrics.

Example

terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 4.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = "us-east-1"
}

# Create a VPC
resource "aws_vpc" "example" {
  cidr_block = "10.0.0.0/16"
}

Configuration and Authentication

  • Adding an access_key, secret_key, and optionally token, to the aws provider block.

      provider "aws" {
        region     = "us-west-2"
        access_key = "my-access-key"
        secret_key = "my-secret-key"
      }
    
  • Adding AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, and AWS_SESSION_TOKEN environment variables. The region can be set using the AWS_REGION or AWS_DEFAULT_REGION environment variables.

      $ export AWS_ACCESS_KEY_ID="anaccesskey"
      $ export AWS_SECRET_ACCESS_KEY="asecretkey"
      $ export AWS_REGION="us-west-2"
    

    For more detail check out here👇

Azure Provider

Now coming to Azure,

The Azure provider for Terraform is designed to manage Azure resources, including virtual machines, storage accounts, databases, and much more.

The provider follows the same resource model of Azure which makes it easy to map Terraform configurations to Azure resources.

The Azure provider allows us to do Multi-Factor authentication and RBAC to secure access to Azure resources.

# Strongly recommend using the required_providers block to set the
# Azure Provider source and version being used
terraform {
  required_providers {
    azurerm = {
      source  = "hashicorp/azurerm"
      version = "=3.0.0"
    }
  }
}

# Configure the Microsoft Azure Provider
provider "azurerm" {
  features {}
}

# Create a resource group
resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "West Europe"
}

# Create a virtual network within the resource group
resource "azurerm_virtual_network" "example" {
  name                = "example-network"
  resource_group_name = azurerm_resource_group.example.name
  location            = azurerm_resource_group.example.location
  address_space       = ["10.0.0.0/16"]
}

GCP Provider

Moving on to Google Cloud Platform.

The GCP provider allows you to manage resources in the Google Cloud environment, including Compute Engine instances, Google Kubernetes Engine clusters, and much more.

With the help of the GCP provider, you can provision and create machine learning models leveraging services such as AutoML, BigQuery, or TensorFlow.

Example

provider "google" {
  project     = "my-project-id"
  region      = "us-central1"
}

Digital Ocean Provider

Lastly, we have Digital Ocean's Terraform provider,

It offers features like Kubernetes cluster management with DOKS (DigitalOcean Kubernetes Service), the ability to provision load balancers, and health checks for droplets.

For more details and to start it for free click here.

Overall, all Terraform providers aim to provide comprehensive coverage to manage cloud resources in a consistent and declarative way.

P.S. I used AWS to create terraform providers, and didn't go through all the platforms. The above note is collected from various resources including official documentation of terraform. So don't quote me on this. Thank you:)🙂

Practice T-P on AWS

Now it's time to gain some hands-on of it. Yes, obviously we are going to do it in the AWS cloud platform because till now we've done everything on it😅.

  1. Log in to the AWS console.

  2. Create an EC2 instance and connect it to your local machine by doing ssh.

  3. Now copy that Public IP address.

  4. Go to the IAM dashboard, now here can be three scenarios

    a) You have a user and access key already created.

    • If this is the case, then go directly to step 5.

b) You have a user but not an access key (which means, you forgot to download the credentials while creating it)

  • If this is the case then, click on "Create Access key" and get your credentials.

c) No user, no key.

  • If this is the key, then click on the "Add User" button and perform all the followed up steps in order to generate one.
  1. Go to your terminal and run,

     $ ssh -i /<key-pair-path> ubuntu@<ip-add>
    

    To connect the instance to the local machine.

  2. Now make a directory using mkdir cmd.

     $ mkdir terraform-provider
     $ cd terraform-provider
    
  3. Now create a file in which we will define the providers.

     $ vim provider.tf
    
     # Authentication using access key
    
     provider "aws" {
         access_key = "AKIAZDXMNSUCC64NNYJQ"
         secret_key = "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"
         region = "us-east-1"
     }
    
  4. Now create another file so that we can perform some actions.

     $ vim resource.tf
    
     resource "aws_instance" "practice" {
         ami = "ami-053b0d53c279acc90"
         instance_type = "t2.micro" # free tier
         security_groups = ["default"]
         key_name = "my-key-pair"
         tags = {
             Name = "PracticeInstance"
         }
     }
    
  5. Now run the init command to initialize in order to download and install all the plugins required to run AWS.

     $ terraform init
    

  6. Now run terraform plan command to see the architecture of our configurations.

    $ terraform plan
    

  7. Now to check the syntax of everything, run

    $ terraform validate
    

  8. Now finally to apply and see your changes visually, run

    $ terraform apply
    

  9. Now go to the AWS console in the EC2 instance dashboard, you can see there the instance has been created and running.

    I am unable to put a screenshot here because I terraform destroy without taking ss. Sorry!

    But I have done a recording of the whole process, you can check here if you wish to.

  10. Now, we have to run terraform destroy. It's a good practice to do so, it will save you from any accident.🚨💸

    $ terraform destroy
    

Okay! So, we have created a service by using Terraform Providers..


This blog is a part of the 7-day #TerraWeek Challenge initiated by Shubham Londhe sir!

Thank you!🖤