Working With Terraform

Limited Time Offer!

For Less Than the Cost of a Starbucks Coffee, Access All DevOpsSchool Videos on YouTube Unlimitedly.
Master DevOps, SRE, DevSecOps Skills!

Enroll Now
  • Write a comparison between Ansible Vs Docker Vs Terraform
  • Write a terraform script where you create a linux ec2 instance with Security group and key defined so you should be able to use key to login to ec2 instance.

Terraform VS Ansible

  • Terraform focus on infrastructure provisioning where as ansible fits well in traditional automation.
  • Terraform acts perfectly in configuring cloud infrastructure where as ansible is employed for configuring servers.
  • Using terraform one can deploy load balancers, VPCs, etc. where as with ansible one can deploy apps on the top of infrastructure.
  • Terraform uses declarative language where as ansible uses procedural.

Terraform VS Docker

  • Docker Machine belongs to “Container Tools” category of the tech stack, while Terraform can be primarily classified under “Infrastructure Build Tools”.
  • “Easy docker hosts management” is the primary reason why developers consider Docker Machine over the competitors, whereas “Infrastructure as code” was stated as the key factor in picking Terraform.
Step 1:- Initializing Terraform Script

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

# Configure the AWS Provider
provider "aws" {
  region = "us-west-2"

}

resource "aws_instance" "avinash" {
  ami           = "ami-03d5c68bab01f3496"
  instance_type = "t2.micro"  
}

- Save the fie with .tf extension
- In the command prompt run the code terraform init 
Note:- the ami should be created in the same region as mentioned in the provider "aws"  
Step 2:- Managing Permissions Using Security Keys
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 3.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region = "us-west-2"
  access_key = ""
  secret_key = ""
}

resource "aws_instance" "avinash" {
  ami           = "ami-03d5c68bab01f3496"
  instance_type = "t2.micro"
  

}

- Save the file and run the command terraform plan
- after the command is a success then run terraform apply
- Type Yes to continue
Note:- If an error occur regarding ami id then do validate the region in which the ami was created.
Linux EC2 terraform Script:-
terraform{
  Required_Providers {
   linux ec2 instance ={
   source = "hashicorp/linux ec2"
   version = "~>3.27
}
}
provider "linux" {
  profile = "jyoshna"
  region = "us-west-2"
  access_key = " "
  secret_key = " "
}
resource "linux_security_group " allow key to login"{
name = "devopsschool"
description = " allow key to login ec2 instance" 
}