Terraform Modules Lab (Beginner Intermediate Real-world)
Objective Build reusable Terraform modules and deploy infrastructure using them. You will: Create a VPC module Create an EC2 module Reuse modules Understand module inputs, outputs, and structure Pa...

Source: DEV Community
Objective Build reusable Terraform modules and deploy infrastructure using them. You will: Create a VPC module Create an EC2 module Reuse modules Understand module inputs, outputs, and structure Part 1 – Project Structure (Real-world layout) terraform-modules-lab/ │ ├── modules/ │ ├── vpc/ │ │ ├── main.tf │ │ ├── variables.tf │ │ └── outputs.tf │ │ │ └── ec2/ │ ├── main.tf │ ├── variables.tf │ └── outputs.tf │ ├── env/ │ └── dev/ │ ├── main.tf │ ├── variables.tf │ └── terraform.tfvars Part 2 – Create VPC Module modules/vpc/main.tf resource "aws_vpc" "main" { cidr_block = var.cidr_block tags = { Name = var.name } } resource "aws_subnet" "public" { vpc_id = aws_vpc.main.id cidr_block = var.subnet_cidr availability_zone = var.az tags = { Name = "${var.name}-subnet" } } modules/vpc/variables.tf variable "cidr_block" {} variable "subnet_cidr" {} variable "az" {} variable "name" {} modules/vpc/outputs.tf output "vpc_id" { value = aws_vpc.main.id } output "subnet_id" { value = aws_subnet.publ