Day 63 - Terraform Variables ????

Day 63 - Terraform Variables ????

In Terraform, variables pass values from external sources into Terraform configuration. They provide a way to configure the infrastructure without hardcoding values directly into the configuration. Variables in Terraform are important, as you need to hold values of names of instances, configs, etc.

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

variable "filename" {
default = "/home/ubuntu/terraform/terraform-variable/var-demo.txt"
}
variable "content" {
default = "This is demo terraform variable file"
}        

Task-01

Create a local file using Terraform.

Create a variable.tf file

No alt text provided for this image

The filename variable is set to /home/ubuntu/terraform/terraform-variable/var-demo.txt by default, which is the path and filename where the local file will be created.

The content variable has the default value of "This is demo terraform variable file", which is the text that will be put into the local file.

Create a main.tf file

No alt text provided for this image

The "devops" local_file resource, which is created by Terraform code block, write a file with the name and content given by the filename and content variables to disc.

initializes a new or existing working directory for Terraform

No alt text provided for this image

Produces an execution plan outlining the steps Terraform will take to achieve the desired state defined in the configuration file.

No alt text provided for this image

When you run Terraform apply, Terraform will generate a file in the local directory named var-demo.txt with the content supplied in the content variable.

No alt text provided for this image

Check file is created in a specified folder using the ls command.

No alt text provided for this image

Data Types in Terraform

Map

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

Task-02

Use terraform to demonstrate usage of List, Set, and Object datatypes

Put proper screenshots of the outputs

Map:

A map is a collection of values where each value is identified by a string label

variable.tf

variable "content_map" {
type = map
default = {
"content1" = "this is cool content1"
"content2" = "this is cooler content2"
}
}        

A map variable named content_map is defined in the example, with a default value of two key-value pairs. Strings serve as both the keys and the values.

No alt text provided for this image

This variable can be used in your Terraform configuration to reference the values associated with each key defined in the map. For example, you might use it to configure a resource that requires content,

resource "local_file" "devops" {
? ? filename = "/home/ubuntu/terraform/terraform-variable/type-map/demo.txt"
? ? content = var.content_map["content1"]
}        
No alt text provided for this image

In this example, we set the content parameter of the local_file resource using the content_map variable. We use dot notation and the syntax var.content_map["content1"] to refer to the value associated with the key "content1."

No alt text provided for this image

terraform plan

No alt text provided for this image

terraform apply

No alt text provided for this image
No alt text provided for this image

Check the demo.txt file content

No alt text provided for this image

List:

A list is an ordered collection of values of the same type. To define a list variable, use the?list?type and specify the type of elements in the list. Here's an example:

variable "file_list"{
type = list
default = ["/home/ubuntu/terraform/terraform-variable/type-map/file1.txt","/home/ubuntu/terraform/terraform-variable/type-map/file2.txt"]
}        
No alt text provided for this image

In this example, the term "file_list" refers to a list of strings with the default value being a list of strings containing file paths.

You can use this variable in your Terraform setup to refer to the list of defined file paths.

resource "local_file" "devops" {
? ? filename = var.file_list[0]
? ? content = var.content_map["content1"]
}        
No alt text provided for this image

In this example, we set the filename parameter of a local_file resource using the file_list variable. We use square bracket notation and the syntax var.file_list[0] to refer to the first element of the list.

Terraform plan

No alt text provided for this image

Terraform apply

No alt text provided for this image
No alt text provided for this image

Check 2 files created with different content

No alt text provided for this image

Object: Unlike a map and list, an object is a structural type that can hold several types of values. It is a set of named attributes, each with its own type.

In the example, an object variable named aws_ec2_object is defined with the default value of a set of EC2 instance properties.

This variable can be used to refer to the specific properties of the EC2 instance defined in the object in your Terraform configuration.

variable "aws_ec2_object" {
? ? type = object({
? ? ? ? name = string
? ? ? ? instances = number
? ? ? ? keys = list(string)
? ? ? ? ami = string
})


default = {
? ? name = "test-ec2-instance"
? ? instances = 4
? ? keys = ["key1.pem","key2.pem"]
? ? ami = "ubuntu-bfde24"
}
}        
No alt text provided for this image

main.tf

This output can be used to show the value of the ami property in the Terraform output after the configuration has been applied.

output "aws-ec2-instances"
? ? value = var.aws_ec2_object.ami
}        
No alt text provided for this image
No alt text provided for this image

terraform apply

you can see the output of aws-ec2-instance value.

No alt text provided for this image

Set:

A set is an unsorted collection of distinct values of the same type. Use the set type to define a set variable and specify the type of components in the set. Here's an illustration:

variable "security_groups" {
? type? ? = set
? default = ["sg-12345678", "sg-87654321"]
}        

In this example, we define a variable named "security_groups" as a set of strings with a default value.

terraform refresh

Reloads the variables to refresh the state of your configuration file.

The Terraform refresh command retrieves the current state of the resources defined in your configuration and refreshes the Terraform state file to match that state.

You can update the state file to reflect the current state of your resources by running Terraform refresh, so Terraform has an accurate perspective of what needs to be updated. This command makes no modifications to your resources; instead, it just updates the state file to reflect the current state of your resources.

No alt text provided for this image

Many thanks for remaining. I think it will help you understand terraforming a lot better.????

要查看或添加评论,请登录

Vinay Kumar的更多文章

  • DevOps Project - 4 ????

    DevOps Project - 4 ????

    Project Description The project aims to deploy a web application using Docker Swarm, a container orchestration tool…

    7 条评论
  • DevOps Project 3 ????

    DevOps Project 3 ????

    Project Description The project involves hosting a static website using an AWS S3 bucket. Amazon S3 is an object…

    3 条评论
  • DevOps Project -2 ????

    DevOps Project -2 ????

    Project Description The project is about automating the deployment process of a web application using Jenkins and its…

  • Day 80: DevOps Project 1 ????

    Day 80: DevOps Project 1 ????

    Project Description The project aims to automate the building, testing, and deployment process of a web application…

    2 条评论
  • Day 73 - Setup Grafana on AWS EC2 Instance ????

    Day 73 - Setup Grafana on AWS EC2 Instance ????

    Task: Set up grafana in your local environment on AWS EC2. Go to the AWS console and Launch an EC2 instance To enable…

  • Day 72 - Grafana ????

    Day 72 - Grafana ????

    What is Grafana? No matter where your metrics are kept, Grafana is an open-source data visualization and monitoring…

    4 条评论
  • Day71 - Terraform Interview Questions ????

    Day71 - Terraform Interview Questions ????

    1. What is Terraform and how it is different from other IaaC tools? HashiCorp's Terraform is an Infrastructure as Code…

  • Day 70 - Terraform Modules ????

    Day 70 - Terraform Modules ????

    Modules are containers for multiple resources that are used together. A module consists of a collection of .

    4 条评论
  • Day 69 - Meta-Arguments in Terraform ???

    Day 69 - Meta-Arguments in Terraform ???

    When you define a resource block in Terraform, by default, this specifies one resource that will be created. To manage…

    4 条评论
  • Day 65 - Terraform Resources ????

    Day 65 - Terraform Resources ????

    Understanding Terraform Resources A resource in Terraform represents a component of your infrastructure, such as a…

    2 条评论

社区洞察

其他会员也浏览了