Terraform Basics
Tags: terraform
Published: 2024-06-26 01:48:11

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'))
```
   2. After that we are going to install Terraform app and then install the Terraform extension using the command

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

```
terraform -help
```
**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
```
Change into the directory.

```
$ cd learn-terraform-aws-instance

```
Create a file to define your infrastructure.

```
$ code main.tf
```
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"
  }
}

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

Initialize the configuration.

```
$ terraform init
```
Apply the configuration. Respond to the confirmation prompt with a yes.

```
$ terraform apply
```
After finishing this go to console their you see one EC2 instance is running. 

**CHANGE INFRASTRUCTURE**

Initialize the configuration.

```
$ terraform init
```
Apply the configuration. Respond to the confirmation prompt with a yes.

```
$ terraform apply
```
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"
 }

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

```
$ terraform apply
```

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

```
$ terraform destroy
```
Answer yes to execute this plan and destroy the infrastructure.



**Define input variables**
Change into the directory.

```
$ cd learn-terraform-aws-instance

```
Create a file to define your infrastructure.

```
$ code main.tf
```
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"
  }
}

```
Create a new file called variables.tf

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

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

```
Initialize the configuration.

```
$ terraform init
```
Apply the configuration. Respond to the confirmation prompt with a yes.

```
$ terraform apply
```
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"
```



**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
}
```
Apply the configuration. Respond to the confirmation prompt with a yes.

```
$ terraform apply
```
output will be displayed on the cmd and we need to delete the EC2 instance .
```
$ terraform destroy
```
Answer yes to execute this plan and destroy the infrastructure.

**Store remote state**

Change into the directory.

```
$ cd learn-terraform-aws-instance

```
Create a file to define your infrastructure.

```
$ code main.tf
```
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"
}

```
Initialize the configuration.

```
$ terraform init
```
Apply the configuration. Respond to the confirmation prompt with a yes.

```
$ terraform apply

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

```
**Login to HCP Terraform**

```
$ terraform login

```

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
```
Now that Terraform has migrated the state file to HCP Terraform, delete the local state file.

```
$ del terraform.tfstate
```
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
```
Terraform will show that there are no changes to be made.Delete the instance.
```
$ terraform destroy
```
Answer yes to execute this plan and destroy the infrastructure.