desaiyang / DevOps

some details about DevOps and it is associated technologies ... browse thru ...
0 stars 0 forks source link

Terraform Files and Sample Code #3

Open desaiyang opened 2 years ago

desaiyang commented 2 years ago
Terraform Files
All the code for Terraform scripts can be authored in a single name having the extension ".tf". Terraform will load, parse, and interpret the file reading the blocks and then executing them logically. The order of execution of blocks in Terraform script is:
terraform

provider

variable

locals

resource

module

output

As mentioned before, all of these blocks can be authored within a single file; however, each of these can also be authored in their own file with each having a .tf extension. Terraform will assemble all these files together before executing them as a single script. The folder becomes a unit for the script execution, and all files within a folder are combined to work as a single Terraform unit and script.

Breaking down a large monolith Terraform file into smaller manageable file is far better and ideal file structure compared to a single file. The script becomes much more comprehendible, easier to read and helps in faster code editing. 

To understand this better, let’s create a new folder named TerraformFileStructure and further create multiple .tf files in it, each one responsible for declaring different blocks of code.

The goal of these files together is a create a resource group on Azure based on values from variables for resource group name and locations.
Variables.tf: This file contains the variables declaration.

Versions.tf: This file contains the terraform configuration related to provider and Terraform versioning.

Providers.tf: This file contains the provider configuration.

Locals.tf: This file contains the definitions of local variables.

main.tf: This file contains resource and module configurations. This file can further be broken into multiple files, each containing different sets of resources. We will look into this pattern later in the next chapter.

Output.tf: This file contains the output blocks.

Code for variables.tf
The variables.tf file contains just the variable declaration. Three variables, called rgname, location, and environment of type string are declared in this file.
variable rgname {
    type = string
}
variable location {
    type = string
}
variable environment {
    type = string
}
Code for versions.tf
This file contains the terraform configuration related to provider and their version numbers. The terraform configuration helps to define the dependency on the Terraform CLI version using the required_version setting. In the code listing shown next, the Terraform CLI version is 0.13.3. The Terraform configuration also helps in configuring the provider plugins used within the scripts using their source name and version numbers. The azurerm provider plugin is declared within required_providers with multiple version conditions.
terraform {
    required_version = "0.13.3"
    required_providers {
        azurerm = {
            source  = "registry.terraform.io/hashicorp/azurerm"
            version = "~>2.36, ~>2.40"
        }
    }
}
Code for providers.tf
The providers.tf file contains the configuration for the provider itself. This could include the authentication information for connecting to the target cloud platform. In this case, it just contains the mandatory empty features {} element(for now, it will be expanded in subsequent chapters).
provider azurerm {
    features {}
}
Code for locals.tf
The locals file contains the locals block and generates local variables using values from variables supplied during execution. The code listing next uses the variables rgname and environment by concatenating them into a single resourcegroupname variable.
locals {
    resourcegroupname = "${var.rgname}-${var.environment}"
}
Code for main.tf
This file contains the resources to be created. For this example, it contains a single resource_group definition and uses both a local as well as global variable for its attributes.
resource azurerm_resource_group rg {
    name= local.resourcegroupname
    location = var.location
}
Code for outputs.tf
The script execution will also contain a single output: the identifier of resource group. This is a runtime dynamically generated identifier and is returned by the script as output. The code listing shown next uses the resource group name along with its "id" property. The id is then assigned to the value attribute of the output block .
output resourcegroupid {
    value = azurerm_resource_group.rg.id
}
Code for values.tfvars
Finally, there is a values.tfvars file that contains all the values for the global variables. Since values for three global variables are expected, all the three values are provided in this file.
rgname = "filestructureexample"
location = "west europe"
environment = "dev"
Executing the Terraform script will result in the creation of a resource group. The Terraform commands are shown next. It is to be noted that there are multiple ways to provide values for global variables, and .tfvars is just one of them. We will explore other ways in the next chapter. The values for variables must be supplied while executing both the plan and apply commands.
terraform init
terraform plan -var-file=values.tfvar
terraform apply -var-file=values.tfvar -auto-approve
The result after successful execution of apply command is shown next.
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
resourcegroupid = /subscriptions/9755ffce-e94b-4332-9be8-1ade15e78909/resourceGroups/filestructureexample-dev