Terraform for Oracle Cloud Infrastructure: Declarative Provisioning from Tenancy to VCN

Infrastructure as code changes how teams interact with Oracle Cloud Infrastructure. Instead of clicking through the console to create compartments, virtual cloud networks, and subnets, you define the desired state in code. Terraform provides a declarative model that translates HCL configuration into repeatable OCI resources, with version control, previewable changes, and safe destruction. This article covers the practical setup of Terraform for OCI, authentication patterns, provider configuration, and the VCN lifecycle using step-by-step workflows documented in official guides and community tutorials.

Understanding Terraform in the OCI Context

Terraform is a powerful Infrastructure as Code tool that allows you to define and provision cloud resources in a declarative way. In OCI environments, especially for small teams or growing systems, Terraform brings structure and predictability to cloud setup.

The core value propositions for OCI are:

  • Consistency: Deploy the same infrastructure across dev, test, and production
  • Speed: Launch full environments in minutes
  • Version control: Track changes like application code
  • Reduced errors: Avoid manual misconfigurations

More importantly, it forces you to think about architecture before deployment, which leads to better designs.

Terraform uses configuration files written in HCL to define OCI resources such as VCNs, subnets, compute instances, and gateways. The basic workflow looks like this:

Write your infrastructure configuration
Initialize Terraform
Preview changes
Apply configuration

OCI also supports Terraform natively through Resource Manager, which allows you to run Terraform jobs directly inside the cloud without managing local state files manually.

Provider Installation and Authentication Prerequisites

Before any OCI resource can be managed, the Terraform provider must be installed and authenticated to a tenancy. The Oracle Cloud Infrastructure Terraform provider is a component that connects Terraform to the OCI services that you want to manage.

Key tasks include how to:

  • Create RSA keys
  • Set up Oracle Cloud Infrastructure Terraform provider scripts
  • Authenticate your Terraform scripts
  • Get information about the availability domains in your tenancy

To successfully perform a tutorial, you must have the following:

  • An Oracle Cloud Infrastructure account
  • A MacOS, Linux, or Windows environment
  • MacOS
  • Linux (Any distribution)
  • You can install a Linux VM with an Always Free Compute shape, on Oracle Cloud Infrastructure
  • Oracle Cloud Infrastructure Cloud Shell

This tutorial uses an Oracle Linux VM environment with an AMD shape for its examples, but you can use any environment mentioned in this section.

Prepare your environment for authenticating and running Terraform scripts. Also, gather the information your account needs to authenticate the scripts. Skip creating RSA keys if you are using Cloud Shell or Resource Manager.

Install Terraform on Mac, Linux, or Windows by downloading the binary or using a package manager (Homebrew or Chocolatey). Then create a Docker container locally by following a quick-start tutorial to check that Terraform installed correctly.

Setting Up OCI Terraform Provider Scripts

Set up Oracle Cloud Infrastructure Terraform provider scripts, documented in the Terraform Registry, to connect to an OCI account. Confirm the setup by fetching information from the tenancy.

The OCI Terraform provider is region agnostic. You can use the Terraform provider to work with supported services in all Oracle Cloud Infrastructure regions where they are available. Oracle US Government Cloud and Oracle US Defense Cloud customers: Use the FIPS-compatible OCI Terraform provider.

You can use the OCI Terraform provider to manage OCI resources wherever you use a Terraform distribution, including Terraform Cloud and the OCI Resource Manager. The OCI Terraform provider is at GitHub.

To migrate an existing Terraform state file to Resource Manager, use an import job.

To begin using this provider to manage OCI resources, see Getting Started.

Licensing: This provider and samples are licensed under the Mozilla Public License 2.0; third-party content is separately licensed as described in the code.

Terraform configurations codify infrastructure in declarative files that contain the steps required to provision infrastructure and maintain its state. You can share these files among team members, treat them as code, edit, review, and version them.

Terraform state files contain all resource attributes that are specified as part of configuration files. If you manage any sensitive data with Terraform, such as database or user passwords or instance private keys, treat the state file itself as sensitive data.

You can use the OCI Terraform provider to manage OCI resources wherever you use a Terraform distribution, including Terraform Cloud and the OCI Resource Manager.

Step-by-Step VCN and Subnet Lifecycle

Build, change, and destroy a virtual cloud network and subnet on Oracle Cloud Infrastructure using Terraform. Step-by-step, command-line tutorials will walk you through the Terraform basics for the first time.

Learn how infrastructure as code lets you safely build, change, and manage infrastructure.

The tutorial sequence is:

  • Install Terraform
  • Build infrastructure
  • Change infrastructure
  • Destroy infrastructure
  • Define input variables

Build infrastructure

Authenticate to Oracle Cloud Infrastructure, and create a Virtual Cloud Network. Write, format and validate a Terraform configuration. Plan and apply the configuration to create an VCN OCI resource.

A minimal provider block references the tenancy and region. Authentication is handled via environment variables or a provider configuration file.

hcl provider "oci" { tenancy_ocid = var.tenancy_ocid user_ocid = var.user_ocid fingerprint = var.fingerprint private_key_path = var.private_key_path region = var.region }

Initialize Terraform:

bash terraform init

Preview changes:

bash terraform plan

Apply configuration:

bash terraform apply

Change infrastructure

Add a subnet to your Oracle Cloud Infrastructure VCN. Modify the subnet display name and plan changes to update the resource in place. Learn how Terraform handles infrastructure change management.

Terraform detects drift and proposes in-place updates for attributes that support modification without replacement. This allows safe evolution of VCNs, subnets, and security rules.

Destroy infrastructure

