Terraform Variable File
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed terraform variables.
https://cloudaffaire.com/terraform-variables/
In this blog post, we will discuss terraform variable file.
What is Terraform variable file?
Values for the input variables of a root module can be gathered in variable definition files and passed together using the -var-file=FILE option. For all files which match terraform.tfvars or *.auto.tfvars present in the current directory, Terraform automatically loads them to populate variables. If the file is located somewhere else, you can pass the path to the file using the -var-file flag. It is recommended to name such files with names ending in .tfvars.
Variable file demo:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
##------------------------------ ## Terraform: Variable File ## ##------------------------------ ## Create a directory and get inside it mkdir terraform && cd terraform ## Create terraform.tfvars (all variables will be declared in this file and called in main.tf file) vi terraform.tfvars --------------- variable "aws_access_key" { default = " } variable "aws_secret_key" { default = " } variable "vpc_cidr" { default = "10.0.0.0/10" } variable "vpc_tag_name" { #variable type string type = "string" default = "myvpc" } variable "subnet_count" { #variable type integer type = "string" default = "2" } variable "subnet_public_ip" { #variable type boolean type = "string" default = "true" } variable "subnet_cidrs" { #variable type list type = "list" default = ["10.0.1.0/24","10.0.2.0/24"] } variable "region" { #variable type map type = "map" default = { "Mumbai" = "ap-south-1" "Singapore" = "ap-southeast-1" "Sydney" = "ap-southeast-2" } } --------------- :wq ## Create main.tf vi main.tf --------------- provider "aws" { access_key = "${var.aws_access_key}" secret_key = "${var.aws_secret_key}" region = "${var.region["Mumbai"]}" #get variable region value (ap-south-1) } resource "aws_vpc" "myvpc" { cidr_block = "${var.vpc_cidr}" tags { Name = "${var.vpc_tag_name}" #get variable vpc_tag_name value (myvpc) } } resource "aws_subnet" "myvpc_subnet" { count = "${var.subnet_count}" #get variable subnet_count value (2) vpc_id = "${aws_vpc.myvpc.id}" cidr_block = "${element(var.subnet_cidrs, count.index)}" #get variable subnet_cidrs value (10.0.1.0/24 and 10.0.2.0/24) map_public_ip_on_launch = "${var.subnet_public_ip}" #get variable subnet_public_ip value (true) tags { Name = "myvpc_subnet_${count.index+1}" } } ------------- :wq ## Format code terraform fmt ## Initialize terraform terraform init ## Create the resource terraform apply #Login to AWS console and check VPC, subnets ## Destroy the resources terraform destroy ## Cleanup cd .. && rm -rf terraform |
Hope you have enjoyed this article. In the next blog post, we will discuss terraform data source.
To get more details on terraform, please refer below terraform documentation
https://www.terraform.io/docs/index.html