Skip to content

Instantly share code, notes, and snippets.

@omenos
Last active April 7, 2025 03:05
Show Gist options
  • Select an option

  • Save omenos/e20c2f52e5558b4bdd183f776048ddf3 to your computer and use it in GitHub Desktop.

Select an option

Save omenos/e20c2f52e5558b4bdd183f776048ddf3 to your computer and use it in GitHub Desktop.
Using AI models to generate Terraform configurations

Overview

This gist is an aggregation of attempts using different AI models to generate a Terraform configuration for deploying a virtual server in Oracle Cloud.

For consistency, an identical prompt was used with all models. Queries were executed only once via Kagi Assistant. Models were not allowed access to the internet.

Prompt

Using Terraform with the "oracle/oci ~ 6.0" module, demonstrate provisioning a virtual server with these requirements:

  • Shape: VM.Standard.E5.Flex
  • OCPU: 8
  • Memory: 64G
  • Boot Disk: 128G
  • Other Disks:
    • 300G
    • 50G
  • Region: us-ashburn-1
  • Image: Ubuntu 24.04

Technical Results

Model E2E time (s) Tokens Speed (tok/s) Version Query Date
ChatGPT 4o 5.71 1938 137 chatgpt-4o-latest (2025.03.26) 2025.04.06
GPT 4o 8.34 2129 115 gpt-4o-2024-11-20 2025.04.06
Claude 3.7 Sonnet 19.52 1471 75 claude-3-7-sonnet-20250219 2025.04.06
Gemini 2.5 Pro Preview 30.91 4593 109 gemini-2.5-pro-preview-03-25 2025.04.06
Llama 4 Maverick 19.39 2667 77 llama4-maverick-instruct-basic 2025.04.06
Llama 4 Scout 4.38 2518 327 meta-llama/llama-4-scout-17b-16e-instruct 2025.04.06

Here's a Terraform configuration using the oracle/oci provider (version ~> 6.0) that provisions a virtual server in Oracle Cloud Infrastructure (OCI) with the specified requirements:

Terraform Configuration

terraform {
  required_providers {
    oci = {
      source  = "oracle/oci"
      version = "~> 6.0"
    }
  }
}

provider "oci" {
  region = "us-ashburn-1"
}

# Variables (replace with your actual values or use a variables.tf file)
variable "compartment_ocid" {}
variable "availability_domain" {}
variable "subnet_ocid" {}

# Get the latest Ubuntu 24.04 image
data "oci_core_images" "ubuntu" {
  compartment_id = var.compartment_ocid
  operating_system = "Canonical Ubuntu"
  operating_system_version = "24.04"
  shape = "VM.Standard.E5.Flex"
  sort_by = "TIMECREATED"
  sort_order = "DESC"
}

