DEV Community

Rishabh Verma
Rishabh Verma

Posted on

Automate Your Cloud In 15 Minutes | Terraform Guide

I am tired of reading so much crap whenever I read an article.

They just yap about all the unnecessary stuff and waste so much time.

I decided to create a quality post that will save your valuable time.

So I spent 3 hours compressing knowledge about Terraform and Simplify as much as possible and presented to you in a format that will give you unfair advantage.

Here we goooo->

Image description

What if you execute a lines of script and the whole damn website is hosted with proper Https, security, backend and all??

yeah thats what terraform is capable of

Lets take an example to better understand.

  • Imagine you have a GF. (I say 'imagine' for a reason ☺️)
  • You're tired of making your own sandwich daily.
  • You ask your GF to make one but she doesn't know how to make a sandwich.
  • so you write a instruction and give it to her.
  • Now, every time you want one, you just tell your GF to make a sandwich, and she reads the instructions and make it for you.

so Terraform works similar to this.

Terraform in 4 Simple Commands

1) terraform init:

  • This command is basically like buying food from the store before actually cooking the sandwich.
  • The terraform init command is the first command you run when you start a new Terraform project. It downloads necessary plugins and libraries.
  • Run this command the first time you set up a project or whenever you add new providers.

2) terraform plan:

  • Before actually making the sandwich, your GF tells you, "Hey, I'm making a spicy sandwich with some pineapple on it. Is that okay?"
  • So this command tells you what changes it will make to your cloud. Remember to use this command before making changes.
  • Run this command to see a preview of the changes before applying them. It helps you avoid mistakes.

3) terraform apply:

  • You might answer your GF, "Hell yeah! I like sandwiches with pineapple on them."
  • This command is like you confirming that the action Terraform is about to take is okay with you. It’s like giving permission to make changes to your cloud.
  • Run this command when you’re ready to make the changes to your cloud infrastructure.

4) terraform destroy:

  • "Yo, WTF! Why did you add apple to my sandwich? Please throw this shit in the dustbin where it belongs."
  • Basically This command deletes the resources it created.
  • Run this command when you want to *remove all the resources *

Now let's roll up our sleeves and get our hands dirty with Terraform!😈

Image description

Sure! Here’s a step-by-step guide to installing Terraform and writing your first Terraform code.

Step 1: Install Terraform

For Windows

  1. Download Terraform:

  2. Extract the Zip File:

    • Extract the downloaded .zip file to a location of your choice.
  3. Add Terraform to System Path:

    • Open the folder where you extracted terraform.exe.
    • Copy the path to this folder.
    • Open the Start Menu, search for "Environment Variables", and select "Edit the system environment variables".
    • In the System Properties window, click on "Environment Variables".
    • In the "System variables" section, find and select the Path variable, then click "Edit".
    • Click "New" and paste the path you copied. Click "OK" to close all windows.
  4. Verify Installation:

    • Open Command Prompt and type:

    terraform --version

Image description

  • you will see something like this.
  • Since Terraform is added to your computer. I recommend you to download Terraform Extension in VS code

Step 2: Writing Your First Terraform Code

  • Inside your project directory, create a file named **main.tf**.
  • Open main.tf in a Vs Code (terraform files always have .tf extension) and add the following code to create a simple AWS EC2 instance:

Image description

To get Ami id you have to create your own ami by starting an ec2 instance and then creating ami of it.

Initialize Terraform:

  • After you write the above code.
  • Run the following command to initialize the project. terraform init
  • This downloads the necessary provider plugins and sets up the environment:

Preview the Changes:

  • To see what Terraform will do before applying changes, run:

    terraform plan

Apply the Configuration:

  • To create the resources defined in your main.tf file, run:

    terraform apply

  • Terraform will show you a summary of what will be created and ask for your confirmation. Type yes to proceed.

Verify the Resources:

  • Log into your AWS Management Console (or the cloud provider’s console) and check that the resources (e.g., EC2 instance) have been created as specified.

Removing the Resources:

  • Run this Command to delete the resource you just have created
  • terraform destroy
  • Terraform will prompt you to confirm before deleting anything.
  • Type yes to proceed.
  • Remember to run the Terraform Destroy after you done. or you get charged for running EC2 Instance .

If you want to become Elite terraformer Or want to get some help.
Just Connect me on Linkedin!. I will be glad to help you :)

My linkedin profile

Top comments (0)