TerraWeek Day 2

TerraWeek Day 2

Task 1:

Task 1: Familiarize yourself with HCL syntax used in Terraform](github.com/gourav-toonwal/TerraWeek/blob/ma..)

  • Learn about HCL blocks, parameters, and arguments

  • Explore the different types of resources and data sources available in Terraform

HCL

Hashicorp Configuration Language, This low-level 07 syntax of the Terraform language is defined in 72360 terms of a syntax called HCL, which is also used by configuration languages in other applications, and in particular other HashiCorp products. It is not necessary to know all of the details of HCL syntax to use Terraform, just knowing the basics, should be enough.

local.tf 
#syntax of HCL

<Block> <Parameter> {
<arguments>
}

The Terraform language syntax is built around two key syntax constructs: arguments and blocks.

In HCL (HashiCorp Configuration Language), block parameters define the structure of a configuration block, while arguments specify the specific attributes and values that configure a resource within that block.

VARIABLES TYPES

Strings, Numbers, Boolean, List, or Maps. We can define a default value, example

variable "vpcname" {
    type = string
    default = "myvpc"
}
variable "enabled" {
    default = true
}
variable "sshport" {
    type = number
    default = 22
}

Note: Number or integers don't need double quotes, but Terraform automatically converts number and bool values to strings when needed. For example 5 and "5" both are correct.

VARIABLES LIST

List is the same than an array. We can store multiple values Remember the first value is the 0 position. For example to access the 0 position is var.mylist[0]

variable "mylist" {
    type = list(string)
    default = ["Value1", "Value2"]
}

VARIABLE MAP

Is a Key:Value pair. We use the key to access to the value

variable "mymap" {
    type = map
    default = {
        Key1 = "Value1"
        Key2 = "Value2"
    }
}

Important: For example, if we need to access the value of Key1 (Value1) we can using the next example var.mymap["Key1"]

Note: Remember, we use [ ] for list, and we use { } for maps

INPUT VARIABLES

Is useful to permit the user to manually set a variable when we run Terraform plan, we can add a "description" and when we run a plan shows a message

It is useful to permit the user to set a variable manually when we run Terraform plan, we can add a "description," and when we run a plan, it shows a message

variable "vpc_name" {
    type = string
    description = "Set VPC name"
}

terraform plan example:

var.inputname
    Set VPC name
    Enter a value:

OUTPUTS

Is about the resource we created, when we run the apply we can see the value, not in the plan because in the next case for example, we need first the VPC for know the vpc.id

output "vpc_id" {
    value = "aws_vpc.myvpc.id"
}

If we run a apply we can see the next message:

Apply complete!
Outputs:
vpcid = vpc-099d9099f5faec2d

LOCAL VALUES

A local value assigns a name to an expression, allowing it to be used multiple times within a module without repeating it.

Comparing modules to functions in a traditional programming language: if input variables are analogous to function arguments and outputs values are analogous to function return values, then local values are comparable to a function's local temporary symbols.

Note: For brevity, local values are often referred to as just "locals" when the meaning is clear from context.

Declaring a local value:

A set of related local values can be declared together in a single locals block

locals {
  service_name = "forum"
  owner        = "Community Team"
}

The expressions assigned to local value names can either be simple constants like the above, allowing these values to be defined only once but used many times, or they can be more complex expressions that transform or combine values from elsewhere in the module:

When to use local values:

Local values can be helpful to avoid repeating the same values or expressions multiple times in a configuration, but if overused they can also make a configuration hard to read by future maintainers by hiding the actual values used.

Use local values only in moderation, in situations where a single value or result is used in many places and that value is likely to be changed in future. The ability to easily change the value in a central place is the key advantage of local values.

ENVIRONMENT VARIABLES

We can create an export with our variable before execute terraform plan, and overwrite the value on the .tf files, for example export TF_VAR_vpcname=envvpc. This is useful for pass secrets or sensitive information in a secure form.

CLI VARIABLES

Another way to set variables is by using the command-line, for example terraform plan -var="vpcname=cliname"

TFVARS FILES

Passing variables inside a file, this is possible create a file called terraform.tfvars this file can be in a yaml or json notation, and is very simple, and also we can add maps, for example:

vpcname = "tfvarsname"
port = 22
policy = {
    test = 1
    debug = "true"
}

Note: The terraform.tfvars file is used to define variables and the .tf file declare that the variable exists.

Link: https://amazicworld.com/difference-between-variable-tf-and-variable-tfvars-in-terraform

AUTO TFVARS

This is for example using a file called dev.auto.tfvars (is the next file that look after look in the terraform.tfvars)

MULTIPLE VALUE FILES

We can create a specified *.tvars file and load for example with terraform plan, this is very useful to settings variables for different environments.

terraform plan -var-file=prod.tfvars

LOAD ORDER

  • Any -var and -var-file options on the command line, in order they are provided. (This includes variables set by a Terraform Cloud workspace.)

  • Any .auto.tfvars or .auto.tfvars.json files, processed in lexical order of their filenames.

  • The tfvars.jsonfile, if present. terraform.tfvars.json

  • The tfvarsfile, if present. terraform.tfvars

  • Environment variables

Note: there is no mention of .tf file declaration in there, this is because variables declared in .tf files are concatenated into a single entity consisting of your variables.tf your main.tf and your output.tf files before being processed by Terraform. Hence this declaration have highest precedence in order of application.

Task 2: Understand variables, data types, and expressions in HCL

  • Create a variables.tf file and define a variable

  • Use the variable in a main.tf file to create a "local_file" resource

now using this variable in the main.tf file, make sure to make the main.tf in the same directory

here you can see we are accessing the value key operator, let's user terraform plan and what is supposed to done

hence you can see, that once we use terraform apply a new file is created with the same name in variable.tf and with the same content

Task 3: Practice writing Terraform configurations using HCL syntax

  • Add required_providers to your configuration, such as Docker or AWS

  • Test your configuration using the Terraform CLI and make any necessary adjustments

for this configuration, we need to first under the provider concept ...

let's make a terraform manifest with talk with docker and pull an nginx image from docker and run a docker container.

once you save the file run terraform plan to confirm what is the plan of terraform to create.

once the desired result satisfy now run command terraform apply to apply the terraform plan and confirm by yes.

once the execution in successful, you just need to confirm that the docker container is running by command

wooohooo thank you for being here

If you've enjoyed this article and want to stay updated on the latest in the tech world, don't forget to connect with me on GOURAV TOONWAL. Let's keep the conversation going and explore the exciting world of technology together. Follow for more insights, discussions, and tech updates.

spreading knowledge in the community ...