Day 60 - Mastering Terraform ๐Ÿš€๐Ÿ”ฅ

Day 60 - Mastering Terraform ๐Ÿš€๐Ÿ”ฅ

ยท

6 min read

Hello Learners,

You guys are doing a fantastic job creating EC2 instances (mostly) manually. Today, letโ€™s automate this process. How? Well, Terraform is the solution! ๐ŸŒŸ


What is Terraform? ๐ŸŒ

ใ„ใพใ•ใ‚‰ใ ใ‘ใฉTerraformใ‚’ๅฐŽๅ…ฅใ—ใฆใ‚ปใ‚ญใƒฅใƒชใƒ†ใ‚ฃใ‚ฐใƒซใƒผใƒ—ใฎ็ฎก็†ใ‚’ใ‚ณใƒผใƒ‰ๅŒ–ใ—ใŸใ‚ˆ๏ฝœOPTEMO ใ‚จใƒณใ‚ธใƒ‹ใ‚ขใƒ–ใƒญใ‚ฐ

Terraform is an Infrastructure as Code (IaC) tool that allows you to create, manage, and update infrastructure resources such as virtual machines, networks, and storage in a repeatable, scalable, and automated way.

Task 1: Install Terraform on Your System ๐Ÿ–ฅ๏ธ

Let's get Terraform installed on your AWS EC2 instance step-by-step. Follow these instructions to get up and running! ๐Ÿš€๐ŸŒŸ

Step 1: Launch an EC2 Instance ๐ŸŒฅ๏ธ

  1. Open the AWS Management Console:

    • Navigate to the EC2 Dashboard ๐Ÿ”.

  2. Launch a New Instance:

    • Click on "Launch Instance" ๐Ÿ†•.

    • Choose an Ubuntu AMI (Amazon Machine Image). For example, select "Ubuntu Server 20.04 LTS (HVM), SSD Volume Type" ๐Ÿ–ฅ๏ธ.

    • Select the instance type (e.g., t2.micro which is free tier eligible) ๐Ÿ†“.

  3. Configure Instance Details:

    • Keep the default settings for "Configure Instance" โš™๏ธ.

    • Add storage if needed (8 GB default is usually sufficient) ๐Ÿ’พ.

  4. Add Tags:

    • Optionally, add a tag to name your instance (e.g., Key: Name, Value: Terraform-Instance) ๐Ÿท๏ธ.
  5. Configure Security Group:

    • Create a new security group or select an existing one ๐Ÿ›ก๏ธ.

    • Add a rule to allow SSH (port 22) access from your IP ๐Ÿ”“.

  6. Review and Launch:

    • Review your instance configuration ๐Ÿง.

    • Click on "Launch" ๐Ÿš€.

    • Select an existing key pair or create a new one and download it ๐Ÿ”‘.

Step 2: Connect to Your EC2 Instance ๐Ÿ”—

  1. Open Terminal:

    • If you're on Windows, you can use PuTTY . On macOS or Linux, use the terminal ๐Ÿง๐Ÿ.
  2. Connect via SSH:

    • Use the following command to connect to your instance:

        ssh -i /path/to/your-key.pem ubuntu@your-ec2-public-ip
      
    • Replace /path/to/your-key.pem with the path to your key file and your-ec2-public-ip with your instance's public IP address ๐ŸŒ.

Step 3: Update the Package List โณ

  1. Update the package list:

     sudo apt-get update
    

    • This command updates the list of available packages and their versions ๐Ÿ†™.

Step 4: Install Terraform ๐Ÿ’ป

  1. Install the Required Dependencies:

    • Install software-properties-common package:

        sudo apt-get install -y software-properties-common
      

    • This package allows you to easily manage your distribution and independent software vendor software sources ๐Ÿ› ๏ธ.

  2. Add the HashiCorp GPG Key:

    • Add the GPG key for HashiCorp products:

        wget -O- https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
      

    • This key is used to sign the Terraform packages, ensuring their authenticity ๐Ÿ”.

  3. Add the Official HashiCorp Linux Repository:

    • Add the repository to your system:

        sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
      

    • This adds the repository for HashiCorp products to your list of repositories ๐Ÿ“ฆ.

  4. Update the Package List Again:

    • Update the package list to include the new repository:

        sudo apt-get update
      

    • This ensures the latest packages from the new repository are included in your package list ๐Ÿ†•.

  5. Install Terraform:

    • Finally, install Terraform:

        sudo apt-get install -y terraform
      

    • This command installs Terraform on your system, making it ready for use ๐Ÿ› ๏ธ.