resource "oci_core_instance" "ubuntu_vm" {
  availability_domain = var.availability_domain
  compartment_id      = var.compartment_ocid
  shape               = "VM.Standard.E5.Flex"
  display_name        = "ubuntu-24-vm"

  shape_config {
    ocpus         = 8
    memory_in_gbs = 64
  }

  create_vnic_details {
    subnet_id        = var.subnet_ocid
    assign_public_ip = true
  }

  source_details {
    source_type = "image"
    source_id   = data.oci_core_images.ubuntu.images[0].id
    boot_volume_size_in_gbs = 128
  }

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

# Additional Block Volume: 300G
resource "oci_core_volume" "volume_300g" {
  availability_domain = var.availability_domain
  compartment_id      = var.compartment_ocid
  size_in_gbs         = 300
  display_name        = "data-volume-300g"
}

# Additional Block Volume: 50G
resource "oci_core_volume" "volume_50g" {
  availability_domain = var.availability_domain
  compartment_id      = var.compartment_ocid
  size_in_gbs         = 50
  display_name        = "data-volume-50g"
}

# Attach 300G volume
resource "oci_core_volume_attachment" "attach_300g" {
  attachment_type = "paravirtualized"
  instance_id     = oci_core_instance.ubuntu_vm.id
  volume_id       = oci_core_volume.volume_300g.id
}

# Attach 50G volume
resource "oci_core_volume_attachment" "attach_50g" {
  attachment_type = "paravirtualized"
  instance_id     = oci_core_instance.ubuntu_vm.id
  volume_id       = oci_core_volume.volume_50g.id
}

📝 Notes

  • Replace the following variables with actual values:
    • compartment_ocid
    • availability_domain (e.g., "kIdk:US-ASHBURN-AD-1")
    • subnet_ocid
  • Ensure your SSH public key is at ~/.ssh/id_rsa.pub or adjust the path.
  • The oci_core_images data source filters the latest Ubuntu 24.04 image available for the specified shape.

Let me know if you want this wrapped in modules or with outputs added.


Generated by The Assistant

Terraform Configuration for OCI Virtual Server

Here's a Terraform configuration to provision an Oracle Cloud Infrastructure (OCI) virtual server with the specified requirements:

terraform {
  required_providers {
    oci = {
      source  = "oracle/oci"
      version = "~> 6.0"
    }
  }
}

provider "oci" {
  region = "us-ashburn-1"
  # Authentication can be provided via environment variables or config file
}

# Variables
variable "compartment_id" {
  description = "The OCID of the compartment to create resources in"
  type        = string
}

variable "availability_domain" {
  description = "The availability domain to create the instance in"
  type        = string
  default     = "aDqM:US-ASHBURN-AD-1"
}

variable "subnet_id" {
  description = "The OCID of the subnet to create the VNIC in"
  type        = string
}

# Get the latest Ubuntu 24.04 image
data "oci_core_images" "ubuntu_images" {
  compartment_id           = var.compartment_id
  operating_system         = "Canonical Ubuntu"
  operating_system_version = "24.04"
  shape                    = "VM.Standard.E5.Flex"
  sort_by                  = "TIMECREATED"
  sort_order               = "DESC"
}

# Compute instance
resource "oci_core_instance" "ubuntu_instance" {
  availability_domain = var.availability_domain
  compartment_id      = var.compartment_id
  display_name        = "ubuntu-e5-flex-instance"
  shape               = "VM.Standard.E5.Flex"

  shape_config {
    ocpus         = 8
    memory_in_gbs = 64
  }

  create_vnic_details {
    subnet_id        = var.subnet_id
    display_name     = "primary-vnic"
    assign_public_ip = true
  }

  source_details {
    source_type = "image"
    source_id   = data.oci_core_images.ubuntu_images.images[0].id
    boot_volume_size_in_gbs = 128
  }

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

# 300G Block Volume
resource "oci_core_volume" "data_volume_300g" {
  availability_domain = var.availability_domain
  compartment_id      = var.compartment_id
  display_name        = "data-volume-300g"
  size_in_gbs         = 300
}

# 50G Block Volume
resource "oci_core_volume" "data_volume_50g" {
  availability_domain = var.availability_domain
  compartment_id      = var.compartment_id
  display_name        = "data-volume-50g"
  size_in_gbs         = 50
}

# Attach 300G volume
resource "oci_core_volume_attachment" "data_volume_300g_attachment" {
  attachment_type = "paravirtualized"
  instance_id     = oci_core_instance.ubuntu_instance.id
  volume_id       = oci_core_volume.data_volume_300g.id
  display_name    = "data-volume-300g-attachment"
  device          = "/dev/oracleoci/oraclevdb"
}

# Attach 50G volume
resource "oci_core_volume_attachment" "data_volume_50g_attachment" {
  attachment_type = "paravirtualized"
  instance_id     = oci_core_instance.ubuntu_instance.id
  volume_id       = oci_core_volume.data_volume_50g.id
  display_name    = "data-volume-50g-attachment"
  device          = "/dev/oracleoci/oraclevdc"
}

# Output the public IP of the instance
output "instance_public_ip" {
  value = oci_core_instance.ubuntu_instance.public_ip
}

# Output the attached volume OCIDs
output "data_volume_300g_id" {
  value = oci_core_volume.data_volume_300g.id
}

output "data_volume_50g_id" {
  value = oci_core_volume.data_volume_50g.id
}

Key Components Explained

  1. Provider Configuration:

    • Uses the Oracle/OCI provider version ~> 6.0
    • Sets the region to us-ashburn-1
  2. Instance Configuration:

    • Shape: VM.Standard.E5.Flex with flexible configuration
    • Resources: 8 OCPUs and 64GB memory as specified
    • Boot volume: 128GB size
  3. Additional Block Volumes:

    • One 300GB data volume
    • One 50GB data volume
    • Both attached as paravirtualized devices
  4. Image Selection:

    • Uses a data source to find the latest Ubuntu 24.04 image
  5. Variables:

    • Requires input for compartment_id and subnet_id
    • Default availability domain is set to US-ASHBURN-AD-1

Usage Instructions

  1. Save this configuration to a .tf file
  2. Initialize Terraform: terraform init
  3. Set required variables:
    export TF_VAR_compartment_id=ocid1.compartment.oc1..example
    export TF_VAR_subnet_id=ocid1.subnet.oc1.phx.example
    
  4. Plan the deployment: terraform plan
  5. Apply the configuration: terraform apply

Remember to authenticate with OCI either through environment variables or a configuration file before running Terraform commands.


Generated by The Assistant

Okay, here is a Terraform configuration example using the oracle/oci provider version ~> 6.0 to provision an OCI compute instance with your specified requirements.

This configuration assumes you have already configured your OCI provider credentials (e.g., via environment variables, OCI config file, or instance principal).

terraform {
  required_providers {
    oci = {
      source  = "oracle/oci"
      version = "~> 6.0"
    }
  }
}

provider "oci" {
  region = "us-ashburn-1"
  # Authentication details (tenancy_ocid, user_ocid, fingerprint, private_key_path)
  # are assumed to be configured via environment variables or OCI config file.
}

# Required Input Variables
variable "compartment_id" {
  description = "The OCID of the compartment where resources will be created."
  type        = string
}

variable "subnet_id" {
  description = "The OCID of the subnet to attach the instance's VNIC to."
  type        = string
}

variable "ssh_public_key" {
  description = "The public SSH key to authorize access to the instance."
  type        = string
  sensitive   = true
}

# Data source to get Availability Domains in the region
data "oci_identity_availability_domains" "ads" {
  compartment_id = var.compartment_id
}

# Data source to find the latest Ubuntu 24.04 image OCID
data "oci_core_images" "ubuntu_image" {
  compartment_id           = var.compartment_id # Use compartment OCID or Oracle image compartment OCID if known
  operating_system         = "Canonical Ubuntu"
  operating_system_version = "24.04"
  shape                    = "VM.Standard.E5.Flex" # Filter by compatible shape if needed
  sort_by                  = "TIMECREATED"
  sort_order               = "DESC"
  filter {
    name   = "display_name"
    values = ["Canonical-Ubuntu-24.04-*"] # Adjust pattern if needed
    regex  = true
  }
}

# --- Core Instance Resource ---
resource "oci_core_instance" "vm_instance" {
  # **Selects the first Availability Domain**
  availability_domain = data.oci_identity_availability_domains.ads.availability_domains[0].name
  compartment_id      = var.compartment_id
  display_name        = "ubuntu-e5-flex-vm"

  # **Specifies the flexible shape and resources**
  shape = "VM.Standard.E5.Flex"
  shape_config {
    ocpus         = 8
    memory_in_gbs = 64
  }

  # **Configures the boot volume using the Ubuntu 24.04 image**
  source_details {
    source_type             = "image"
    source_id               = data.oci_core_images.ubuntu_image.images[0].id
    boot_volume_size_in_gbs = 128
  }

  # **Configures networking**
  create_vnic_details {
    subnet_id        = var.subnet_id
    assign_public_ip = true # Set to false if you don't need a public IP
    display_name     = "primaryvnic"
  }

  # **Adds SSH key for access**
  metadata = {
    ssh_authorized_keys = var.ssh_public_key
  }

  # Optional: Define timeouts for create, update, delete operations
  timeouts {
    create = "60m"
  }
}

# --- Additional Block Volumes ---

# **Defines the 300GB block volume**
resource "oci_core_volume" "block_volume_300g" {
  availability_domain = data.oci_identity_availability_domains.ads.availability_domains[0].name
  compartment_id      = var.compartment_id
  display_name        = "data-volume-300g"
  size_in_gbs         = 300
  # Optional: Specify performance level (vpus_per_gb) if needed
  # vpus_per_gb = 10 # Default (Balanced)
}

# **Defines the 50GB block volume**
resource "oci_core_volume" "block_volume_50g" {
  availability_domain = data.oci_identity_availability_domains.ads.availability_domains[0].name
  compartment_id      = var.compartment_id
  display_name        = "data-volume-50g"
  size_in_gbs         = 50
  # Optional: Specify performance level (vpus_per_gb) if needed
  # vpus_per_gb = 10 # Default (Balanced)
}

# --- Volume Attachments ---

# **Attaches the 300GB volume to the instance**
resource "oci_core_volume_attachment" "attach_volume_300g" {
  attachment_type = "iscsi" # Other options: 'paravirtualized'
  instance_id     = oci_core_instance.vm_instance.id
  volume_id       = oci_core_volume.block_volume_300g.id
  # Optional: Define device path, display name, read-only status
  # device = "/dev/oracleoci/oraclevdb" # Example device name, let OCI assign if omitted
  # display_name = "attachment-300g"
  # is_read_only = false
}

# **Attaches the 50GB volume to the instance**
resource "oci_core_volume_attachment" "attach_volume_50g" {
  attachment_type = "iscsi"
  instance_id     = oci_core_instance.vm_instance.id
  volume_id       = oci_core_volume.block_volume_50g.id
  # Optional: Define device path, display name, read-only status
  # device = "/dev/oracleoci/oraclevdc" # Example device name, let OCI assign if omitted
  # display_name = "attachment-50g"
  # is_read_only = false
}

# --- Outputs (Optional) ---
output "instance_public_ip" {
  description = "Public IP address of the provisioned instance."
  value       = oci_core_instance.vm_instance.public_ip
}

output "instance_id" {
  description = "OCID of the provisioned instance."
  value       = oci_core_instance.vm_instance.id
}

output "instance_private_ip" {
  description = "Private IP address of the provisioned instance."
  value       = oci_core_instance.vm_instance.private_ip
}

Explanation:

  1. Provider Configuration: Sets up the OCI provider, specifies the required version (~> 6.0), and sets the target region (us-ashburn-1).
  2. Variables: Defines input variables for compartment_id, subnet_id, and ssh_public_key which you'll need to provide when running Terraform.
  3. Data Sources:
    • oci_identity_availability_domains: Fetches the list of Availability Domains (ADs) in your specified region and compartment. The instance and volumes will be placed in the first AD found (ads.availability_domains[0].name).
    • oci_core_images: Dynamically finds the latest OCI Image OCID for Ubuntu 24.04 compatible with the specified shape. It sorts by creation time descending and picks the first one. You might need to adjust the compartment_id or filter depending on where the image resides (your compartment or Oracle's).
  4. oci_core_instance:
    • Provisions the virtual machine itself.
    • Sets the availability_domain, compartment_id, and display_name.
    • Specifies the shape as VM.Standard.E5.Flex.
    • Uses shape_config to define the 8 OCPUs and 64 GB Memory.
    • Uses source_details to configure the boot volume with the Ubuntu 24.04 image OCID found by the data source and sets the boot volume size to 128 GB.
    • create_vnic_details configures the primary network interface, attaching it to your specified subnet_id.
    • metadata includes the ssh_public_key for initial access.
  5. oci_core_volume:
    • Two resources define the additional block volumes (300 GB and 50 GB) in the same AD as the instance.
  6. oci_core_volume_attachment:
    • Two resources attach the previously defined block volumes to the instance using the iscsi attachment type.
  7. Outputs: (Optional) Provides useful information like the instance's public/private IP and OCID after creation.

To use this code:

  1. Save it as a .tf file (e.g., main.tf).
  2. Create a terraform.tfvars file (or use environment variables/command-line flags) to provide values for compartment_id, subnet_id, and ssh_public_key.
  3. Run terraform init.
  4. Run terraform plan to review the changes.
  5. Run terraform apply to provision the resources.

Generated by The Assistant

Below is an example Terraform configuration to provision a virtual server in Oracle Cloud Infrastructure (OCI) using the oracle/oci provider module version ~6.0. This configuration meets the specified requirements:

terraform {
  required_providers {
    oci = {
      source  = "oracle/oci"
      version = "~> 6.0"
    }
  }

  required_version = ">= 1.3.0"
}

provider "oci" {
  region = "us-ashburn-1"
}

resource "oci_core_instance" "example_instance" {
  # Specify the availability domain
  availability_domain = data.oci_identity_availability_domains.ads.availability_domains[0].name

  # Compartment OCID (replace with your compartment OCID)
  compartment_id = var.compartment_id

  # Instance shape
  shape = "VM.Standard.E5.Flex"

  # Shape configuration for OCPUs and memory
  shape_config {
    ocpus = 8
    memory_in_gbs = 64
  }

  # Source details for the boot volume
  source_details {
    source_type = "image"
    source_id   = data.oci_core_images.ubuntu_image.images[0].id
  }

  # Boot volume size
  create_vnic_details {
    subnet_id = var.subnet_id
  }

  boot_volume_size_in_gbs = 128

  # Metadata (optional, e.g., SSH keys)
  metadata = {
    ssh_authorized_keys = file("~/.ssh/id_rsa.pub")
  }
}

# Additional block volumes
resource "oci_core_volume" "volume_300g" {
  availability_domain = data.oci_identity_availability_domains.ads.availability_domains[0].name
  compartment_id      = var.compartment_id
  size_in_gbs         = 300
}

resource "oci_core_volume" "volume_50g" {
  availability_domain = data.oci_identity_availability_domains.ads.availability_domains[0].name
  compartment_id      = var.compartment_id
  size_in_gbs         = 50
}

# Attach block volumes to the instance
resource "oci_core_volume_attachment" "attach_volume_300g" {
  instance_id = oci_core_instance.example_instance.id
  volume_id   = oci_core_volume.volume_300g.id
}

resource "oci_core_volume_attachment" "attach_volume_50g" {
  instance_id = oci_core_instance.example_instance.id
  volume_id   = oci_core_volume.volume_50g.id
}

# Data source to fetch Ubuntu 24.04 image
data "oci_core_images" "ubuntu_image" {
  compartment_id = var.compartment_id
  operating_system = "Canonical Ubuntu"
  operating_system_version = "24.04"
}

# Data source to fetch availability domains
data "oci_identity_availability_domains" "ads" {
  compartment_id = var.compartment_id
}

# Variables
variable "compartment_id" {
  description = "The OCID of the compartment where resources will be created"
  type        = string
}

variable "subnet_id" {
  description = "The OCID of the subnet where the instance will be created"
  type        = string
}

Explanation:

  1. Provider Configuration:

    • The oci provider is configured for the us-ashburn-1 region.
  2. Instance Configuration:

    • The instance uses the VM.Standard.E5.Flex shape with 8 OCPUs and 64 GB of memory.
    • The boot volume is set to 128 GB.
    • The image is filtered to use Ubuntu 24.04.
  3. Block Volumes:

    • Two additional block volumes are created: one with 300 GB and another with 50 GB.
    • These volumes are attached to the instance.
  4. Data Sources:

    • Availability domains and the Ubuntu 24.04 image are fetched dynamically using data sources.
  5. Variables:

    • compartment_id and subnet_id are defined as variables to make the configuration reusable.

Steps to Use:

  1. Replace var.compartment_id and var.subnet_id with the appropriate OCIDs for your compartment and subnet.
  2. Ensure your SSH public key file (~/.ssh/id_rsa.pub) exists and is correctly referenced.
  3. Initialize Terraform with terraform init.
  4. Plan the deployment with terraform plan.
  5. Apply the configuration with terraform apply.

Generated by The Assistant

To provision a virtual server with the specified requirements using Terraform with the "oracle/oci ~ 6.0" module, you will need to create a Terraform configuration file. Below is a step-by-step guide and a sample configuration.

Step 1: Install Terraform and Oracle Cloud Infrastructure (OCI) Provider

Ensure you have Terraform installed on your machine. You can download it from the official Terraform website. The OCI provider will be automatically installed when you run terraform init.

Step 2: Configure OCI Provider

Create a file named main.tf and start by configuring the OCI provider.

provider "oci" {
  tenancy_ocid     = var.tenancy_ocid
  user_ocid        = var.user_ocid
  fingerprint      = var.fingerprint
  key_file         = var.key_file
  region           = "us-ashburn-1"
}

You'll need to define the variables used above in a variables.tf file.

variable "tenancy_ocid" {
  type = string
}

variable "user_ocid" {
  type = string
}

variable "fingerprint" {
  type = string
}

variable "key_file" {
  type = string
}

Step 3: Create a Virtual Cloud Network (VCN)

Before creating a compute instance, you need a VCN. Add the following to your main.tf.

resource "oci_core_virtual_network" "example_vcn" {
  cidr_block     = "10.0.0.0/16"
  compartment_id = var.compartment_ocid
  display_name   = "example-vcn"
  dns_label      = "examplevcn"
}

resource "oci_core_subnet" "example_subnet" {
  cidr_block        = "10.0.1.0/24"
  display_name      = "example-subnet"
  dns_label         = "examplesubnet"
  compartment_id    = var.compartment_ocid
  vcn_id            = oci_core_virtual_network.example_vcn.id
  security_list_ids = [oci_core_virtual_network.example_vcn.default_security_list_id]
  route_table_id    = oci_core_virtual_network.example_vcn.default_route_table_id
  dhcp_options_id   = oci_core_virtual_network.example_vcn.default_dhcp_options_id
}

Add compartment_ocid to your variables.tf.

variable "compartment_ocid" {
  type = string
}

Step 4: Fetch the Ubuntu 24.04 Image

You need to fetch the OCID of the Ubuntu 24.04 image. You can do this using a data source.

data "oci_core_images" "ubuntu_images" {
  compartment_id           = var.compartment_ocid
  operating_system         = "Canonical Ubuntu"
  operating_system_version = "24.04"
  shape                     = "VM.Standard.E5.Flex"
  sort_by                   = "TIMECREATED"
  sort_order                = "DESC"
}

locals {
  ubuntu_image_id = data.oci_core_images.ubuntu_images.images[0].id
}

Step 5: Create the Compute Instance

Now, you can create the compute instance with the specified requirements.

resource "oci_core_instance" "example_instance" {
  availability_domain = data.oci_identity_availability_domains.ads.availability_domains[0].name
  compartment_id      = var.compartment_ocid
  shape               = "VM.Standard.E5.Flex"

  shape_config {
    ocpus         = 8
    memory_in_gbs = 64
  }

  create_vnic_details {
    subnet_id        = oci_core_subnet.example_subnet.id
    assign_public_ip = "true"
  }

  source_details {
    source_type = "image"
    source_id   = local.ubuntu_image_id
    boot_volume_size_in_gbs = "128"
  }
}

You'll also need the oci_identity_availability_domains data source.

data "oci_identity_availability_domains" "ads" {
  compartment_id = var.tenancy_ocid
}

Step 6: Attach Additional Block Volumes

To attach additional block volumes, you can use the oci_core_volume and oci_core_volume_attachment resources.

resource "oci_core_volume" "example_volume_1" {
  availability_domain = data.oci_identity_availability_domains.ads.availability_domains[0].name
  compartment_id      = var.compartment_ocid
  size_in_gbs         = "300"
}

resource "oci_core_volume" "example_volume_2" {
  availability_domain = data.oci_identity_availability_domains.ads.availability_domains[0].name
  compartment_id      = var.compartment_ocid
  size_in_gbs         = "50"
}

resource "oci_core_volume_attachment" "example_attachment_1" {
  instance_id = oci_core_instance.example_instance.id
  volume_id   = oci_core_volume.example_volume_1.id
  attachment_type = "iscsi"
}

resource "oci_core_volume_attachment" "example_attachment_2" {
  instance_id = oci_core_instance.example_instance.id
  volume_id   = oci_core_volume.example_volume_2.id
  attachment_type = "iscsi"
}

Putting It All Together

Your main.tf should now look something like this:

# Provider configuration
provider "oci" {
  tenancy_ocid     = var.tenancy_ocid
  user_ocid        = var.user_ocid
  fingerprint      = var.fingerprint
  key_file         = var.key_file
  region           = "us-ashburn-1"
}

# VCN and subnet
resource "oci_core_virtual_network" "example_vcn" {
  # ...
}

resource "oci_core_subnet" "example_subnet" {
  # ...
}

# Fetch Ubuntu image
data "oci_core_images" "ubuntu_images" {
  # ...
}

locals {
  ubuntu_image_id = data.oci_core_images.ubuntu_images.images[0].id
}

# Availability domains
data "oci_identity_availability_domains" "ads" {
  compartment_id = var.tenancy_ocid
}

# Compute instance
resource "oci_core_instance" "example_instance" {
  # ...
}

# Additional block volumes
resource "oci_core_volume" "example_volume_1" {
  # ...
}

resource "oci_core_volume" "example_volume_2" {
  # ...
}

resource "oci_core_volume_attachment" "example_attachment_1" {
  # ...
}

resource "oci_core_volume_attachment" "example_attachment_2" {
  # ...
}

Initialize and Apply Terraform Configuration

  1. Run terraform init to initialize the Terraform working directory.
  2. Run terraform plan to see the execution plan.
  3. Run terraform apply to apply the configuration.

Make sure you have the necessary OCI credentials and compartment OCID ready to input when prompted or set them as environment variables.

This configuration provisions a virtual server with the specified requirements in Oracle Cloud Infrastructure using Terraform.


Generated by The Assistant

Provisioning a Virtual Server with Terraform and Oracle OCI

Below is an example Terraform configuration using the oracle/oci ~6.0 module to provision a virtual server with the specified requirements.

# Configure the Oracle Cloud Infrastructure provider
provider "oci" {
  region           = "us-ashburn-1"
  user             = var.oci_user
  fingerprint      = var.oci_fingerprint
  key_file         = var.oci_key_file
  tenancy_ocid     = var.oci_tenancy_ocid
}

# Create a virtual cloud network
resource "oci_core_virtual_cloud_network" "example_vcn" {
  display_name   = "example_vcn"
  compartment_id = var.oci_compartment_ocid
  cidr_blocks    = ["10.1.0.0/16"]
}

# Create a subnet
resource "oci_core_subnet" "example_subnet" {
  display_name   = "example_subnet"
  compartment_id = var.oci_compartment_ocid
  vcn_id         = oci_core_virtual_cloud_network.example_vcn.id
  cidr_block     = "10.1.1.0/24"
}

# Create a security list
resource "oci_core_security_list" "example_security_list" {
  display_name   = "example_security_list"
  compartment_id = var.oci_compartment_ocid
  vcn_id         = oci_core_virtual_cloud_network.example_vcn.id

  ingress_security_rules {
    protocol    = "6"
    source      = "0.0.0.0/0"
    source_type = "CIDR_BLOCK"
    tcp_options {
      min_port = 22
      max_port = 22
    }
  }
}

# Create a compute instance
resource "oci_core_instance" "example_instance" {
  display_name   = "example_instance"
  compartment_id = var.oci_compartment_ocid
  shape          = "VM.Standard.E5.Flex"

  create_vnic_details {
    assign_public_ip = true
    subnet_id        = oci_core_subnet.example_subnet.id
    nsg_ids          = [oci_core_network_security_group.example_nsg.id]
  }

  shape_config {
    ocpus         = 8
    memory_in_gbs = 64
  }

  source_details {
    source_type = "image"
    source_id   = data.oci_core_images.ubuntu.id
  }

  metadata_service {
    metadata_type = "v2"
  }
}

# Create a network security group
resource "oci_core_network_security_group" "example_nsg" {
  display_name   = "example_nsg"
  compartment_id = var.oci_compartment_ocid
  vcn_id         = oci_core_virtual_cloud_network.example_vcn.id
}

# Add security rules to the network security group
resource "oci_core_network_security_group_security_rule" "example_security_rule" {
  network_security_group_id = oci_core_network_security_group.example_nsg.id
  direction                  = "INGRESS"
  protocol                   = 6
  source                     = "0.0.0.0/0"
  source_type                = "CIDR_BLOCK"
  tcp_options {
    min_port = 22
    max_port = 22
  }
}

# Get the OCID of the Ubuntu 24.04 image
data "oci_core_images" "ubuntu" {
  compartment_id = var.oci_compartment_ocid
  display_name   = "Canonical Ubuntu"
  operating_system = "Canonical Ubuntu"
  operating_system_version = "24.04"
  shape          = "VM.Standard.E5.Flex"
  sort_by        = "TIMECREATED"
  sort_order     = "DESC"
}

# Create a block storage volume for boot disk
resource "oci_core_volume" "boot_volume" {
  display_name   = "boot_volume"
  compartment_id = var.oci_compartment_ocid
  size_in_gbs    = 128
  vpus_per_gb    = 10
}

# Attach the boot volume to the instance
resource "oci_core_volume_attachment" "boot_volume_attachment" {
  instance_id = oci_core_instance.example_instance.id
  volume_id   = oci_core_volume.boot_volume.id
  device      = "/dev/oracleoci_vdb0"
}

# Create block storage volumes for other disks
resource "oci_core_volume" "other_volume_1" {
  display_name   = "other_volume_1"
  compartment_id = var.oci_compartment_ocid
  size_in_gbs    = 300
  vpus_per_gb    = 10
}

resource "oci_core_volume_attachment" "other_volume_1_attachment" {
  instance_id = oci_core_instance.example_instance.id
  volume_id   = oci_core_volume.other_volume_1.id
  device      = "/dev/oracleoci_vdb1"
}

resource "oci_core_volume" "other_volume_2" {
  display_name   = "other_volume_2"
  compartment_id = var.oci_compartment_ocid
  size_in_gbs    = 50
  vpus_per_gb    = 10
}

resource "oci_core_volume_attachment" "other_volume_2_attachment" {
  instance_id = oci_core_instance.example_instance.id
  volume_id   = oci_core_volume.other_volume_2.id
  device      = "/dev/oracleoci_vdb2"
}

Variables

You will need to define the following variables:

variable "oci_user" {
  type        = string
  description = "OCI username"
}

variable "oci_fingerprint" {
  type        = string
  description = "OCI fingerprint"
}

variable "oci_key_file" {
  type        = string
  description = "OCI private key file"
}

variable "oci_tenancy_ocid" {
  type        = string
  description = "OCI tenancy OCID"
}

variable "oci_compartment_ocid" {
  type        = string
  description = "OCI compartment OCID"
}

Notes

  • Make sure to replace the variable values with your actual OCI credentials and compartment OCID.
  • The oci_core_instance resource uses the VM.Standard.E5.Flex shape with 8 OCPU and 64 GB of memory.
  • The oci_core_volume resources create block storage volumes for the boot disk and other disks.
  • The oci_core_volume_attachment resources attach the volumes to the instance.

Please ensure you have the necessary OCI credentials and permissions to create resources in your tenancy. Also, be aware of the costs associated with creating and running resources in Oracle Cloud Infrastructure.


Generated by The Assistant

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment