TerraWeek Day:1

TerraWeek Day:1

Introduction to Terraform and Terraform Basics

1.What is Terraform and how can it help you manage infrastructure as code?

  • What is Terraform ?

Terraform is an open-source infrastructure as a code software tool created by HashiCorp. It enables you to safely and predictably create, change, and improve infrastructure. Terraform codifies all infrastructure as configuration and applies that configuration to real resources.

Terraform uses declarative language to describe your infrastructure resources. This means that you describe the desired state of your infrastructure, and Terraform will figure out how to get there. This makes it much easier to manage infrastructure, as you don't have to worry about the details of how Terraform will actually provision your resources.

  • Why Terraform?

Terraform is a popular infrastructure as code (IaC) tool that allows you to manage cloud resources in a declarative way. It maintains state, supports various cloud providers, and provides flexibility to define and manage resources of any kind. Its robust community and ecosystem offer pre-built modules and plugins to extend functionality.

  • Infrastructure as a Code

Infrastructure as code (IaC) is the practice of defining and managing infrastructure resources, such as servers, databases, and networks, using code instead of manual processes. By using Terraform, infrastructure can be treated like a software product, with changes tracked, versioned, and tested like any other code. Moreover, this enables teams to automate provisioning, make code changes, and apply changes with speed and consistency across different environments, making infrastructure management more efficient, reliable, and easier to maintain.

2.Why do we need Terraform and how does it simplify infrastructure provisioning?

  • Terraform simplifies infrastructure provisioning by providing a single, consistent way to define and manage infrastructure across multiple cloud providers. This allows IT teams to automate provisioning workflows, maintain consistency and manage resources at scale, leading to increased efficiency and reduced complexity.

  • Terraform is important because it helps simplify infrastructure provisioning in the following ways:

    1. Infrastructure as Code: With Terraform, infrastructure provisioning is defined and managed using code that can be versioned, tested, and audited. This provides greater consistency, repeatability, and scalability compared to manual processes.

    2. Multi-Cloud Support: Terraform provides a consistent workflow across multiple cloud providers, allowing developers to avoid vendor lock-in and deploy infrastructure on several providers.

    3. Declarative Model: Terraform uses a declarative language to define infrastructure, allowing developers to express the desired end-state of the infrastructure without specifying how the infrastructure should be created. Terraform applies the changes needed to make the current state match the desired state, abstracting away the underlying complexity of infrastructure provisioning.

    4. Resource Dependencies: Terraform has built-in support for managing resource dependencies and allowing for multi-stage deployment. For example, a virtual machine cannot be created before the network infrastructure has been created, and Terraform ensures that dependent resources are created in the right order.

    5. Change Management: Terraform provides change management capabilities such as diff reporting, execution planning, and resource locking to help ensure changes are safe and predictable.

3.How can you install Terraform and set up the environment for AWS, Azure, or GCP?

  1. Download and Install Terraform: You can download the latest version of Terraform for your operating system from the official website - Terraform website and Install it following the instructions provided.

  2. For Ubuntu, we can use these commands to install terraform.

        wget -O- https://apt.releases.hashicorp.com/gpg | sudo gpg --dearmor -o /usr/share/keyrings/hashicorp-archive-keyring.gpg
        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
        sudo apt update && sudo apt install terraform
    
  3. To verify the installation, run the following command in the terminal.

        terraform version
    
  4. Set Up Credentials for Cloud Providers: To use Terraform with cloud providers such as AWS, Azure, or GCP, you will need to set up the necessary credentials to authenticate Terraform with the cloud providers'

    services:

a. AWS: create an IAM user with the appropriate permissions, and set the AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY environment variables.

  • Set up AWS Credentials

    • Navigate to the IAM console, and click on "Users".

    • Click on the "Add user" button to create a new IAM user.

    • Enter name and click on next.

    • On the "Set permissions" page, we can assign policies to the user. Select the appropriate policies.

    • Review the user's configuration details. If everything looks correct, click on "Create user" to create the IAM user.

    • After the user is created click on the security credentials tab of the user.

    • Click on create an access key.

    • Select CLI and click on next.

  • Access keys and secrets are generated, copy from here and download as we

b. Azure: create a service principal with the appropriate permissions, and set the AZURE_CLIENT_ID, AZURE_CLIENT_SECRET, AZURE_SUBSCRIPTION_ID, and AZURE_TENANT_ID environment variables.

c. GCP: create a service account with the appropriate permissions, and set the GOOGLE_CREDENTIALS environment variable to the path of the JSON-formatted service account key file.

  1. Define the Infrastructure resources: Create a Terraform configuration file (.tf) and define your infrastructure resources such as compute instances, storage, network, etc., using the Terraform Resource Configuration Language (RCL).

  2. Initialize and apply the Infrastructure Changes: Initialize the Terraform configuration files with the command

        terraform init
    
  3. And then apply the infrastructure changes with the terraform apply command.

        terraform apply
    
  1. To destroy the structure use the below commands.

        terraform destroy
    

    4.Explain the important terminologies of Terraform with the example at least (5 crucial terminologies).

  1. Resource: A resource is a unit of infrastructure that Terraform can create, update, or delete. Resources can be anything from virtual machines to databases to load balancers.

For example, the following code creates a new AWS EC2 instance:

resource "aws_instance" "example" {
  ami = "ami-0123456789abcdef0"
  instance_type = "t2.micro"
}
  1. Provider: A provider is a plugin that allows Terraform to interact with a specific cloud platform. Terraform has providers for AWS, Azure, Google Cloud Platform, and many other platforms.

For example, the following code specifies that the aws_instance resource will use the AWS provider:

provider "aws" {
  region = "us-east-1"
}
  1. Plan: A plan is a description of the changes that Terraform will make to your infrastructure. Terraform generates a plan before it makes any changes, so you can review the plan and make sure that Terraform is going to do what you expect.

For example, the following command will generate a plan for the aws_instance resource:

terraform plan
  1. Apply: The apply command tells Terraform to make the changes that are specified in the plan. Terraform will create, update, or delete resources as necessary to bring your infrastructure into the desired state.

For example, the following command will apply the plan that was generated for the aws_instance resource:

terraform apply
  1. Statefile: The statefile is a file that Terraform uses to track the current state of your infrastructure. The statefile includes information about the resources that have been created, their configuration, and their current state.

Terraform automatically creates and manages the statefile for you. You should never edit the statefile directly.

These are just a few of the important terminologies of Terraform.

Thank you,