Terraform Modules

module "vpc" {
source = "terraform-aws-modules/vpc/aws"

name = "my-vpc"
cidr = ""

azs = ["us-west-1b", "us-west-1c"]
private_subnets = ["", ""]
public_subnets = ["", ""]

tags = {
Terraform = "true"
Environment = "dev"
resource "aws_instance" "demo" {
count = length(module.vpc.private_subnets)
provider = aws.aws_west
ami = var.ami
instance_type = var.type
vpc_security_group_ids = [module.vpc.default_security_group_id]
subnet_id = tolist(module.vpc.private_subnets)[count.index]

tags = {
name = "Demo VM ${count.index}"
  1. A VPC is created with a security group
  2. 2 private and 2 public subnets are also created
  3. 2 EC2 instances are created, and each of them is part of 2 different private subnets

About root and child

Inheritance of providers


Publishing a module

  1. Attributed to the Inheritance discussed previously, Module configuration should not contain any provider blocks.
  2. All the configuration files should be placed in the root directory of the module.
  3. Three files that should always exist in a module are — main.tf, variables.tf and outputs.tf. Variables and outputs should have descriptions. This is because, when publishing modules to Terraform Registry, documentation is automatically generated using this description.
  4. There are certain patterns and techniques described in the documentation which refer to topics like conditional creation of modules, dependency inversion, data only modules, etc. which can be made use of depending on the requirement at hand.
  5. Do not build modules for things that are already simpler. Modules are supposed to wrap a certain level of infrastructure abstraction and are not meant to be thin wrappers around small resources.
  6. Name your module repository in this format — terraform-<provider>-<module_name>
  1. Write a Terraform configuration, initialize it with Git repository.
  2. Push this repository to Github. Terraform works with other VCS like Gitlab, Bitbucket, and Azure DevOps.
  3. Log in to Terraform Cloud platform, and click on the Modules tab.
  4. Click on the + Add module button. It will ask you to connect your VCS with Terraform Cloud.
  5. Pick the VCS of your choice and follow along the steps to authorize Terraform Cloud to it. In this case, it is Github.
  6. Once authorized successfully, Terraform Cloud will automatically detect repositories in the name format described in point# 6 above.
  7. Select the repository and confirm selection and click on the Publish module. That’s it.
  8. Navigate over to Terraform Registry, and search for your module by a given name.





Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Web Scraping With Multi-Threaded File Downloading Using Python

Looker into Oracle Data Warehouse Cloud (ADWC) — DaAnalytics

Oracle Autonomous Data Warehouse Cloud host settings in Looker

Alvin’s Dew Drop Daily — Issue #21

Go(lang) back to basics

Parallels and the Virtual HDD Swap Disk

How to Print Alphabet N in Python?

Upgrading Docker CE to EE for the Impatient Part 3

Power BI & R Part 2: Running R scripts in Power BI

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Let's Do Tech

Let's Do Tech


More from Medium

Learn Kubernetes: Deployments — Recording of changes

Amazon Elastic Kubernetes Service (EKS) vs. Azure Kubernetes Service (AKS)

Configuring Kubernetes Cluster with Terraform and Ansible

Automating installation of HA RKE2 Kubernetes cluster with Ansible