Define Terraform as per your learning

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

What is Terraform?

Terraform is an open-source infrastructure as code (IaC) tool created by HashiCorp. It allows users to define and provision data center infrastructure using a declarative configuration language known as HashiCorp Configuration Language (HCL). Terraform can manage resources across various service providers, such as public clouds (e.g., AWS, Azure, GCP), private clouds, and even SaaS services.

How Terraform Works?

Terraform works by reading configuration files that describe the desired state of infrastructure and then applying these configurations to manage the actual infrastructure. Here’s a high-level overview of how Terraform works:

  1. Write Configuration: You define your infrastructure in configuration files using HCL. These files describe the resources needed, such as servers, databases, and networking components.
  2. Initialize: Terraform initializes the working directory containing the configuration files. This step downloads the provider plugins needed for the defined infrastructure.
  3. Plan: Terraform creates an execution plan, which shows the user what actions will be taken to reach the desired state defined in the configuration files. It does this by comparing the current state with the desired state.
  4. Apply: Terraform executes the actions proposed in the plan to create, update, or delete infrastructure resources.
  5. Manage: Terraform continuously manages and updates the infrastructure as the configuration files change. It keeps track of the current state of the infrastructure in a state file.

Top 5 Terraform Commands

  1. terraform init: Initializes a working directory containing Terraform configuration files. This command downloads the necessary provider plugins and sets up the environment. terraform init
  2. terraform plan: Creates an execution plan, which shows the changes that will be made to the infrastructure based on the configuration files. terraform plan
  3. terraform apply: Applies the changes required to reach the desired state of the configuration. It creates or updates the infrastructure. terraform apply
  4. terraform destroy: Destroys the infrastructure managed by the configuration files. This command is used to clean up all resources defined in the configuration. terraform destroy
  5. terraform validate: Validates the configuration files for syntax errors and other issues. This command helps ensure that the configuration is syntactically correct. terraform validate

Sample Terraform Code for AWS

Here’s a sample Terraform configuration to create an EC2 instance in AWS:

# Provider configuration
provider "aws" {
  region = "us-west-2"
}

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

# Create a subnet
resource "aws_subnet" "example_subnet" {
  vpc_id            = aws_vpc.example_vpc.id
  cidr_block        = "10.0.1.0/24"
  availability_zone = "us-west-2a"
}

# Create an Internet Gateway
resource "aws_internet_gateway" "example_igw" {
  vpc_id = aws_vpc.example_vpc.id
}

# Create a route table
resource "aws_route_table" "example_route_table" {
  vpc_id = aws_vpc.example_vpc.id

  route {
    cidr_block = "0.0.0.0/0"
    gateway_id = aws_internet_gateway.example_igw.id
  }
}

# Associate the route table with the subnet
resource "aws_route_table_association" "example_route_table_assoc" {
  subnet_id      = aws_subnet.example_subnet.id
  route_table_id = aws_route_table.example_route_table.id
}

# Create a security group
resource "aws_security_group" "example_sg" {
  vpc_id = aws_vpc.example_vpc.id

  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

# Create an EC2 instance
resource "aws_instance" "example_instance" {
  ami           = "ami-0c55b159cbfafe1f0" # Amazon Linux 2 AMI ID
  instance_type = "t2.micro"
  subnet_id     = aws_subnet.example_subnet.id
  security_groups = [aws_security_group.example_sg.name]

  tags = {
    Name = "ExampleInstance"
  }
}

Sample Terraform Code for Azure

Here’s a sample Terraform configuration to create a virtual machine in Azure:

# Provider configuration
provider "azurerm" {
  features {}
}

# Create a resource group
resource "azurerm_resource_group" "example_rg" {
  name     = "example-resources"
  location = "East US"
}

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

# Create a subnet
resource "azurerm_subnet" "example_subnet" {
  name                 = "example-subnet"
  resource_group_name  = azurerm_resource_group.example_rg.name
  virtual_network_name = azurerm_virtual_network.example_vnet.name
  address_prefixes     = ["10.0.1.0/24"]
}

# Create a network interface
resource "azurerm_network_interface" "example_nic" {
  name                = "example-nic"
  location            = azurerm_resource_group.example_rg.location
  resource_group_name = azurerm_resource_group.example_rg.name

  ip_configuration {
    name                          = "internal"
    subnet_id                     = azurerm_subnet.example_subnet.id
    private_ip_address_allocation = "Dynamic"
  }
}

# Create a virtual machine
resource "azurerm_virtual_machine" "example_vm" {
  name                  = "example-vm"
  location              = azurerm_resource_group.example_rg.location
  resource_group_name   = azurerm_resource_group.example_rg.name
  network_interface_ids = [azurerm_network_interface.example_nic.id]
  vm_size               = "Standard_DS1_v2"

  storage_image_reference {
    publisher = "Canonical"
    offer     = "UbuntuServer"
    sku       = "18.04-LTS"
    version   = "latest"
  }

  storage_os_disk {
    name              = "example-osdisk"
    caching           = "ReadWrite"
    create_option     = "FromImage"
    managed_disk_type = "Standard_LRS"
  }

  os_profile {
    computer_name  = "examplevm"
    admin_username = "adminuser"
    admin_password = "Password1234!"
  }

  os_profile_linux_config {
    disable_password_authentication = false
  }
}
Subscribe
Notify of
guest

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

11 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Ariel Balduzzi
Ariel Balduzzi
2 months ago

1) What is Terraform?
A tool to work with Infra as a code

