What’s new in Terraform v0.13

There are few additions, but they are worth knowing, because this will help expand your opportunities when working with Terraform

Let’s take a look at the most useful new features when working with modules.

We will deploy the infrastructure in aws. Let’s create three vpc and networks for dev, stage and prod contours.

source – tells Terraform where to find the source code for what you want child module.
The sources are different, we will use the local path to the module env, to simplify the code, we indicate which contour vpc_cidr – the range of IPv4 addresses for our VPC in CIDR block format.

The variables are as follows:

variable "vpc_network_cidr" { 
   default    = {
       dev     = "172.16.1.0/24"
       stage  = "172.17.1.0/24"
       prod    = "172.18.1.0/24"
   }
}

variable "region" {
   default = "eu-west-2"
}

As you can see, the module is used. Its structure is pretty simple:

cidr_block – this is about the range of IPv4 addresses for your VPC in the CIDR block format. Block sizes must be between netmask / 16 and netmask / 28

tags – creates a tag with the key “Name” and the value you specified

vpc_id – VPC identifier to create

The variables for the module are as follows:

variable "vpc_cidr" {
   default = "172.16.0.0/16"
}

variable "env" {
   default = "dev"
}

variable "public_subnet_cidrs" { 
   default = [
      "172.16.1.0/24"
   ]
}

variable "private_subnet_cidrs" { 
   default = [
      "172.16.2.0/24"
   ] 
}

Let’s say we want to create a module vpc-prod only after the two previous ones have been created. In Terraform v0.13, this can be done with the following addition:

depends_on = [module.vpc-dev, module.vpc-stage] in section module "vpc-prod"

At the exit, after starting the creation of infrastructure, you can see that at first, networks were simultaneously created for dev and stage contours and, only after their creation, a network was created and vpc for prod contour.

In AWS itself, we created vps and the networks will look like this:

Another innovation is the ability to use a loop. On the plus side, your code will be much smaller.

Loops are created with count and for_eachIn previous versions of Terraform, functions for_each and count allowed the systematic creation of multiple instances of resources from one resource block based on data from other parts of the module.

Terraform 0.13 provides a similar capability for entire modules, allowing a single module block to systematically create multiple module instances.

Meta argument for_each takes a map or rowset and creates an instance for each item in that map or rowset. Each instance has a separate infrastructure object associated with it, and each is individually created, updated, or destroyed when the configuration is applied.

In our case, we use a map: each.key and each.value

each.key – the card key corresponding to this instance.

each.value – the value of the card corresponding to this instance.

Meta argument count takes an integer and creates the specified number of module instances. Each instance has a separate infrastructure object associated with it, and each is individually created, updated, or destroyed when the configuration is applied.

count.index – the individual index number (starting at 0) corresponding to this instance.

We do terraform apply and we see that those who count , will be with an index of the form [0], Those who for_each will be with the names of the contours inside.

In AWS itself, we created vps and the networks will look like this:


This article was published on the eve of the start of the course Administrator Linux. Advanced… Learn more about the course by following the link. We also invite you to watch the recording of a free demo lesson on the topic “Cluster file system Luster”


GET A DISCOUNT

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *