Skip to content

Lab:43 Creating instance through terraform and stack

TanveeKhanDB edited this page Jun 27, 2026 · 14 revisions

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

image

Terraform core workflow

image

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
image

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:

image

What is a Terraform Stack in OCI

image

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

image

Creating instance via terraform and stack method by CLI:

image image image image image

Opne OCI Code Editor: image image

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 : image

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

image image

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

image

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:

image image

view the planning:

image image

Instance creation:

image image image

Validating the instance creation by terraform code:

image

Deleting the instance: image image image image


Creating instance via terraform and stack method by GUI:


image image image image image image image image image

As an output Jump_Server has been created:

image image image

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

image image

Clone this wiki locally