Skip to content
EgyKode
04 · Infrastructure as CodeLab 21 / 59
Guided labterraform

Terraform Modules

Turn a working configuration into a network module and a compute module, called twice with different inputs.

Time
50 min
Level
Intermediate
Objectives
5 objectives
Cost
Low cost

Where this fits in the platform

This lab adds

  • A reusable module with inputs and outputs

Before you start

You will need

  • Terraform >= 1.6
  • AWS CLI v2, configured

You do not need these already — the lab environment below provides them.

You will be able to

  • Extract a module with a deliberate input/output contract
  • Call one module from another and let the graph order the work
  • Know when a module is not worth writing

CostLow cost

— a VPC, subnets and one `t3.micro`. No NAT Gateway in this lab, deliberately: it is the one resource here that would bill hourly.

Nothing to pay in the browser. Open the terminal runs this against a simulated cloud — the same API calls and the same commands, with no account and no bill. The figure above applies only if you build it in your own.

How to clean up

The scenario#

The configuration from the previous lab works, and now a second environment needs the same shape with different addresses. Copying the directory is the obvious move and the wrong one — two copies drift, and the drift is discovered during an incident.

A module is how the same definition serves both.

Hands-on environment

Run this lab in a real terminal, free and in your browser. The environment is temporary and yours alone — break it as much as you like.

Open the terminal

Opens in Killercoda, in a new tab — keep this page open for the steps.

Run it on AWS

This lab builds real cloud infrastructure, so it needs your own AWS account. Follow the cost and cleanup notes above — the resources are yours, and so is the bill.

Anything you tick here is your own record. EgyKode cannot see inside that terminal, so the success criteria stay self-assessed even when the environment checks your work for you.

The network module

Step 1 of 4

The contract#

A module is a directory of .tf files with exactly three surfaces:

text
modules/network/
  main.tf         the resources it owns
  variables.tf    the inputs  — its public API
  outputs.tf      the outputs — what callers may depend on

Anything a caller needs must leave through an output. There is no reaching inside for a resource, and that restriction is precisely what makes a module safe to change later.

When not to write a module#

A module costs a directory, two extra files and a layer of indirection. It earns that when the same shape is built more than once, or when it hides genuine complexity behind a small interface.

Wrapping a single aws_s3_bucket in a module buys nothing and forces the next reader to open two files to understand one resource. The useful test: would a second caller ever exist? If not, write the resource directly and extract it the day the second caller appears.

Clean up#

Run this even if you did not finish.

DestructiveThis removes real resources. Check which environment you are in first.

Terminal
terraform destroy -auto-approve
aws ec2 describe-instances --filters Name=instance-state-name,Values=running --query 'Reservations[].Instances[].InstanceId'

Cost of this lab: Free tier — a VPC, subnets and one t3.micro. No NAT Gateway in this lab, deliberately: it is the one resource here that would bill hourly.

Success criteria

0 of 5

The concept behind it

Ready to try it without help?Do the challenge

Next up

Lab 21 of 59 on the project path

Terraform Remote State & LockingMove state off your laptop into an encrypted, versioned, locked backend — and prove the lock works by breaking it deliberately.45 minIntermediate

Previous: Terraform Fundamentals