Lab:43 Creating instance through terraform and stack - TanveeKhanDB/OCI GitHub Wiki

Terraform:

  • Terraform is an open-source Infrastructure as Code (IaC) tool by HashiCorp that lets you define, provision, and manage cloud infrastructure using simple configuration files (.tf files) instead of manually clicking through a console.

  • Terraform turns your OCI infrastructure into version-controlled, repeatable, automated code — eliminating manual effort, human error, and configuration drift permanently.

  • Terraform is a constructive and distructive language it can create and terminate Infrastructure however resource management is not possible by this language.

Example — create an OCI VCN with Terraform

Terraform core workflow

Terraform in OCI

OCI has an official Terraform provider, meaning you can write code to create and manage any OCI resource — VCNs, subnets, compute instances, databases, load balancers, policies — automatically and repeatedly.

Key Benefits in OCI specifically:

  • Provision entire OCI environments — network, compute, storage — in minutes
  • Manage OCI Resource Manager (Oracle's native Terraform service) directly from the Console
  • Reuse modules across projects — write once, deploy anywhere
  • Enforce governance — infrastructure reviewed like code before it goes live

Stack

In OCI (and Terraform generally), a Stack is a collection of Terraform configuration files (.tf) bundled together as a deployable unit. Think of it as a "package" of infrastructure code that OCI's Resource Manager can run, track, and manage — including its state. One Stack = one environment's complete infrastructure definition.

A Stack is your infrastructure in a zip file — OCI Resource Manager unpacks it, runs it, tracks its state, and lets you update or destroy it cleanly at any time.

The real power of a Stack over raw Terraform is that OCI Resource Manager handles the state file for you — no S3 bucket, no manual backend config, no team conflicts. It just works, natively, inside the console.

Key Points:

What is a Terraform Stack in OCI

Terraform Stack lifecycle in OCI

Here is the lifecycle — how you go from writing a Stack to a fully running OCI environment, step by step


Creating instance via terraform and stack method by CLI:

Opne OCI Code Editor:

Needs to fill the required OCI details for our Terraform script under the OCI block

provider "oci" {

auth = "APIKey"

tenancy_ocid = ""

user_ocid = ""

fingerprint = ""

private_key_path = ""

region = ""

}

Generating Key :

As we don't have server or public key created as of now hence choosing Generate Key so OCI will create it automatically:

Save this key in a seprate file under OCI code editor:

Imp commands for terraform script:

To initiate the terraform script: terraform init

To see the plan of terraform script: terraform plan

To apply or create the instance in the terraform script: terraform apply

To destroy or delete the instance in the terraform script: terraform destroy

Initialization:

view the planning:

Instance creation:

Validating the instance creation by terraform code:

Deleting the instance:


Creating instance via terraform and stack method by GUI:


As an output Jump_Server has been created:

It has a default log files which keeps all the steps it should not be deleted.