DEV Community

AZEEZ KAMALDEEN
AZEEZ KAMALDEEN

Posted on

I Created My First AWS EC2 Instance with Terraform

I recently started working with the AWS console and Elastic Cloud Computing(Ec2), and let me tell you, it has been so much fun

But there’s something even better.

I deployed the exact same server with a very few lines of code.

It wasn’t perfect, there were errors and confusions, and I visited Stack Overflow more than I ever have. But when Terraform apply finally completed with no errors, and I refreshed the AWS console to see my instance running, I realized I had just crossed a huge milestone in my cloud journey.

Why I Decided to Learn Terraform

I’ve been learning AWS for a while now, and I noticed a few problems.

  1. I was clicking everything manually,
    Launch Instance
    Choose AMI
    Choose Instance Type
    Configure Storage
    Security Groups
    Key Pair
    Networking
    I know this doesn’t seem like much, but when you start working with large servers and identical servers, it starts to get tedious.

  2. I forget what I configured sometimes.

Now I know this sounds like a skill issue, but sometimes I forget what I configured. I mean, we have the security groups, inbound rule, outbound rule, subnets, route tables, internet gateway, VPC, and believe me, I’m not remembering all of that ten minutes later.

But that’s where Terraform comes in.

Everything is written in code.

resource “aws_security_group” “web” {

No guessing

3.I’m very clumsy, and sometimes I accidentally click the wrong things

I think everyone eventually does this sometimes

Delete a security group.

Delete a subnet.

Terminate an EC2.

Terraform fixes this

You review

terraform plan
before

terraform apply
Nothing changes accidentally.

Become a Medium member
4.Hidden costs.

Back to my issue of forgetfulness, there are times when I forget I created Elastic IPs or EBS gateways, and then I just wake up to a bill

But Terraform fixes this

Everything Terraform creates can be destroyed in one command:

terraform destroy
Reducing the chance of forgotten resources continuing to incur charges.

My First Terraform Configuration

Well, it didn’t really go well the first time

The reality wasn’t as smooth as tutorials

I got errors.

The AMI I wanted didn’t exist in my region.

My key pair wasn’t found.

SSH refused to let me into my instance.

Sometimes Terraform said everything was fine, but AWS showed something completely different.

Each error forced me to slow down and actually understand what Terraform was trying to do instead of blindly following a tutorial.

At its core, my configuration was very basic

I defined:

An AWS provider
An EC2 instance resource
The instance type
The AMI
My key pair
Basic networking settings
Then came the commands that every article and tutorial I watched were spamming :

terraform init
terraform plan
terraform apply
The first initializes the project.

The second shows exactly what Terraform intends to create.

The third makes it happen.

The moment Terraform finished, I refreshed the AWS Console.

There it was.

A brand-new EC2 instance.

Created entirely from code.

Top comments (0)