↓ Skip to main content
Infrastructure as Code: Provision a Server
  1. Projects/

Infrastructure as Code: Provision a Server

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
#

LayerToolWhy this one
CloudAWS EC2 (t2.micro)Free-tier eligible, and the provider I’m certified in
ProvisioningTerraform + AWS provider ~> 5.0Declarative, shows a plan before changing anything, tracks what it created
OS imageUbuntu 24.04, looked up by a data sourceAlways the latest official Canonical image, never a hard-coded AMI ID
ConfigurationAnsibleAgentless (only needs SSH) and idempotent, so it can be re-run safely
FirewallAWS security group + UFWTwo layers: one at the network edge, one on the host
RuntimeDocker (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_cidr set in terraform.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.example to 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 .example templates 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.ini before adding it to .gitignore, and ignoring a file doesn’t untrack it once it’s committed. Fix: removed it from Git with git rm --cached, ignored it, and kept a hosts.ini.example instead. The lesson: the IP is still in the history, so set up .gitignore before 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.

Code
#

Related