48. Terraform Basics: Understanding Infrastructure as Code - Ayushi-srivastav/OCI GitHub Wiki

Infrastructure as a Code (IaaC)


Infrastructure as a Code (IaaC) is a modern approach to managing and provisioning computing infrastructure using machine-readable scripts, rather than physical hardware configuration or interactive tools. It brings several benefits such as consistency, repeatability, version control, and automation, making it an essential concept in DevOps and cloud computing.

Terraform


Terraform, developed by HashiCorp, is one of the most popular tools for implementing IaaC. It allows you to define infrastructure in a high-level configuration language called HashiCorp Configuration Language (HCL). With Terraform, you can create, update, and manage resources across multiple cloud providers like AWS, Azure, Google Cloud, and more.

Key Concepts in Terraform


Providers:

The OCI provider in Terraform allows it to interact with OCI services. It manages the authentication and API interactions necessary to create and manage OCI resources.

Resources:

Resources are the building blocks of your OCI infrastructure. These can include compute instances, block storage volumes, networking components, and more.

Modules:

Modules are reusable containers for Terraform configurations that can be shared and used across different projects. They help maintain consistent infrastructure setups.

State Management:

Terraform keeps track of your infrastructure state using state files, ensuring that it knows what resources are managed and their current status.

Execution Plans:

Terraform provides an execution plan that previews the changes it will make to your infrastructure, allowing you to review and approve changes before they are applied.

Benefits of Using Terraform


Consistency: Define your infrastructure as code to ensure consistent configurations.

Scalability: Easily scale your infrastructure up or down as needed.

Version Control: Track changes to your infrastructure definitions using version control systems like Git.

Automation: Automate the deployment and management of OCI resources, reducing manual intervention and errors.

Multi-Cloud Support: Manage resources across different cloud providers with a single tool.

Example Configuration with OCI


Let's create a simple setup that includes a Virtual Cloud Network (VCN), a subnet, and a compute instance in OCI using Terraform.

Step 1: Provider Configuration

First, configure the OCI provider with your credentials. Save the following in a file named provider.tf:

provider "oci" {
  tenancy_ocid     = "ocid1.tenancy.oc1..xxxxxxEXAMPLExxxxxx"
  user_ocid        = "ocid1.user.oc1..xxxxxxEXAMPLExxxxxx"
  fingerprint      = "12:34:56:78:90:AB:CD:EF:12:34:56:78:90:AB:CD:EF"
  private_key_path = "/path/to/private_key.pem"
  region           = "us-ashburn-1"
}

Step 2: Virtual Cloud Network (VCN) and Subnet

Next, create a VCN and a subnet within the VCN. Save the following in a file named network.tf:

resource "oci_core_vcn" "example_vcn" {
  cidr_block = "10.0.0.0/16"
  display_name = "ExampleVCN"
  compartment_id = var.compartment_id
}
resource "oci_core_subnet" "example_subnet" {
  cidr_block = "10.0.1.0/24"
  vcn_id = oci_core_vcn.example_vcn.id
  display_name = "ExampleSubnet"
  compartment_id = var.compartment_id
}

Step 3: Compute Instance

Create a compute instance and attach it to the subnet. Save the following in a file named compute.tf:

resource "oci_core_instance" "example_instance" {
  availability_domain = "Uocm:PHX-AD-1"
  compartment_id = var.compartment_id
  shape = "VM.Standard2.1"

  create_vnic_details {
    subnet_id = oci_core_subnet.example_subnet.id
  }

  source_details {
    source_type = "image"
    source_id = "ocid1.image.oc1..xxxxxxEXAMPLExxxxxx"  # Replace with a valid image OCID
  }

  metadata = {
    ssh_authorized_keys = file("~/.ssh/id_rsa.pub")
  }
}

Step 4: Initialize and Apply Configuration

Navigate to your configuration directory and run the following commands:

Initialize Terraform: Set up the working directory and download necessary plugins.

terraform init

Plan Changes: Preview the changes Terraform will make to your infrastructure.

terraform plan

Apply Configuration: Apply the changes to provision the resources.

terraform apply

Destroy Infrastructure: When resources are no longer needed, use terraform destroy to clean up.

terraform destroy

Conclusion


Using Terraform with OCI allows you to automate and manage your cloud infrastructure efficiently. By defining your infrastructure as code, you can achieve consistency, repeatability, and scalability in your deployments.