Terraform Basics

sanjaikumar2311

SanjaiKumar2311

Posted on June 26, 2024

Terraform Basics

Infrastructure as Code (IaC) tools allow you to manage infrastructure with configuration files rather than through a graphical user interface.Terraform is HashiCorp's infrastructure as code tool.HashiCorp's is the company that develop the terraform.It describes the desired end-state for your infrastructure, in contrast to procedural programming languages that require step-by-step instructions to perform tasks.

FEATURES

  1. multiple cloud platforms. 2.The human-readable configuration language.

To deploy infrastructure with Terraform:

  1. - Scope - identifying infrastructure
  2. - Author - to write the configuration
  3. - Initialize - install the packages
  4. - Plan - preview the changes
  5. - Apply -after all this process it is ready to implement.

This blog gives you a complete step-by-step guide that explains you how to create, modify and delete a VM using Terraform CLI.

INSTALLATION
1.Before going to install Terraform, we are going install chocolatey using command in powershell administrator

Set-ExecutionPolicy Bypass -Scope Process -Force; iex ((New-Object System.Net.WebClient).DownloadString('https://community.chocolatey.org/install.ps1'))
Enter fullscreen mode Exit fullscreen mode
  1. After that we are going to install Terraform app and then install the Terraform extension using the command
choco install Terraform
Enter fullscreen mode Exit fullscreen mode

3.Then we can check if Terraform is installed correctly by using the command.

terraform -help
Enter fullscreen mode Exit fullscreen mode

BUILD INFRASTRUCTURE

Prerequisites
To follow this tutorial you will need:

  1. The Terraform CLI (1.2.0+) installed.
  2. The AWS CLI installed.
  3. AWS account and associated credentials that allow you to create resources.

Now go to command prompt and then connect cli with aws console by creating acccess key and secret key.

After that create the directory using the command

$ mkdir learn-terraform-aws-instance
Enter fullscreen mode Exit fullscreen mode

Change into the directory.

$ cd learn-terraform-aws-instance

Enter fullscreen mode Exit fullscreen mode

Create a file to define your infrastructure.

$ code main.tf
Enter fullscreen mode Exit fullscreen mode

Paste the code in main.tf

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-830c94e3"
  instance_type = "t2.micro"

  tags = {
    Name = "ExampleAppServerInstance"
  }
}

Enter fullscreen mode Exit fullscreen mode

paste the code in any coding platform and go to cmd.

Initialize the configuration.

$ terraform init
Enter fullscreen mode Exit fullscreen mode

Apply the configuration. Respond to the confirmation prompt with a yes.

$ terraform apply
Enter fullscreen mode Exit fullscreen mode

After finishing this go to console their you see one EC2 instance is running.

CHANGE INFRASTRUCTURE

Initialize the configuration.

$ terraform init
Enter fullscreen mode Exit fullscreen mode

Apply the configuration. Respond to the confirmation prompt with a yes.

$ terraform apply
Enter fullscreen mode Exit fullscreen mode

Now update the ami of your instance. Change the aws_instance.app_server resource under the provider block in main.tf by replacing the current AMI ID with a new one.

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

Enter fullscreen mode Exit fullscreen mode

Apply the configuration. Respond to the confirmation prompt with a yes.

$ terraform apply
Enter fullscreen mode Exit fullscreen mode

DESTROY
The terraform destroy command terminates resources managed by your Terraform project.

$ terraform destroy
Enter fullscreen mode Exit fullscreen mode

Answer yes to execute this plan and destroy the infrastructure.

Define input variables
Change into the directory.

$ cd learn-terraform-aws-instance

Enter fullscreen mode Exit fullscreen mode

Create a file to define your infrastructure.

$ code main.tf
Enter fullscreen mode Exit fullscreen mode

Paste the code in main.tf

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"
  }
}

Enter fullscreen mode Exit fullscreen mode

Create a new file called variables.tf

