Skip to main content

Command Palette

Search for a command to run...

How to create a file using terraform

Published
3 min read
P

👋 Hello, and welcome to my DevOps journey! 🚀 I am Priyanka Varshney,🛠️ As an aspiring DevOps engineer, I'm all about bridging the gap between development and operations, making software delivery seamless and efficient. 💻🔧 On this Hashnode blog, I'll be sharing my learnings, experiences and adventures as I dive deep into the world of continuous integration, automation, and cloud technologies. ☁️⚙️ Let's connect, learn, and grow as a vibrant DevOps community. Follow my Hashnode blog, and let's embrace the DevOps adventure together! 🤝🔗

Terraform Tutorial for Beginners : Everything You Should Know

Introduction

Terraform is an Infrastructure as Code (IaC) tool that allows you to define and provision infrastructure using a declarative configuration language (HCL - HashiCorp Configuration Language).

With Terraform, infrastructure can be versioned, reused, and shared just like application code. It supports major cloud providers like AWS, Azure, and GCP, and can manage anything from VMs and storage to DNS and networking.

One of its key features is the execution plan (terraform plan) that shows what will change before applying, and state management to track real-world resources.

Basic Commands :

terraform init # Initialize Terraform in a directory 
terraform plan # Preview changes before applying 
terraform apply # Deploy infrastructure 
terraform destroy # Delete all resources

Install Terraform:(Linux )

Launch an EC2 Instance:

  1. Create an EC2 instance with 15GB of storage.

    • Install Terraform:

      • Start by updating your package index and installing prerequisites:

          sudo apt-get update && sudo apt-get install -y gnupg software-properties-common
        
      • Add the HashiCorp GPG key:

          wget -O- https://apt.releases.hashicorp.com/gpg | \
          gpg --dearmor | \
          sudo tee /usr/share/keyrings/hashicorp-archive-keyring.gpg > /dev/null
        
      • Verify the GPG key’s fingerprint:

          gpg --no-default-keyring \
          --keyring /usr/share/keyrings/hashicorp-archive-keyring.gpg \
          --fingerprint
        
      • Add the official HashiCorp repository to your sources list. Use the lsb_release -cs command to retrieve your system's codename (e.g., buster, groovy, sid):

          echo "deb [signed-by=/usr/share/keyrings/hashicorp-archive-keyring.gpg] \
          https://apt.releases.hashicorp.com $(lsb_release -cs) main" | \
          sudo tee /etc/apt/sources.list.d/hashicorp.list
        
      • Update your package list:

          sudo apt update
        
      • Finally, install Terraform:

          sudo apt-get install terraform
          terraform --version
        

Now you can create terraform file using the following commands :

  1. Open the command prompt and create a main.tf
vim main.tf
  1. You can include the block, parameter, and argument as follows:
  • Block: A structural element in Terraform (e.g., resource, variable, output).

  • Parameter: Input settings or attributes used inside a block (e.g., default in a variable block).

  • Argument: Key-value pairs within a block that define configurations (e.g., ami = "ami-12345678").

  1. You can use terraform init to initialize the Terraform file and then validate it with terraform validate -

     terraform init 
     terraform validate
    

  2. You can run terraform plan.

    This command allows you to preview the modifications Terraform will make to your infrastructure before applying them. It provides insights into the planned actions without executing them.

terraform plan

  1. You can run terraform apply
terraform apply

You can do ls to check the list of files , then check the automate.txt file by using the cat command.

  1. You can delete as well by using the terraform destroy
terraform destroy

To skip the manual confirmation process, you can use terraform apply -auto-approve

In Terraform, the -auto-approve flag is used with the terraform apply or terraform destroy commands. It automatically approves the execution plan without prompting for user confirmation. This is particularly helpful in automated workflows or scripts.

terraform apply --auto-approve
terraform destroy --auto-approve

Thank you for reading :-)