What Are Terraform Modules and How Do They Work?
What does a module do?
A Terraform module allows you to create logical abstraction on the top of some resource set. In other words, a module allows you to group resources together and reuse this group later, possibly many times.
Modules: Definitions

Root Modules

Child Modules

The output block and how it’s used in Modules
One of the uses of an output block is to expose a subset of the module’s resource attributes to a parent module. As a precursor to Module Composition, bellow is an illustration of how outputs are used in modules:

Let's assume we have a virtual server with some features hosted in the cloud. What set of resources might describe that server? For example:
the virtual machine itself, created from some imagean attached block device of a specified size for additional storage
a static public IP mapped to the server's virtual network interface
a set of firewall rules to be attached to the server
other things like another block device, additional network interface, and so on
Now let's assume that you need to create this server with a set of resources many times. This is where modules are really helpful – you don't want to repeat the same configuration code over and over again, do you?
We can create sample VPC using modules:
https://github.com/GudditiNaganjaneyulu/Terraform/tree/main/vpc
terraform init
Observation:
1. Verify if modules got downloaded to .terraform folder
# Terraform Validate
terraform validate
# Terraform plan
terraform plan
# Terraform Apply
terraform apply -auto-approve
Observation:
1) Verify VPC
2) Verify Subnets
3) Verify IGW
4) Verify Public Route for Public Subnets
5) Verify no public route for private subnets
6) Verify NAT Gateway and Elastic IP for NAT Gateway
7) Verify NAT Gateway route for Private Subnets
8) Verify no public route or no NAT Gateway route to Database Subnets
9) Verify Tags
# Terraform Destroy
terraform destroy -auto-approve
# Delete Files
rm -rf .terraform*
Comments
Post a Comment