Step 5: Verify the Installation โœ…

  1. Check the Terraform Version:

    • Verify that Terraform is installed correctly:

        terraform -version
      

    • You should see the version of Terraform displayed in the terminal, indicating that the installation was successful ๐ŸŽ‰.

Conclusion ๐ŸŽŠ

Congratulations! ๐ŸŽ‰ You've successfully installed Terraform on your AWS EC2 instance. You're now ready to start using Terraform to manage your infrastructure as code. Stay tuned for more tasks and happy automating! ๐Ÿš€โœจ

Task 2: Answer the Following Questions โ“

Why do we use Terraform? ๐Ÿค”

Why use Terraform? โ€“ O'Reilly

Terraform is used to automate the provisioning of infrastructure. It allows for the creation, updating, and versioning of your infrastructure safely and efficiently. By defining your infrastructure as code, you can easily replicate your environment and manage it with version control, reducing errors and manual intervention. ๐Ÿ“ˆโœจ

What is Infrastructure as Code (IaC)? ๐Ÿ’ป

Infrastructure as Code - Opstimus

Infrastructure as Code (IaC) is the practice of managing and provisioning computing infrastructure through machine-readable definition files, rather than through physical hardware configuration or interactive configuration tools. IaC allows for consistency, repeatability, and automation in infrastructure management. ๐Ÿ› ๏ธ๐Ÿ”„

What is a Resource? ๐Ÿ“ฆ

What is Terraform

In Terraform, a resource is a component of your infrastructure, such as a virtual machine, storage bucket, or network. Resources are the basic building blocks of your infrastructure, and Terraform allows you to define these resources in configuration files. ๐Ÿ—๏ธ๐Ÿ”ง

What is a Provider? ๐Ÿ”Œ

Terraform by HashiCorp

A provider in Terraform is a plugin that allows Terraform to interact with cloud providers, SaaS providers, or other APIs. Providers offer a set of resource types and data sources that Terraform can manage. For example, the AWS provider enables Terraform to create and manage AWS resources. ๐ŸŒโ˜๏ธ

What is a State File in Terraform? ๐Ÿ“„

Terraform stacks, explained

The state file in Terraform is a critical component that keeps track of the current state of your infrastructure. It records the mappings between the resources defined in your configuration files and the actual resources in the real world. The state file is important because it helps Terraform determine the changes that need to be applied to achieve the desired state of the infrastructure. ๐Ÿ“Š๐Ÿ”

What is Desired and Current State? ๐Ÿ“ˆ๐Ÿ“‰

  • Desired State: This is the state defined in your Terraform configuration files, representing how you want your infrastructure to look. ๐Ÿ“‹โœจ

  • Current State: This is the state of your infrastructure as recorded in the Terraform state file. It represents the actual state of your managed resources at a given time. ๐Ÿ“œ๐Ÿ”„

Terraform compares the desired state with the current state to determine what changes are necessary to align the two. โš–๏ธ๐Ÿ”ง

You can prepare for tomorrow's task from here ๐Ÿš€๐Ÿš€

We hope these tasks will help you understand how to write a basic Terraform configuration file and basic commands on Terraform.

Sample Terraform Configuration File ๐Ÿ“„

Here's a basic example to get you started:

provider "aws" {
  region = "us-west-2"
}

resource "aws_instance" "example" {
  ami           = "ami-0c55b159cbfafe1f0"
  instance_type = "t2.micro"

  tags = {
    Name = "example-instance"
  }
}

Basic Terraform Commands ๐Ÿ“

  • terraform init: Initialize a working directory containing Terraform configuration files. ๐Ÿ› ๏ธ

  • terraform plan: Create an execution plan to preview the changes that Terraform will make to your infrastructure. ๐Ÿ—‚๏ธ๐Ÿ”

  • terraform apply: Apply the changes required to reach the desired state of the configuration. ๐Ÿš€๐Ÿ”ง

  • terraform destroy: Destroy the Terraform-managed infrastructure. ๐Ÿ’ฅ๐Ÿ—‘๏ธ

By following these steps, youโ€™ll automate the process of creating and managing your infrastructure, making it more efficient and less prone to manual errors.

Happy automating with Terraform! ๐ŸŒŸ๐Ÿค–

#DevOps #Terraform #90DaysOfDevOpsChallenge ๐Ÿš€

Did you find this article valuable?

Support Nilkanth Mistry by becoming a sponsor. Any amount is appreciated!

ย