Day-1-terraform

Getting Started with Terraform: Day 1 of the Zero to Hero Series

TLDR: In Day 1 of the Terraform Zero to Hero series, we explore the fundamentals of Infrastructure as Code, the significance of Terraform for DevOps and Cloud Engineers, installation instructions for various operating systems, and how to set up Terraform for AWS. We also write our first Terraform script to launch an EC2 instance and understand the Terraform lifecycle.

Welcome to Day 1 of the Terraform Zero to Hero series! In this series, we will cover everything you need to know about Terraform, starting from the basics and progressing to advanced concepts. This series is designed for beginners, so no prior knowledge of Terraform is required.

Terraform → iac/api as code
platform dependedNo (aws[CloudFormation template],azure[Resource Manager],gcp,openstack[Heat template]) —→ universal approach
coding requiredno
for creating resourceno manual click is required
lifecyclei-pad → (init,plan,apply,destroy)
github sandbox —> aws and terrform will be installed
fork repository —→ code → codespaces → + (free for 60/hr a month)
search → > Add dev container configuration files → modify your active configuration —→ search → terraform (verified) → ok → keep default
search → > Add dev container configuration files → modify your active configuration —→ search → aws cli (verified) → ok → keep default
search → > rebuilt containers → rebuild
.devcontainer → .devcontainer.json
→ terraform —version
→ aws —version
github sandbox authenticate with aws
iam user (top right) → security credentials → access key (card )→ create access key → [access key & secrete access key]
aws configure → access key : → secrete access key : → region : us-east1 → default output format : json
aws s3 ls —→ to know you are authenticated to aws

Overview of the Series

This series spans seven days, and each day will focus on different aspects of Terraform. For a detailed syllabus, you can refer to the GitHub repository named "Terraform Zero to Hero" available in the description. Additionally, a video titled "Day Zero" provides an overview of what you will learn throughout the series.

What is Infrastructure as Code?

Infrastructure as Code (IaC) is a key concept in modern DevOps practices. It allows you to manage and provision infrastructure through code rather than manual processes. This approach not only automates the setup of infrastructure but also ensures consistency and reduces the risk of human error.

Why Terraform?

Terraform is one of the most sought-after skills for DevOps and Cloud Engineers today. It provides a universal approach to managing infrastructure across various cloud providers like AWS, Azure, and Google Cloud Platform (GCP). Unlike other tools that are specific to a single provider, Terraform allows you to write your infrastructure code once and deploy it across multiple platforms.

Installing Terraform

Installation on Different Operating Systems

Installing Terraform is straightforward. Here are the steps for different operating systems:

  • Mac OS: Download the appropriate package from the Terraform website and follow the installation instructions.

  • Linux: Use the package manager for your distribution to install Terraform.

  • Windows: Download the installer and ensure that Terraform is added to your system PATH.

For those who may not have access to a personal laptop or face restrictions on their work machines, GitHub Codespaces offers a free environment to run Terraform without local installation. Codespaces provides a containerized environment with the necessary tools pre-installed.

Setting Up Terraform for AWS

To use Terraform with AWS, you need to authenticate Terraform to your AWS account. This involves creating an IAM user and generating access keys. Here’s how to do it:

  1. Log in to your AWS account and navigate to the IAM section.

  2. Create a new IAM user with programmatic access.

  3. Generate access keys for the user and save them securely.

  4. Run the command aws configure in your terminal to set up your AWS credentials.

Writing Your First Terraform Code

Now that we have Terraform installed and configured for AWS, let’s write our first Terraform script to launch an EC2 instance.

Creating the Terraform Configuration File

  1. Create a file named main.tf in your project directory.

  2. Define the provider and resource in the file:

     provider "aws" {
         region = "us-east-1"
     }
    
     resource "aws_instance" "example" {
         ami           = "ami-0c55b159cbfafe1f0"  # Replace with a valid AMI ID
         instance_type = "t2.micro"
     }
    
  3. Save the file.

Terraform Lifecycle Commands

To manage your infrastructure, Terraform uses a lifecycle of commands:

  • terraform init: Initializes the Terraform configuration and downloads necessary provider plugins.

  • terraform plan: Shows what actions Terraform will take to reach the desired state defined in your configuration.

  • terraform apply: Applies the changes required to reach the desired state.

  • terraform destroy: Removes all the resources defined in your configuration.

Running the Commands

  1. Open your terminal and navigate to the directory containing main.tf.

  2. Run terraform init to initialize the configuration.

  3. Run terraform plan to see what will be created.

  4. Run terraform apply to create the EC2 instance. Confirm the action when prompted.

  5. Finally, run terraform destroy to clean up the resources when you are done.

Understanding the Terraform State File

Terraform maintains a state file (terraform.tfstate) that records the current state of your infrastructure. This file is crucial for tracking resources and managing changes over time. In future lessons, we will delve deeper into managing state files and best practices for securing sensitive information.

Conclusion

In this first day of the Terraform Zero to Hero series, we covered the basics of Infrastructure as Code, the importance of Terraform, installation instructions, and how to set up Terraform for AWS. We also wrote our first Terraform script to launch an EC2 instance and learned about the Terraform lifecycle commands.