The Ops Community

Cover image for Let's Terraform It : Pt 1 -> Providers & Resources
Jatin Mehrotra
Jatin Mehrotra

Posted on • Updated on

Let's Terraform It : Pt 1 -> Providers & Resources

Terraform as an Infrastructure as Code is great because of its fast learning curve and simple and intuitive HashiCorp Configuration Language (HCL).

I love terraform because it has support for 2171 providers ( AWS, Azure, Github etc...), a rich community due to its popularity, great plugin support and flexible integration with configuration management tools like ansible.

I will try to post a series of blogs around terraform of course starting from 100 level to 200 level, which will not help to understand terraform fundamentally but also help to clear terraform associate level.

Prerequisites

  • AWS Account
  • IDE (Visual Studio Code)
  • Access to Terminal

What is a provider?

  • There are many articles for starting with terraform create ec2 instance with terraform but it is very important to know what happening behind the scenes than just writing some code.( This BTS knowledge will help you to solve and troubleshoot any kind of problem ).
provider "aws" {
  region     = "us-west-2"
  access_key = "your-access-key"
  secret_key = "your-secret-key"
}

resource "aws_instance" "myFirstEc2Instance" {
  ami           = "ami-0ca285d4c2cda3300"
  instance_type = "t2.micro"
}
Enter fullscreen mode Exit fullscreen mode
  • We used provider block to tell terraform that we want to interact and create infrastructure for AWS.
  • Let's name the above-mentioned code as ec2.tf, this wont be enough because terraform.
  • As a next step we use terraform init on the command line which terraform uses to download plugins for the AWS provider in order to interact with AWS services. terraform init terraoform aws plugin
  • We can clearly see how terraform created a .terraform directory to store the AWS provider plugins.

  • List of Terraform providers

Note:- Terraform init also results in the creation of the .terraform.lock.hcl file, which keeps track of the provider version. Every time you add a new provider, this lock file needs to be updated which means terraform init needs to be run again.

What are resources?

  • Resources are the way by which terraform allows interacting with different services based on the provider block we have chosen.
  • For aws provider, terraform provides all the equivalent resources for the services AWS provides for example Ec2, EBS, and Dynamodb.

  • List of AWS Services which has sub resources

  • Under Resources, some parameters will be optional and some will be required.

AWS EC2 Resource

Note:- From terraform 0.13 terraform recommends to add terraform block. This is not mandatory for terraform managed provider like aws but for providers which are not managed by terraform it is mandatory.

  • optional ( aws is terraform managed provider but recommended )
terraform {
  required_providers {
    aws = {
      source = "hashicorp/aws"
      version = "4.17.1"
    }
  }
}

provider "aws" {
  # Configuration options
}
Enter fullscreen mode Exit fullscreen mode
  • mandatory ( for providers not managed by terraform provider)
terraform {
  required_providers {
    github = {
      source  = "integrations/github"
      version = "4.25.0-alpha"
    }
  }
}
Enter fullscreen mode Exit fullscreen mode

From a DevOps Perspective

  • If you understand resources and block with one provider, you can apply the same knowledge to other providers by referring to that provider resource's documentation.

Challenge

  • Using the knowledge of Resources and providers, try creating a GitHub repository using terraform.

  • Post your answers in the comments.

If you want to add anything or share your views, feel free to post in the comments.

Discussion (0)