Day 63 - Terraform Variables


In Terraform, variables are used to 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 quite important, as you need to hold values of names of instance, configs etc.

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

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

Task-01

Create a local file using Terraform.

Create a variable.tf file

No alt text provided for this image

The filename variable has a default value of /home/ubuntu/terraform/terraform-variable/var-demo.txt, which is the path and filename where the local file will be created.

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

Create a main.tf file

No alt text provided for this image

Terraform code block creates a local_file resource named "devops" that writes a file to disk with the filename and content specified by the filename and content variables.

Initializes a new or existing terraform working directory

No alt text provided for this image

Generates an execution plan that shows what actions terraform will take to reach the desired state specified in the configuration file

No alt text provided for this image

When you run terraform apply, Terraform will create a file called var-demo.txt in the local directory with the content specified in the content variable.

No alt text provided for this image

Check file is created in specified folder using 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"
}
}        

In the example, a map variable named content_map is defined with a default value of two key-value pairs. The keys are strings and the values are also strings.

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 use the content_map variable to set the content parameter of local_file resource. We reference the value associated with the key "content1" using the dot notation and var.content_map["content1"] syntax.

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

Check 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, we define a variable named "file_list" as a list of strings with a default value which contain files path.

This variable can be used in your Terraform configuration to reference the file paths defined in the list.

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 use the file_list variable to set the filename parameter of an local_file resource. We reference the first element of the list using the square bracket notation and var.file_list[0] syntax.

Terraform plan

No alt text provided for this image

Terraform apply

No alt text provided for this image

Check 2 files created with different content

No alt text provided for this image

Object:

?An object is a structural type that can contain different types of values, unlike map, list. It is a collection of named attributes that each have their own type.

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

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


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 display the value of the ami property in the Terraform output after applying the configuration.

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

terraform plan

No alt text provided for this image

terraform apply

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

No alt text provided for this image

Set:

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

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.


Use?terraform refresh

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

terraform refresh is a Terraform command that retrieves the current state of the resources defined in your configuration and updates the Terraform state file to match that state.

By running terraform refresh, you can update the state file to reflect the current state of your resources, so that Terraform has an accurate view of what needs to be changed. This command does not make any changes to your resources, but simply updates the state file to match the current state of your resources.

No alt text provided for this image

Thank you for reading!

Subho Dey

AWS DevOps | Cybersecurity | Cloud ?? | Git | Kubernetes?? | Docker?? | CI/CD | Linux ??| Terraform | Automation | 1x-Oracle | Technical Support | Networking

2 年

Well explained!!!

回复
Apar Gupta

Senior Engineer | SBS | Ex-HCL

2 年

Well Explained! Thanks for sharing.

回复
Sunil Kumar

Senior Cloud Engineer - Nagarro | Google Cloud Certified {"CDI, ACE , PCA, PDE}|Cloud DevOps Specialist | 5x GCP , Azure Certified | Devops | Linux | Docker | Terraform | Jenkins CI/CD | Kubernetes

2 年

wow ! going great Sayali Shewale

回复

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

Sayali Shewale的更多文章

  • 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…

    7 条评论
  • DevOps Project-2

    DevOps Project-2

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

    2 条评论
  • 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…

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

    Day 73 - Setup Grafana on AWS EC2 Instance

    Task: Setup grafana in your local environment on AWS EC2. Go to the AWS console and Launch an EC2 instance Open port…

    2 条评论
  • Day 72 - Grafana

    Day 72 - Grafana

    What is Grafana? Grafana is an open-source data visualization and monitoring tool that allows you to query, visualize…

    3 条评论
  • 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 .

    2 条评论
  • 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…

    1 条评论
  • Day 68 - Scaling with Terraform

    Day 68 - Scaling with Terraform

    Understanding Scaling Scaling is the process of adding or removing resources to match the changing demands of your…

  • Day 67: AWS S3 Bucket Creation and Management using terraform

    Day 67: AWS S3 Bucket Creation and Management using terraform

    AWS S3 Bucket Amazon S3 (Simple Storage Service) is an object storage service that offers industry-leading scalability,…

    1 条评论
  • Day 66 - Terraform Hands-on Project - Build Your Own AWS Infrastructure with Ease using Infrastructure as Code (IaC)

    Day 66 - Terraform Hands-on Project - Build Your Own AWS Infrastructure with Ease using Infrastructure as Code (IaC)

    Task: 1.Create a VPC (Virtual Private Cloud) with CIDR block 10.

    3 条评论

社区洞察

其他会员也浏览了