DevOps Classroom Series – 04/Aug/2021

Installing Terraform

  • Terraform can be installed on any system.
choco install terraform -y
  • Terms:

    • Provider: Terraform providers help in creating infrastructure in Cloud/Hypervisors or other types. Each Providers will have Resource Types and DataSources Added
    • Resource:
      • This represents the infrastructure element which you want to create.
    • Argument: Inputs passed to the Resource are referred as arguments
    • Attribute: Outputs are referred as Attributes
    • DataSource: Terraform allows to fetch information from provider by a special kind of resource called as DataSource
  • Syntax: To Create resources with Terraform we need to specify the resources, providers etc in some files. To write Terraform supports two configuration syntax

    • Configuration Syntax: Native grammar of Terraform Language
    • JSON Configuration:
  • Provider syntax:

provider "<provider name>" {
    argument1 = "value1"
    ...
    argumentn = "valuen"
}
  • Provider example
provider "aws" {
    region = "us-west-2"
}

provider "azurerm" {
  features {}
}
  • resources syntax:
resource "<resource_type>" "<NAME>" {
    argument1 = "value1"
    ...
    argumentn = "valuen"
}
  • resource example
resource "aws_instance" "example" {
    ami     = "ami-0782463"
    instance_type = "t2.micro"
}

resource "azurerm_resource_group" "example" {
  name     = "example-resources"
  location = "West Europe"
}
  • Workflow with terraform:
    • Find the Provider
    • In Provider find the resource types to match your needs
    • Express providers and resource types in a file with .tf extension in a new folder
    • Execute some terraform commands
    terraform init
    terraform validate
    terraform apply
    

Scenario-1: Create an S3 Bucket in AWS

  • Manual Steps:
    • Login into AWS Account & Navigate to S3 Preview Preview Preview Preview Preview
  • Working with terraform
    • Create a new folder and open that folder with visual studio code
    • Prepare visual studio code for terraform Preview Preview
    • Create a new file called as main.tf
    • Since we are working to create resource called as s3 bucket in AWS. We need to find a provider for AWS Refer Here
    • We need to configure the AWS Provider and terraform needs to authenticate (login) into our AWS account and create resources. To do this we need to have some kind of credentials
    • For authentication with AWS Refer Here
    • To Create a user and get the credentials to be used in terraform Refer Here
    • In the main.tf file add the access key and secret to the aws provider
      terraform {
      required_providers {
          aws = {
          source  = "hashicorp/aws"
          version = "~> 3.0"
          }
      }
      }
    
      provider "aws" {
          region      = "ap-south-1"
          access_key  = "<your access key here>"
          secret_key  = "<your secret access key here>"
      
      }
    
    • Find an s3 bucket resource Preview
    • The s3 bucket resource type found is Refer Here
    • Add the following to the main.tf
    # Resource for s3
    
      resource "aws_s3_bucket" "my_bucket" {
          bucket = "qts3fromtf"
      
      }
    
    • Lets execute the commands to download the providers
    terraform init
    
    Preview
    • Now lets validate our terraform configuration Preview
    • Now lets ask terraform to create the resource by executing apply Preview Preview Preview
    • We can destroy whatever we have created using terraform destroy

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Floating Social Media Icons by Acurax Wordpress Designers

Discover more from Direct DevOps from Quality Thought

Subscribe now to keep reading and get access to the full archive.

Continue reading

Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube