Terraform

Terraform

Terraform is an infrastructure as a code that allows you to build, change & version infrastructure safely and efficiently.

  • it is open-source & uses the declarative configuration language " Hashicrop"
  • easy to manage & automate your infrastructure

#terraform #infrastructureascode #terraformcertified #devops #devopscommunity

Terraform Architecture

No alt text provided for this image

Main Commands in Terraform

  • Terraform init: Initializes a working directory for Terraform by setting up the environment and downloading required modules.
  • Terraform validate: validate the syntax and configuration of Terraform files to ensure that they are valid and ready for use.
  • Terraform plan: Create an execution plan that shows what changes Terraform will make to the infrastructure based on the current configuration files.
  • Terraform apply: Applies the execution plan generated by Terraform plan to create or modify infrastructure resources.
  • Terraform destroy: Destroys all resources created by Terraform, effectively deleting them from the infrastructure.
  • Terraform Refresh: Refreshes the state of the infrastructure resources to ensure that Terraform has accurate and up-to-date information about its current status.

No alt text provided for this image


Example of Terraform Configuration file

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


  required_version = ">= 1.2.0"
}


provider "aws" {
  region  = "us-west-2"
}


resource "aws_instance" "app_server" {
  ami           = "ami-08d70e59c07c61a3a"
  instance_type = "t2.micro"


  tags = {
    Name = "ExampleAppServerInstance"
  }
}

{        

Components of Terraform:

  • Providers: Terraform relies on plugins called "providers" to interact with cloud providers, saas providers & other APIs

provider "aws" 
  region = "us-west-2"
  access_key = "ACCESS_KEY"
  secret_key = "SECRET_KEY"
}

{        

  • Resources: Resources are the most important element in the terraform language. Each resource block describes one or more infrastructure objects such as EC2, VPC & S3

provider "aws" 
  region = "us-west-2"
  access_key = "ACCESS_KEY"
  secret_key = "SECRET_KEY"
}

{        

  • Variables: Allow users to parameterize their infrastructure code and enable reuse of code by using dynamic values

variable "instance_type" { default = "t2.micro" }        

  • Statefile: Stores the current state of the infrastructure managed by Terraform and is used to track changes and manage resources

terraform { backend "s3" { bucket = "my-bucket" key = "terraform.tfstate" region = "us-east-1" } }        

  • Provisioners: Execute scripts or configuration management tools on the resources created by Terraform during deployment

provisioner "remote-exec" { inline = ["sudo apt-get update", "sudo apt-get install -y nginx"] }        

  • Backends: Store Terraform state data, such as the state file, in a remote location for better collaboration and security

terraform { backend "consul" { address = "127.0.0.1:8500" } }        

  • Modules: A self-contained package of Terraform configuration and resources that can be used as a building block for larger infrastructure deployments.

module "web" { source = "git::https://example.com/vpc.git" }        

  • Data sources: Allow Terraform to fetch information from an external service or resource and use it in the infrastructure code.

ata "aws_ami" "ubuntu" { most_recent = true owners = ["099720109477"] filter { name = "name" values = ["ubuntu/images/hvm-ssd/ubuntu-focal-20.04-amd64-server-*"] } }        

Installation of Terraform in different Operating Systems:

🔗 https://developer.hashicorp.com/terraform/tutorials/aws-get-started/install-cli

For more info check out the below link

🔗 https://developer.hashicorp.com/terraform/docs

To view or add a comment, sign in

More articles by Munikanth Polepalli

  • Top 10 Websites to Craft an ATS-Beating Resume in 2024

    Landing your dream job often involves getting your resume past the Applicant Tracking System (ATS). These digital…

  • AWS Route53

    Amazon Route 53 is a highly scalable and reliable Domain Name System (DNS) service provided by Amazon Web Services…

    1 Comment
  • Kubernetes k8's

    Kubernetes is an Open-Source container orchestration system for automating software deployment, scaling & Management…

    4 Comments

Others also viewed

Explore content categories