Part 4 · Exercises

Part 4 Exercises

Prove to yourself you can write infrastructure as code — not just read it.

Terraform

Exercise 4.1

What is the difference between terraform plan and terraform apply? When would you run each?

Exercise 4.2

Write a Terraform resource block that creates an S3 bucket named my-static-assets in us-east-1 with a tag Environment = "production".

Exercise 4.3

What is Terraform state, and why must it be stored remotely when working in a team? Name two risks of storing it locally.

Exercise 4.4

Explain what a Terraform module is. Give an example of a real-world use case where you would create one.

Exercise 4.5

What is OpenTofu and why did the community create it? When might you choose it over HashiCorp Terraform?

Ansible

Exercise 4.6

What is the difference between Terraform and Ansible? If you need to create an EC2 instance and install Nginx on it, which tool handles which part?

Exercise 4.7

Write an Ansible task that installs nodejs and npm using the apt module on an Ubuntu server.

Exercise 4.8

What is an Ansible handler and when does it run? Give a practical example involving an Nginx config change.

Exercise 4.9

Write an Ansible YAML inventory file that defines: 2 web servers (web1.example.com, web2.example.com) and 1 database server (db1.example.com), all using ubuntu as the SSH user.

Exercise 4.10

What does ansible-playbook site.yml --check --diff do? Why is this useful before running against production servers?

Pulumi

Exercise 4.11

What is the main difference between Pulumi and Terraform in terms of how you write infrastructure definitions?

Exercise 4.12 — Capstone

Design (on paper or in pseudocode) a complete IaC setup for a small web app on AWS. Using Terraform: create a VPC, public subnet, security group, and EC2 instance. Using Ansible: install Node.js, PM2, and Nginx on that EC2 instance. Describe the sequence of commands you would run to provision and configure the whole environment from scratch.