Terraform modules
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In the last blog post, we have discussed how to deploy an AWS LAMP stack using terraform.
https://cloudaffaire.com/how-to-deploy-a-lamp-stack-in-aws-using-terraform/
In this blog post, we will discuss modules in terraform.
What are modules in terraform?
A module is a container for multiple resources that are used together. Every Terraform configuration has at least one module, known as its root module, which consists of the resources defined in the .tf files in the main working directory.
A module can call other modules, which lets you include the child module’s resources into the configuration in a concise way. Modules can also be called multiple times, either within the same configuration or in separate configurations, allowing resource configurations to be packaged and re-used.
Modules can be called by calling the module block
1 2 3 4 5 6 |
module " source = " provider = {} version = "0.0.5" } |
The resources defined in a module are encapsulated, so the calling module cannot access their attributes directly. However, the child module can declare output values to selectively export certain values to be accessed by the calling module.
Next, we are going to create a vpc and a subnet using terraform module concept.
Below is the directory structure for this demo.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
## Directory structure . ├── main.tf ├── outputs.tf ├── variables.tf └── module ├── subnet │ ├── main.tf │ ├── outputs.tf │ └── variables.tf └── vpc ├── main.tf ├── outputs.tf └── variables.tf |
We have main block in the root level and two directories for vpc and subnet. Each directory (module) has its own main block. Modules are called in the root level main block using module block.
Terraform modules:
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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
##------------------------ ## Terraform: Modules ## ##------------------------ ## Create a directory name terraform and get inside it mkdir terraform && cd terraform ## Create a directory name module and get inside it mkdir module && cd module ## Create a directory named vpc and subnet mkdir vpc subnet ## Get inside vpc directory cd vpc ## Create main.tf under vpc directory vi main.tf ----------------------- #creates one vpc resource "aws_vpc" "myvpc" { cidr_block = "${var.vpc_cidr}" tags { Name = "myvpc" } } ----------------------- :wq ## Create variables.tf under vpc directory vi variables.tf ----------------------- #declare variables for main.tf under vpc directory variable "vpc_cidr" { description = "CIDR block of VPC" default = "192.168.0.0/16" } ----------------------- :wq ## Create outputs.tf under vpc directory vi outputs.tf ----------------------- #generates vpc attributes that can be passed to other modules #or printed as output in terraform apply output "vpc_id" { description = "The ID of the VPC" value = "${aws_vpc.myvpc.id}" } ----------------------- :wq ## Get inside subnet directory cd .. && cd subnet ## Create main.tf under subnet directory vi main.tf ----------------------- #creates one subnet resource "aws_subnet" "myvpc_subnet" { vpc_id = "${var.vpc_id}" cidr_block = "${var.subnet_cidr}" availability_zone = "${var.subnet_availability_zone}" tags { Name = "myvpc_subnet" } } ----------------------- :wq ## Create variables.tf under subnet directory vi variables.tf ----------------------- #declares variables for main.tf under subnet directory variable "vpc_id" { description = "VPC ID" default = "" } variable "subnet_cidr" { description = "CIDR block for the subnet" default = "" } variable "subnet_availability_zone" { description = "Availability zone of the subnet" default = "" } ----------------------- :wq ## Create outputs.tf under subnet directory vi outputs.tf ----------------------- #generates subnet attributes that can be passed to other modules output "subnet_id" { description = "Subnet ID" value = "${aws_subnet.myvpc_subnet.id}" } ----------------------- :wq ## Go to parent directory cd /home/ec2-user/terraform ## Create main.tf under terraform directory vi main.tf ----------------------- #Creates a VPC and a subnet provider "aws" { access_key = "${var.access_key}" secret_key = "${var.secret_key}" region = "${var.region}" } #module vpc called module "vpc" { source = "./module/vpc" vpc_cidr = "${var.vpc_cidr}" } #module subnet called module "subnet" { source = "./module/subnet" #observe vpc id is passed from module vpc to module subnet vpc_id = "${module.vpc.vpc_id}" subnet_cidr = "${var.subnet_cidr}" subnet_availability_zone = "${var.subnet_availability_zone}" } ----------------------- :wq ## Create variables.tf under terraform directory vi variables.tf ----------------------- #declare variables for main.tf in terraform directory variable "access_key" { default = " } variable "secret_key" { default = " } variable "region" { default = "ap-south-1" } variable "subnet_availability_zone" { default = "ap-south-1a" } variable "vpc_cidr" { default = "10.0.0.0/16" #overrides default 192.168.0.0/16 } variable "subnet_cidr" { default = "10.0.0.0/24" } ----------------------- :wq ## Create outputs.tf under terraform directory vi outputs.tf ----------------------- #generate output output "vpc_id" { description = "The ID of the VPC" value = "${module.vpc.vpc_id}" } output "subnet_id" { description = "The ID of the Subnet" value = "${module.subnet.subnet_id}" } ----------------------- :wq ## Format the code terraform fmt ## Initialize the provider terraform init ## Plan the changes terraform plan ## Apply the changes terraform apply ## Show state details terraform show ## Cleanup ## Destory resources terraform destory ## Remove terraform directory cd .. && rm -rf terraform |
Hope you have enjoyed this article. In the next blog post, we will continue with terraform module and will use the community supported modules to create a vpc.
To get more details on terraform, please refer below terraform documentation
https://www.terraform.io/docs/index.html