AWS Builder Center
My Terraform refresher on AWS: an S3 bucket, an EC2 server and a CI pipeline that failed first

My Terraform refresher on AWS: an S3 bucket, an EC2 server and a CI pipeline that failed first

What broke while I rebuilt my AWS basics in code, and how a GitHub Actions pipeline helped me catch and fix it.

Cloud & DevOps | Terraform, IAM, CI/CD | Writing about what breaks
I hold the AWS Certified Cloud Practitioner certification, and I'm building toward DevOps and MLOps. To go beyond the exam, I rebuilt the basics in code: an S3 bucket, an EC2 web server and a GitHub Actions pipeline to check it all.
It didn't go smoothly, and that's why I'm writing this. Here's what I built, what broke, and how I fixed it.

Setting up the safe way

I didn't work from the root account. I created an IAM user called  grace-admin , turned on MFA for it, and installed the AWS CLI and Terraform inside WSL (Ubuntu on Windows).
My first error was the classic  terraform: command not found . It simply meant Terraform wasn't installed yet. After adding HashiCorp's package source and installing it,  terraform -version  worked.
This one was embarrassing. While running aws configure, I pasted my access key into the wrong prompt, and then my secret key showed up on my screen. I took a screenshot to ask for help, and only afterwards realised the keys were visible in it. I deleted them in IAM straight away and made new ones. Lesson learned: check your screen before you screenshot.
With  aws sts get-caller-identity  returning my IAM user, I was ready to build.

Project 1: an S3 bucket

My first config was small: the AWS provider, the  us-east-1  region and one  aws_s3_bucket  resource.
resource "aws_s3_bucket" "demo" {
bucket = "my-unique-bucket-name"
}
The workflow was  terraform init ,  terraform plan ,  terraform apply , and then  terraform destroy  to clean up. Reading the plan before applying ( 1 to add, 0 to change, 0 to destroy ) is a habit I'm keeping.
I committed it to GitHub with a  .gitignore  that excludes state files and variable files, because state can contain sensitive data.


Project 2: an EC2 web server with Nginx

Next I described a small server in code:
  • A key pair for SSH access
  • A security group that allows SSH only from my own IP, and HTTP from anywhere
  • A  t3.micro  Amazon Linux 2023 instance that installs Nginx on first boot through  user_data 
  • An output that prints the server's public IP
After  terraform apply , I connected over SSH to check the server, then ran  terraform destroy  so nothing kept using my credits.
The two things I took from this project were locking SSH to my own IP instead of opening it to the whole internet, and using  user_data  so the server configures itself.

Terraform created the key pair, the security group and the instance in one apply.

The CI pipeline that failed on its first run

I added a GitHub Actions workflow that runs  terraform fmt -check  and  terraform validate  on every push.
It failed straight away. My code read the SSH public key from a file on my laptop, and that file doesn't exist on GitHub's clean machine:
public_key = file("~/.ssh/terraform-demo.pub")
The fix was to pass the key in as a variable instead of reading a local file:
public_key = var.public_key
The next run turned green.
This was my favorite part of the project. The code "worked on my machine," but the pipeline caught something I couldn't see locally. That's exactly what CI is for.

My first pipeline run failed, and the second passed after the fix.

What I learned

  • Plan before apply, and destroy practice infrastructure when you're done.
  • Keep state files, keys and tokens out of Git.
  • Don't open SSH to the whole internet.
  • CI catches problems that only exist on your own machine.
  • Writing infrastructure in code means I can rebuild it in seconds.

What's next

  • Remote state in S3
  • A  terraform plan  step in the pipeline, with secure AWS access
  • Docker and containers
  • Bringing the same approach to ML and generative AI workloads

Repos

Any opinions in this article are those of the individual author and may not reflect the opinions of AWS.
Enjoyed reading this content? Let the author know!

Your likes, comments, shares, and saves help creators reach more builders.

Loading recommendations

Loading article