Getting Started with Terraform in OCI - Part 1

Getting Started with Terraform in OCI

In today’s, cloud-driven world, managing infrastructure efficiently isn’t just a nice-to-have—it’s essential. As businesses scales, the complexity of managing infrastructure grows, making automation a critical part of any IT strategy. This is where Infrastructure as Code (IaC) comes in, and Terraform is at the forefront of this revolution.

Provisioning manual infrastructure can be a feel good factor only when there is a limited resources to provision. Imagine to provision 100's compute with same configuration, it will end with frustration resulting in improper configuration or some other mistakes.

If you’re working with Oracle Cloud Infrastructure (OCI), this blog series is your guide to mastering Terraform. In this first post, we’ll cover the key concepts you need to understand before diving into code, followed by practical examples that you can try out yourself.

No Deep Theories—Just Practical Learning

Our focus in this series is on practical implementation. We won’t go deep into theories, but instead, will highlight the essential concepts that you need to understand. This post is all about getting hands-on with Terraform, using hardcoded values to declare your infrastructure. In later parts of this series, we’ll revisit the same infrastructure using advanced topics, such as variables, outputs, and modules, to make it more dynamic and scalable.

Step 1: Setting Up Your Environment

Before we jump into writing Terraform code, let’s get your environment set up. Whether you’re new to Terraform or just need a refresher, here’s what you’ll need:

  • Install Terraform: Follow the official installation guide to get it up and running on your machine.
  • Install Visual Studio Code (VS Code): Download it here.
  • Add the OCI Provider Plugin: To manage OCI resources, you’ll need to configure Terraform with the OCI provider.

Key Concepts in Terraform

Understanding Terraform’s core concepts is crucial to using it effectively. Let’s break down the most important ones, with practical insights along the way.

1. Providers

Providers are the link between Terraform and the services you want to manage. Think of them as plugins that extend Terraform’s capabilities. In our case, the OCI provider is what enables Terraform to create and manage resources in Oracle Cloud.

Practical Tip: Always ensure you’re using the latest version of the OCI provider to take advantage of new features and improvements.

2. Resources

Resources are the building blocks of your infrastructure. In Terraform, every cloud component, whether it’s a compute instance, a database, or a network, is defined as a resource. Each resource has attributes and properties that determine its configuration.

Practical Tip: When defining resources, start with hardcoded values to understand the basics. As you advance, you can use variables to make your code more dynamic and reusable.

3. State Management

Terraform keeps track of your infrastructure using state files. These files act as a snapshot of your managed resources, enabling Terraform to determine what changes need to be applied.

Practical Tip: Store your state files in a remote backend (like OCI Object Storage) when working in teams. This prevents conflicts and ensures everyone is working with the latest state.

Practical Examples

Now, let’s put these concepts into practice. Below are simple, hardcoded Terraform configurations to get you started. As you progress, you can expand these examples to match your specific needs.

1. Creating a Compartment

resource "oci_identity_compartment" "chetan" {
  #Required
  compartment_id = ""
  description    = "New compartment"
  name           = "ChetanTerraform"
}
            

2. Setting Up a Virtual Cloud Network (VCN)

resource "oci_core_vcn" "cms_vcn" {
    compartment_id = <"compartment-id">
    cidr_block = "10.0.0.0/16"
    display_name = "terraform_vcn"
  }
            

3. Creating a Load Balancer

resource "oci_load_balancer_load_balancer" "example_load_balancer" {
  compartment_id       = <"compartment-id">
  display_name         = "cms-load-balancer"
  shape                = "flexible"  # Can be "flexible" or a fixed shape like "100Mbps"
  shape_details {
    maximum_bandwidth_in_mbps = "10"
    minimum_bandwidth_in_mbps = "10"
  }
  subnet_ids           = [<"subnet.ocid">]
  is_private           = false  # Set to true for a private load balancer
  #idle_timeout_in_seconds = 300
}
            

4. Provisioning a Compute Instance

resource "oci_core_instance" "cms01" {
  availability_domain = <"AD-name">
  compartment_id      = <"compartment-id">
  shape               = "VM.Standard.A1.Flex"
  shape_config {
    ocpus = "1"
    memory_in_gbs = "6"
  }

  source_details {
    source_id               = <"image-ocid">
    source_type             = "image"
    boot_volume_size_in_gbs = "50"
  }
  display_name = "cms01"
  metadata = {
    ssh_authorized_keys = file(<"Path to key file">)
    }
  create_vnic_details {
    subnet_id = <"subnet.ocid">
  }
  preserve_boot_volume = "false"
}
            

This blog post is your launchpad into the world of Terraform with OCI. We’ve covered the essential concepts—providers, resources, and state management—and provided practical examples to get you started.

In this part, we’ve focused on hardcoded values for simplicity. In future posts, we’ll implement the same infrastructure using advanced topics like variables, outputs, and more.

Check out the complete code on GitHub here: https://github.com/cmschetan/OCI--Terraform/tree/main/Part-1

Stay tuned for the next blog in the series!

No comments:

Post a Comment