2) How Terraform Works?
It reads a terraform code, and send to different providers to create, update or delete resources using API.

3) What are the top 5 terraform commands
init, plan, apply, destroy, validate

4) Write a Sample Terraform code for your fav providers.
resource “aws_instance” “web” {
 ami      = ami.289416498369649
 instance_type = “t2.micro”

 tags = {
  Name = “MyNewInstance”
 }
}

Davs Reyes
Davs Reyes
2 months ago

Terraform is a code for Infra or simply means as Infra as a Code (IAAC). It’s a code or a tool that lets you build and change resources

Terraform work as a code which communicate to an infrastructure

This is being used by DevOps team to automate various task in the infrastructure

The top Terraform command
Init
validate
plan
apply
destroy

Ashish khurana
Ashish khurana
2 months ago

Terraform is the IAC tool. It helps in managing resources on cloud or appliances, on-prem, etc.

Terraform manages resources with the help of providers or APIs on end points.

Commands – Init, plan, destroy, validate, and show.

provider “Azure” {
region = “us-east”
}

Quek
Quek
2 months ago

1. Terraform is a tool and open-source from HashiCorp. t empowers users to define and manage cloud infrastructure and on-premises resources in a human-readable, declarative language called HashiCorp Configuration Language (HCL). 

2. Terraform creates and manages resources on cloud platforms and other services through their application programming interfaces (APIs).

3.init, validate, plan, apply, destroy

4. terraform {
 required_providers {
  github = {
   source = “integrations/github”
   version = “~> 6.0”
  }
 }
}

# Configure the GitHub Provider
provider “github” {}

Marcin Kenar
Marcin Kenar
2 months ago

Answers:

  1. Terraform it is a “GO” language which allow you to Create/Update/Destroy objects.
  2. Run code located in current directory, connect by the provider to the infrastructure and then C/U/D something and give you output.
  3. plan/apply/init/get/validate