variable "instance_name" {
  description = "Value of the Name tag for the EC2 instance"
  type        = string
  default     = "ExampleAppServerInstance"
}

Enter fullscreen mode Exit fullscreen mode

In main.tf, update the aws_instance resource block to use the new variable.

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

   tags = {
-    Name = "ExampleAppServerInstance"
+    Name = var.instance_name
   }
 }

Enter fullscreen mode Exit fullscreen mode

Initialize the configuration.

$ terraform init
Enter fullscreen mode Exit fullscreen mode

Apply the configuration. Respond to the confirmation prompt with a yes.

$ terraform apply
Enter fullscreen mode Exit fullscreen mode

if we apply the above instruction it doesn't change anything once you apply the command and Respond to the confirmation prompt with yes.The instance name will change automatically.

$ terraform apply -var "instance_name=YetAnotherName"
Enter fullscreen mode Exit fullscreen mode

Define input variables
Follow the same step as wee follow in input variable definition. In this we add one more file called output.tf.
Add the configuration below to outputs.tf to define outputs for your EC2 instance's ID and IP address.

output "instance_id" {
  description = "ID of the EC2 instance"
  value       = aws_instance.app_server.id
}

output "instance_public_ip" {
  description = "Public IP address of the EC2 instance"
  value       = aws_instance.app_server.public_ip
}
Enter fullscreen mode Exit fullscreen mode

Apply the configuration. Respond to the confirmation prompt with a yes.

$ terraform apply
Enter fullscreen mode Exit fullscreen mode

output will be displayed on the cmd and we need to delete the EC2 instance .

$ terraform destroy
Enter fullscreen mode Exit fullscreen mode

Answer yes to execute this plan and destroy the infrastructure.

Store remote state

Change into the directory.

$ cd learn-terraform-aws-instance

Enter fullscreen mode Exit fullscreen mode

Create a file to define your infrastructure.

$ code main.tf
Enter fullscreen mode Exit fullscreen mode

Paste the code in main.tf

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"
}

Enter fullscreen mode Exit fullscreen mode

Initialize the configuration.

$ terraform init
Enter fullscreen mode Exit fullscreen mode

Apply the configuration. Respond to the confirmation prompt with a yes.

$ terraform apply

Enter fullscreen mode Exit fullscreen mode

Set up HCP Terraform

If you have a HashiCorp Cloud Platform or HCP Terraform account, log in using your existing credentials and create an organization.

Modify main.tf to add a cloud block to your Terraform configuration, and replace organization-name with your organization name.

terraform {
  cloud {
    organization = "organization-name"
    workspaces {
      name = "learn-terraform-aws"
    }
  }

Enter fullscreen mode Exit fullscreen mode

Login to HCP Terraform

$ terraform login

Enter fullscreen mode Exit fullscreen mode

Confirm with a yes and follow the workflow in the browser window that will automatically open.And now it is asking for token,create a token and copy & paste it.
It will login in to terraform.

Initialize the configuration.

$ terraform init
Enter fullscreen mode Exit fullscreen mode

Now that Terraform has migrated the state file to HCP Terraform, delete the local state file.

$ del terraform.tfstate
Enter fullscreen mode Exit fullscreen mode

The terraform init step created the learn-terraform-aws workspace in your HCP Terraform organization.
Navigate to your learn-terraform-aws workspace in HCP Terraform and go to the workspace's Variables page. Under Workspace Variables, add your AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY as Environment Variables, making sure to mark them as "Sensitive".

Apply the configuration.

$ terraform apply
Enter fullscreen mode Exit fullscreen mode

Terraform will show that there are no changes to be made.Delete the instance.

$ terraform destroy
Enter fullscreen mode Exit fullscreen mode

Answer yes to execute this plan and destroy the infrastructure.

💖 💪 🙅 🚩
sanjaikumar2311
SanjaiKumar2311

Posted on June 26, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related