Day63 ---> 90DaysOfDevOps Challenge @TWS

Day63 ---> 90DaysOfDevOps Challenge @TWS

Terraform Variables

Variables in Terraform are quite important, as you need to hold values of names of instances, configs, etc. to use them in the configuration file.

We can create variables.tf file which will hold all the variables.

variable "filename" {
default = "path/to/file/demo-var.txt"
}
variable "content" {
default = "This is coming from a variable which was updated"
}

These variables can be accessed by the var object in main.tf

How to create a local file using Terraform->

Hint:

resource "local_file" "devops" {
filename = var.filename
content = var.content
}

image

image

image

image

image

cghvjh

Use of Data Types in Terraform

Map

In Terraform, the map type is used to represent a collection of key-value pairs. It allows you to store and manipulate data in a structured way. A map is similar to a dictionary or hash table in other programming languages.

Here's an example of defining a map variable in Terraform:

variable "file_contents" {
type = map
default = {
"statement1" = "this is cool"
"statement2" = "this is cooler"
}
}

Let's use map type to create files--->

image

image

image

image

image

dswqadea

How to use variables with datatypes - List, Set and Object to access them in the configuration file.

Let's use type - list to create files--->

image

image

image

image

image

image

Let's use type - set to create files--->

image

image

image

image

vdfvs

Let's use type - Object to create files--->

image

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

provider "aws" {
  region = "us-east-1"
}

resource "aws_security_group" "main" {
  name_prefix = "${var.aws_ec2_object.name}-sg"
  ingress {
    from_port   = 22
    to_port     = 22
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

resource "aws_instance" "ec2" {
  count = var.aws_ec2_object.instances

  ami           = var.aws_ec2_object.ami
  instance_type = var.aws_ec2_object.instance_type
  key_name      = element(var.aws_ec2_object.keys, count.index)
  vpc_security_group_ids = [aws_security_group.main.id]

  tags = {
    Name = "${var.aws_ec2_object.name}-${count.index+1}"
  }
}

image

image

image

image

image

image

image

Use of terraform refresh --->

To refresh the state of your configuration file, reloads the variables

terraform refresh is used to update the state file with the real-world state of the resources that are already managed by Terraform.

When a Terraform configuration is applied using terraform apply or terraform plan, the state file is updated with the latest state of the resources. However, the state file can become out of sync with the real-world state of the resources being managed.

For example, if a resource is manually edited, created, or destroyed outside of Terraform, the state file will no longer accurately reflect the true state of the resource. Terraform refresh can be used to remedy this issue. This command refreshes the state file from the real-world state of the resources, ensuring that the state file accurately reflects the current state of the resources.

It's a good practice to run Terraform refresh before running Terraform plan to ensure that the Terraform state file is up to date with real-world resources. This helps avoid any potential problems that might arise from applying a configuration based on stale or inaccurate state information.

image

Happy Learning :)

Day 63 task is complete!

90DaysOfDevOps Tasks👇

github.com/Chaitannyaa/90DaysOfDevOps.git

Chaitannyaa Gaikwad | LinkedIn