resource “aws_instance” “web” {
ami = “linux8.8”
instance_type = “something”
key_name = “Kyndryl_training”

tags = {
Name = “Hi_all”

Cesar Gonzalez - México
Cesar Gonzalez - México
2 months ago

What is Terraform?
Terraform is a declarative programming tool that allows developers to use a high-level configuration language called HashiCorp Configuration Language (HCL) to describe the desired “end state” of on-premises or cloud infrastructure to run an application. It then generates a plan to reach that final state and executes it to provision the infrastructure..

How Terraform Works?
Is a opencode tool using a simple syntax, Terraform provisions infrastructure across multiple data centers (providers) in the cloud and on-premises. Additionally, it securely and efficiently re-provisions infrastructure in response to configuration changes.

What are the top 5 terraform commands
-Init
-Destroy
-Validate
-Plan
-Apply

Write a Sample Terraform code for your fav providers.
 
terraform {
 required_providers {
   azurerm = {
     source = “hashicorp/azurerm”
     version = “3.107.0”
   }
 }
}
 
provider “azurerm” {
 # Configuration options
}

 [
  {
    "cloudName": "AzureCloud",
    "id": "IDDETENTANT",
    "isDefault": true,
    "name": "CLIENT Subscription",
    "state": "Enabled",
    "tenantId": "TENTANTID",
    "user": {
      "name": "user@example.com",
      "type": "user"
    }
  }
]

 

Mahendar Singh
Mahendar Singh
2 months ago

What is Terraform?
Terraform is an infrastructure as code IAC tool created by HashiCorp. It allows you to define and provision infrastructure resources across multiple cloud providers, including AWS, Azure, Google Cloud, and more

How Terraform Works:
Terraform work with below steps
Write Configuration
Initialize
Plan: 
Apply
Destroy

Top 5 Terraform Commands:

terraform init
terraform plan
terraform apply
terraform show
terraform destroy

Prapasri
Prapasri
2 months ago

What is Terraform?
: Terraform is a management tool. infrastructure safely and efficiently using a short and easy-to-understand configuration syntax. (Infrastructure as Code) Terraform can be used to manage almost any cloud, Docker, Kubernetes, and more than 200+ providers.
How Terraform Works?
: Define resource code, manage configuration file, execution plan, apply

What are the top 5 terraform commandsWhat are the top 5 terraform commandsWhat are the top 5 terraform commands
: terraform init , plan, apply, destroy,validate

\
Write a Sample Terraform code for your fav providers.

:resource "aws_db_instance" "changeme_simple_aws_db_instance" {
  allocated_storage   = 5
  engine              = "mysql"
  engine_version      = "5.7"
  instance_class      = "db.t3.micro"
  name                = "changeme_simple_aws_db_instance"
  username            = "changemeusername"
  password            = "changeme_password"
  skip_final_snapshot = true
}
Slawomir Koper
Slawomir Koper
2 months ago

What is Terraform?

  • It is a very useful and simple tool which can be used to build any infrastructure

How Terraform Works?

  • Terraform uses the APIs of supported cloud platforms to connect with them and interract (create new or change existing objects)

What are the top 5 terraform commands

  • plan
  • apply
  • destroy
  • show
  • init

Write a Sample Terraform code for your fav providers.

  • sample code

# Configure the Azure provider
terraform { 
required_providers {   
azurerm = {     
source = “hashicorp/azurerm”     
version = “~> 3.0.2”   

}
required_version = “>= 1.1.0”
}

Pablo Rossi
Pablo Rossi
2 months ago

What is Terraform?
Terraform is an open-source infrastructure as code (IaC) .It allows users to define and provision data center infrastructure using a high-level configuration language called HashiCorp Configuration Language (HCL), or optionally JSON.

How Terraform Works?
Terraform’s workflow involves writing declarative configuration files, initializing the environment, planning changes, applying changes, and maintaining state

What are the top 5 terraform commands

terraform init
terraform plan
terraform apply
terraform destroy
terraform state

Write a Sample Terraform code for your fav providers.

For VMware provider

terraform {

 required_providers {

   vsphere = {

     source = "hashicorp/vsphere"

     version = "2.8.1"

   }

 }

}




provider "vsphere" {

 # Configuration options

}
Kuu
Kuu
2 months ago

What is Terraform?
Terraform is an open-source infrastructure as code (laC) tool developed by HashiCorp. It allows users to define and provision data center infrastructure using a high level of configuration language with HCL or Json.

How Terraform Works?

  1. Write configuration
  2. Initialize
  3. Plan
  4. Apply
  5. Manage State
  6. Destroy

What are the top 5 terraform commands
Top 5 Terraform commands are:

  1. init – initializes the working directory that containing Terraform configuration file and download the necessary provider plugins
  2. plan – Generates an execution plan. Show what actions Terraform will take to create, update or delete resources. Useful for understanding changes before applying them.
  3. apply – apply the changes to reach desire state of the configuration. Creates, updates or deletes resources based on execution plan.
  4. destroy – deletes all the resources defined in the configuration. useful for cleaning up environments when they are no longer needed
  5. fmt – format the configuration files to follow the canonical format and style. Ensure consistency and readability in configuration files.

Write a Sample Terraform code for your fav providers.

provider “aws” {
 access_key = “var.access_key”
 secret_key = “var.secret_key”
 region = var.region
}

resource “ami_instance” “webserver” {
 ami = var.ami
 instance_type = var.instance_type
}

11
0
Would love your thoughts, please comment.x
()
x