Destroy the Oracle Cloud Infrastructure Virtual Cloud Network and subnet you created in the previous tutorials. Evaluate the plan and confirm the destruction.

bash terraform plan -destroy terraform destroy

Define input variables

Declare your OCI region and compartment ID as variables. Reference the variable in Terraform configuration.

```hcl
variable "region" {
description = "OCI region"
type = string
}

variable "compartment_id" {
description = "Compartment OCID"
type = string
}
```

Configuration Management with Variables and State

Terraform uses providers to interface between the Terraform engine and the supported cloud platform. The Oracle Cloud Infrastructure Terraform provider is a component that connects Terraform to the OCI services that you want to manage.

State management is central to safe operations. Terraform state files contain all resource attributes that are specified as part of configuration files. If you manage any sensitive data with Terraform, such as database or user passwords or instance private keys, treat the state file itself as sensitive data.

For more information, see Storing Sensitive Data.

You can use the OCI Terraform provider to manage OCI resources wherever you use a Terraform distribution, including Terraform Cloud and the OCI Resource Manager.

The tutorial shows how to set up Terraform to manage Oracle Cloud Infrastructure, configure the OCI CLI on macOS and Windows, and create an OCI compartment with Terraform. It includes sample commands, Terraform code snippets, configuration tips, and suggested places where you would add graphical snapshots in your documentation or blog. References to official OCI and Terraform guides are included for each major step.

OCI Terraform Modules and Community Landscape

Terraform projects for Oracle Cloud Infrastructure, developed and maintained by Oracle and the OCI community, provide reusable building blocks.

The Oracle GitHub organization maintains several related projects:

  • Oracle – our primary GitHub organization
  • Fn Project – the container native, cloud agnostic serverless platform
  • GraalVM - projects related to GraalVM, a high-performance JDK with native compilation
  • Helidon – cloud-native set of Java libraries for writing microservices
  • Java Community Process – developing standard technical specifications for Java technology
  • LiveLabs – workshops for building and deploying applications using Oracle's technologies
  • MySQL – the world's most popular open source database
  • OCI Landing Zones – landing zones for automated deployment of OCI services
  • Oracle Cloud Native Environment - fully integrated suite for the development and management of cloud-native applications
  • OpenJDK – OpenJDK development and related repositories
  • Oracle DevRel – developer code samples, tutorials, and more
  • Oracle GitHub Actions – GitHub Actions from Oracle
  • Oracle Sample Projects – new projects and sample applications
  • Oracle Terraform Modules – Terraform modules for provisioning Oracle Cloud Infrastructure resources
  • Quick Start – automated deployments of enterprise software on Oracle Cloud Infrastructure
  • VirtualBox - Oracle's virtualization software running on many operating systems using AMD/Intel/ARM processors

The Oracle Terraform Modules repository contains production-ready modules for provisioning OCI resources. Using modules reduces duplication and enforces organizational standards across teams.

Practical Workflow with Resource Manager and State Security

Instead of clicking through the console, you define your infrastructure in code. That means repeatable deployments, version control, and a much cleaner way to scale your environment.

Why Terraform Matters for OCI

Terraform brings structure and predictability to your cloud setup. In OCI environments, especially for small teams or growing systems, this becomes critical.

Consistency: Deploy the same infrastructure across dev, test, and production
Speed: Launch full environments in minutes
Version control: Track changes like application code
Reduced errors: Avoid manual misconfigurations

How Terraform Works in Oracle Cloud Infrastructure

Terraform uses configuration files (written in HCL) to define OCI resources such as VCNs, subnets, compute instances, and gateways.

The basic workflow looks like this:

  • Write your infrastructure configuration
  • Initialize Terraform (terraform init)
  • Preview changes (terraform plan)
  • Apply configuration (terraform apply)

OCI also supports Terraform natively through Resource Manager, which allows you to run Terraform jobs directly inside the cloud without managing local state files manually.

The following table summarizes the core tutorial tasks and their purpose.

| Phase | Action | Outcome |
| Build | Authenticate to OCI and create VCN | Virtual Cloud Network provisioned |
| Change | Add subnet and modify display name | In-place update of existing resource |
| Destroy | Plan and apply destruction | Safe removal of VCN and subnet |
| Variables | Declare region and compartment ID | Configurable reusable modules |

Contributions to the provider are open source. Got a fix for a bug, or a new feature you'd like to contribute? The OCI Terraform provider is open source and accepting pull requests on GitHub.

To be notified when a new version of the OCI Terraform provider is released, subscribe to the Atom feed.

Conclusion

Terraform for Oracle Cloud Infrastructure delivers a complete declarative lifecycle from tenancy authentication to VCN and subnet management, with provider support across all commercial and government regions, FIPS-compatible options for regulated workloads, and native integration with OCI Resource Manager for job execution without local state handling. The combination of official provider documentation, step-by-step HashiCorp tutorials for VCN creation, change management, and destruction, and the Oracle Terraform Modules repository provides a durable foundation for teams building repeatable OCI environments. Authentication patterns using RSA keys, Cloud Shell shortcuts, and Always Free compute for Linux development lower the barrier to entry, while state sensitivity guidance and Mozilla Public License 2.0 licensing ensure safe and compliant operations. Adopting the workflow of write, init, plan, and apply with variables for region and compartment ID makes infrastructure changes auditable, reversible, and consistent across dev, test, and production.

Sources

  1. Terraform Manage Oracle Cloud Infrastructure OCI Part 1
  2. Oracle Terraform Modules
  3. Hashicorp Terraform OCI Get Started
  4. Oracle Docs TF Provider Tutorial
  5. Oracle Docs Terraform Home
  6. dmcloudarchitect Terraform OCI Practical Setup Guide

Related Posts