Terraform contd
Activity 2: Create a virtual network in Azure (contd)
- we have already created a resource group
- For Manual steps watch classroom video
- Refer Here for the changes done
Dependencies
- When a resource in a terraform uses attributes of other resource then an implicit dependency is created i.e the current resource will not be created until the other resource is created
- Dependencies can be made explicit by adding a meta argument
depends_on Refer Here
idempotance
- Terraform execution is idempotent as execution irrespective of number of times will give you the same result (i.e desired state)
Terraform console
Reasons for writing Template
- We would want to reuse the terraform templates across various environments and for every environment there will be changes
- Changes can be in
- names
- values
- count of items
- Try to make terraform template generic not specific
- Specific example
def multiply():
return 10 * 20
# required arguments
def multiply(a, b):
return a * b
# default arguments
def multiply(a=10, b=20):
return a * b
Activity 1.1: Lets change the vpc template
- Lets ask the user the vpc cidr range and subnet cidr ranges
- Lets ask the user for the Name of vpc and subnets
- Exercise: Try implementing variables
Activity 2.1: Lets change the virtual network template
- Lets ask the user the vnet cidr range and subnet cidr ranges
- Lets ask the user for the Name of virtual network and subnets
Refer Here for changes
Variable
- Refer Here for official docs
- Terraform provides variables where user can set values while applying
- Variable definition
variable "<variable_name>" {
type = string | number | bool | map | object | list
description =
default =
}
# definition
variable "vpc_cidr" {
type = string
default = "10.0.0.0/16"
description = "vpc cidr"
}
resource "aws_vpc" "base" {
# usage
cidr_block = var.vpc_cidr
}
- Good practice is to define all variables in a file called as
variables.tf or inputs.tf
- Variables can be passed at runtime during apply by adding -var to the apply
terraform apply -var vpc_cidr='192.168.0.0/16'