Terraform “Hello world”

Duminda Wijesinghe
Level Up Coding
Published in
3 min readNov 7, 2020

--

Sewing the Sail, Painting by Joaquín Sorolla

In an afternoon, I decided to learn Terraform to spin up an EC2 instance with all the necessary ports open and attach my existing key pair so that I can SSH into the box and start working. I wanted to set up the API I was building at the time in it and invite my client for a quick test round.

Terraform was something I always wanted to try from the first day I heard about it. Moving back and forth in the AWS console didn’t feel productive at all.

The setup

The following diagram explains what I wanted to achieve.

Install Terraform

Installation was easy enough. I’m on Linux by the way. https://learn.hashicorp.com/tutorials/terraform/install-cli?in=terraform/aws-get-started

curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"sudo apt-get update && sudo apt-get install terraform

Configure AWS

For this step, we need an AWS account and AWS CLI installed.

aws configure 

AWS CLI will prompt us for the AWS Access Key ID and the Secret Access Key. After finished, the credentials will be stored in ~/.aws/credentials file.

Initialize new project

mkdir terraform-aws-dev-boxcd terraform-aws-dev-boxtouch dev-box-setup.tf 

We should include the following config in touch dev-box-setup.tf .

terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 2.70"
}
}
}

provider "aws" {
profile = "default"
region = "us-east-1"
}

Then we can initialize by running the below command.

terraform init 

This will pull the provider aws from the Terraform registry registry.terraform.io/hashicorp/aws. profile = "default" means that terraform will use credentials for the AWS default profile.

Now we can specify the other resources and the relationships among them.

The Magic

Finally, it’s time for us to test it. But before going forward, we should get our configuration validated.

terraform validate

This will come back with errors if there are any invalid declarations otherwise it will give us a success message.

Success! The configuration is valid.

Finally, let's apply the configuration.

terraform apply

Then Terraform will show us a list of changes that it’s going to make. We will be prompted whether we want to apply the listed changes. Let’s say yes.

Do you want to perform these actions?
Terraform will perform the actions described above.
Only 'yes' will be accepted to approve.

Enter a value: yes

Wait for it to run and then let’s head over to the AWS console.

🎉 We are blessed with a fresh ec2 instance. In the security section, we can see all the required inbound rules.

Conclusion

Once the initial Terraform setup is done, it takes a few seconds to spin up new resources and we can replicate it without missing a single config. This will help us to keep our infrastructure consistent plus it will vastly improve productivity. Now we can spend our time building the actual product, not browsing the AWS console.

--

--