48 ‐ Introduction to Infrastructure as a Code(IaaC) with Terraform - SanjeevOCI/Ocidocs GitHub Wiki

Introduction to Infrastructure as Code (IaC) with Terraform

Infrastructure as Code (IaC) is a key DevOps practice that involves managing and provisioning computing infrastructure through machine-readable configuration files, rather than through physical hardware configuration or interactive configuration tools. Terraform, developed by HashiCorp, is a popular open-source IaC tool that allows you to define and provision infrastructure using a high-level configuration language.

Key Concepts of Terraform

  1. Configuration Files:

    • Terraform uses configuration files written in HashiCorp Configuration Language (HCL) to describe the desired state of your infrastructure.
    • These files typically have a .tf extension.
  2. Providers:

    • Providers are responsible for understanding API interactions and exposing resources. Examples include AWS, Azure, Google Cloud, and Oracle Cloud Infrastructure (OCI).
    • Each provider requires configuration to specify credentials and other settings.
  3. Resources:

    • Resources are the most important element in the Terraform language. Each resource block describes one or more infrastructure objects, such as virtual networks, compute instances, or databases.
  4. State:

    • Terraform maintains a state file to keep track of the resources it manages. This state file is used to map real-world resources to your configuration and to determine what changes need to be applied.

Example: Setting Up a Simple OCI Infrastructure with Terraform

Here is a simple example of how to use Terraform to set up a basic infrastructure in Oracle Cloud Infrastructure (OCI).

Step 1: Install Terraform

Download and install Terraform from the official website.

Step 2: Configure OCI Provider

Create a provider.tf file to configure the OCI provider:

provider "oci" {
  tenancy_ocid     = "ocid1.tenancy.oc1..exampleuniqueID"
  user_ocid        = "ocid1.user.oc1..exampleuniqueID"
  fingerprint      = "20:3b:97:13:55:1c:6d:1a:1f:8b:8f:1d:9b:3e:4b:1a"
  private_key_path = "/path/to/your/private_key.pem"
  region           = "us-ashburn-1"
}

Step 3: Define Resources

Create a main.tf file to define the resources. For example, to create a VCN and a subnet:

resource "oci_core_vcn" "example_vcn" {
  cidr_block     = "10.0.0.0/16"
  display_name   = "example_vcn"
  compartment_id = "ocid1.compartment.oc1..exampleuniqueID"
}

resource "oci_core_subnet" "example_subnet" {
  cidr_block       = "10.0.1.0/24"
  vcn_id           = oci_core_vcn.example_vcn.id
  display_name     = "example_subnet"
  compartment_id   = "ocid1.compartment.oc1..exampleuniqueID"
  dns_label        = "example"
  prohibit_public_ip_on_vnic = false
}

Step 4: Initialize Terraform

Run the following command to initialize Terraform. This will download the necessary provider plugins:

terraform init

Step 5: Plan and Apply

Run the following commands to plan and apply the changes:

terraform plan
terraform apply

Conclusion

By using Terraform, you can define your infrastructure as code, making it easier to manage, version, and automate. This approach brings consistency and repeatability to your infrastructure provisioning process, reducing the risk of human error and improving overall efficiency.

Example Script: Disk Usage Monitoring

Here is an example of a simple shell script to monitor disk usage and highlight usage above 85%:

#!/bin/bash

# Get the disk usage information
df_output=$(df -h)

# Print the header
echo "$df_output" | head -n 1

# Iterate over each line of the df output
echo "$df_output" | tail -n +2 | while read -r line; do
  # Extract the usage percentage
  usage=$(echo $line | awk '{print $5}' | sed 's/%//')

  # Check if the usage is above 85%
  if [ "$usage" -gt 85 ]; then
    # Highlight the line
    echo -e "\e[31m$line\e[0m"
  else
    # Print the line normally
    echo "$line"
  fi
done

This script retrieves disk usage information, checks if the usage is above 85%, and highlights the lines where the usage exceeds the threshold.