Status: Done · Stack: Terraform, AWS (EC2, security groups), Ansible, Docker, UFW · Repo: github.com/Delajeng/devops-project-02
Goal#
The second project in my cloud DevOps series: stop clicking servers together in a console. Done means a server I can create, configure and destroy from code, with the same result every time and nothing secret in Git.
Architecture#
flowchart LR laptop["Laptop"] -- "terraform apply" --> aws["AWS API"] aws --> ec2["EC2 t2.micro
Ubuntu 24.04"] sg["Security group
SSH from my IP only
app port 8080"] --- ec2 laptop -- "ansible-playbook over SSH" --> ec2 ec2 --> app["Docker + UFW
app container on :8080"]
Terraform talks to the AWS API and creates three things: an SSH key pair from my public key, a security group, and an EC2 instance running the latest official Ubuntu 24.04 image. It prints the server’s public IP. Ansible then connects over SSH and configures the server: it installs Docker, enables a UFW firewall that allows only SSH and the app port, pulls the app image and starts it as a container that restarts on its own. terraform destroy removes everything.
Stack#
| Layer | Tool | Why this one |
|---|---|---|
| Cloud | AWS EC2 (t2.micro) | Free-tier eligible, and the provider I’m certified in |
| Provisioning | Terraform + AWS provider ~> 5.0 | Declarative, shows a plan before changing anything, tracks what it created |
| OS image | Ubuntu 24.04, looked up by a data source | Always the latest official Canonical image, never a hard-coded AMI ID |
| Configuration | Ansible | Agentless (only needs SSH) and idempotent, so it can be re-run safely |
| Firewall | AWS security group + UFW | Two layers: one at the network edge, one on the host |
| Runtime | Docker (community.docker collection) | The app ships as an image, like Project 1 |
Key decisions#
- Terraform creates, Ansible configures. Terraform is good at cloud resources and their lifecycle; Ansible is good at what’s inside the server. Each tool does the job it’s built for.
- SSH only from my IP. The security group allows port 22 from a single
admin_cidrset interraform.tfvars. Only the app port is open to the world. - The AMI is looked up, not hard-coded. A data source finds the newest Ubuntu 24.04 image from Canonical’s account, so the code doesn’t go stale when AWS publishes new images.
- Everything is a variable. Region, instance type, key path, admin IP and app port live in
variables.tf, with a.tfvars.exampleto copy. - Idempotent playbook. Every task describes a state (“Docker installed”, “container started”), so running it twice changes nothing the second time.
- Real values stay out of Git.
terraform.tfvars, Terraform state and the Ansible inventory are gitignored; only.exampletemplates are committed.
What broke and what I learned#
- Symptom: after my first commit, the Ansible inventory with the server’s real public IP was on GitHub. Cause: I created
hosts.inibefore adding it to.gitignore, and ignoring a file doesn’t untrack it once it’s committed. Fix: removed it from Git withgit rm --cached, ignored it, and kept ahosts.ini.exampleinstead. The lesson: the IP is still in the history, so set up.gitignorebefore the first commit. Here it was harmless, because the instance is destroyed and AWS releases the IP.
Along the way I learned:
- Terraform’s workflow: providers, resources, variables, outputs, state, and
plan/apply/destroy - Ansible’s building blocks: inventory, playbooks, modules, collections and idempotency
- Where provisioning ends and configuration begins
- SSH key handling for automation
What’s next#
- Feed the Terraform output straight into the Ansible inventory, so there’s no copy-paste step.
- Store Terraform state remotely (S3 with locking) instead of on my laptop.
- Make the container port a variable, so the playbook can run the Project 1 image (port 8080) as well as the default
nginx:alpine.

