Terraform

This topic provides sample Terraform configurations to provision individual Oracle Database@AWS resources. These code snippets serve as illustrative examples to help you understand the basic setup and usage.

For detailed configuration options, please see the Terraform documentation linked below.

Table 1-1

Resources hashicorp/aws oracle/oci
ODB Network aws_odb_network Not applicable
Exadata Infrastructure aws_odb_cloud_exadata_infrastructure oci_database_cloud_exadata_infrastructure (update only)
Exadata VM Cluster aws_odb_cloud_vm_cluster oci_database_cloud_vm_cluster (update only)
Autonomous VM Cluster aws_odb_cloud_autonomous_vm_cluster oci_database_cloud_autonomous_vm_cluster (update only)
ODB Peering Connection aws_odb_network_peering_connection Not applicable
Database Home Not applicable oci_database_db_home
Container Database (CDB) Not applicable oci_database_database
Pluggable Database (PDB) Not applicable oci_database_pluggable_database
Autonomous Container Database Not applicable oci_database_autonomous_container_database
Autonomous Database Not applicable oci_database_autonomous_database

For an end-to-end example, see the OCI Multicloud Landing Zone for AWS GitHub repo.

Resources Using AWS Terraform Provider

Configure AWS Terraform Provider
# Configure the Terraform AWS Provider, version 6.15.0 or higher for ODB resources
terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = ">= 6.15.0"
    }
  }
}

# Configure the AWS Provider
provider "aws" {
  region              = "us-west-2"
  shared_config_files = ["~/.aws/config"]
  profile             = "OCI-Demo"
}  
Provision an ODB Network
# Create an ODB Network
resource "aws_odb_network" "this" {
  # Required Arguments
  display_name         = "odb-network"
  availability_zone_id = "usw2-az3"
  client_subnet_cidr   = "10.33.1.0/24"
  backup_subnet_cidr   = "10.33.0.0/24"
  s3_access            = "DISABLED"
  zero_etl_access      = "DISABLED"
  # Optional Arguments
  availability_zone    = "us-west-2c"
  region               = "us-west-2"  
  tags                 = {
    env        = "demo"
  }
} 
Provision an Exadata Infrastructure
# Create an Exadata Infrastructure
resource "aws_odb_cloud_exadata_infrastructure" "this" {
  # Required Arguments
  display_name                     = "exadb-inf-demo"
  shape                            = "Exadata.X11M"
  compute_count                    = 2
  storage_count                    = 3
  availability_zone_id             = "usw2-az3"
  # Optional Arguments
  customer_contacts_to_send_to_oci = [
	{ email = "demo@example.com" }
  ]
  region                           = "us-west-2"
  availability_zone                = "us-west-2c"
  database_server_type             = "X11M"
  storage_server_type              = "X11M-HC"
  tags                             = {
	"env" = "dev"
  }
  maintenance_window {
    patching_mode                    = "ROLLING"
    preference                       = "NO_PREFERENCE"
    is_custom_action_timeout_enabled = false
    custom_action_timeout_in_mins    = 15
    days_of_week                     = null
    hours_of_day                     = null
    lead_time_in_weeks               = null
    months                           = null
    weeks_of_month                   = null
  }
}

# Get list of DB Servers for provisioning VM Cluster / Autonomous VM Cluster
data "aws_odb_db_servers" "this" {
  cloud_exadata_infrastructure_id = aws_odb_cloud_exadata_infrastructure.this.id
}

# Optional output of OCIDs for configurations using OCI Terraform Provider
output "oci_region" {
   value = regex("(?i:region=)([^?&/]+)", aws_odb_cloud_exadata_infrastructure.this.oci_url)[0]
}

output "oci_compartment_ocid" {
   value = regex("(?i:compartmentId=)([^?&/]+)", aws_odb_cloud_exadata_infrastructure.this.oci_url)[0]
}

output "oci_tenant" {
   value = regex("(?i:tenant=)([^?&/]+)", aws_odb_cloud_exadata_infrastructure.this.oci_url)[0]
}

output "oci_cloud_exadata_infrastructure_ocid" {
   value = aws_odb_cloud_exadata_infrastructure.this.ocid
}
Provision an Exadata VM Cluster
locals {
  # IDs of depending resources
  cloud_exadata_infrastructure_id = aws_odb_cloud_exadata_infrastructure.this.id
  db_servers = data.aws_odb_db_servers.this.db_servers[*].id
  odb_network_id = aws_odb_network.this.id
}

# Create a VM Cluster in the Exadata Infrastructure
resource "aws_odb_cloud_vm_cluster" "this" {
  # Optional explicit dependencies
  depends_on = [aws_odb_cloud_exadata_infrastructure.this, aws_odb_network.this]

  # Required Arguments
  cloud_exadata_infrastructure_id = local.cloud_exadata_infrastructure_id
  cpu_core_count                  = 16
  db_servers                      = local.db_servers
  display_name                    = "tf-vmc-demo"
  gi_version                      = "23.0.0.0"
  hostname_prefix                 = "vm"
  odb_network_id                  = local.odb_network_id
  ssh_public_keys                 = [
    file("~/.ssh/demo-ssh-key.pub")
  ]
  data_collection_options {
    is_diagnostics_events_enabled = true
    is_health_monitoring_enabled  = true
    is_incident_logs_enabled      = true
  }
  
  # Optional Arguments
  cluster_name                    = "gic-demo"
  data_storage_size_in_tbs        = 2
  db_node_storage_size_in_gbs     = 120
  is_local_backup_enabled         = false
  is_sparse_diskgroup_enabled     = false
  license_model                   = "BRING_YOUR_OWN_LICENSE"
  memory_size_in_gbs              = 60
  scan_listener_port_tcp          = 1521
  timezone                        = "UTC"
  region                          = "us-west-2"
  tags                            = {
    created_via  = "terraform"
    env          = "demo"
  }

  # timeouts for long running operations
  timeouts {
    create = "24h"
    update = "2h"
    delete = "8h"
  }

  # gi_version will be updated with minor version, e.g. "23.8.0.25.04" 
  lifecycle {
    ignore_changes = [
      gi_version
    ]    
  }
}

# Output OCID of Exadata VM Cluster for Database Home creation (Optional)
output "oci_cloud_vm_cluster_ocid" {
  value = aws_odb_cloud_vm_cluster.this.ocid
}
Provision an Autonomous VM Cluster
locals {
  # IDs of depending resources
  cloud_exadata_infrastructure_id = aws_odb_cloud_exadata_infrastructure.this.id
  db_servers = data.aws_odb_db_servers.this.db_servers[*].id
  odb_network_id = aws_odb_network.this.id
}

# Create an Autonomous VM Cluster in the Exadata Infrastructure
resource "aws_odb_cloud_autonomous_vm_cluster" "this" {
  # Optional explicit dependencies
  depends_on = [aws_odb_cloud_exadata_infrastructure.this, aws_odb_network.this]

  # Required Arguments 
  cloud_exadata_infrastructure_id       = local.cloud_exadata_infrastructure_id
  autonomous_data_storage_size_in_tbs   = 5
  cpu_core_count_per_node               = 40
  db_servers                            = local.db_servers
  display_name                          = "tf-avmc-demo"
  memory_per_oracle_compute_unit_in_gbs = 2
  odb_network_id                        = local.odb_network_id
  scan_listener_port_non_tls            = 1521
  scan_listener_port_tls                = 2484
  total_container_databases             = 2
  maintenance_window {
    preference         = "NO_PREFERENCE"
    lead_time_in_weeks = null
    days_of_week       = null
    hours_of_day       = null
    months             = null
    weeks_of_month     = null
  }

  # Optional Arguments 
  description                = "Autonomous VM Cluster"
  is_mtls_enabled_vm_cluster = true
  license_model              = "BRING_YOUR_OWN_LICENSE"
  time_zone                  = "UTC"
  region                     = "us-west-2"
  tags                       = {
    created_via  = "terraform"
    env          = "demo"
  }
}

# Output OCID of Autonomous VM Cluster for the creation of Autonomous Container Database (Optional)
output "oci_cloud_autonomous_vm_cluster_ocid" {
  value = aws_odb_cloud_autonomous_vm_cluster.this.ocid
}
Provision an ODB Peering Connection
locals {
  # IDs of depending resources
  peer_network_id = module.app_vpc.vpc_attributes.id
  odb_network_id  = aws_odb_network.this.id
}

# Create a Peering Connection between the ODB Network and the VPC
resource "aws_odb_network_peering_connection" "this" {
  # Optional explicit dependencies
  depends_on = [ aws_odb_network.this, module.app_vpc ] 
  
  # Required Arguments 
  odb_network_id  = local.odb_network_id
  peer_network_id = local.peer_network_id
  display_name    = "tf-odb-peering-conn-demo"
  
  # Optional Arguments
  region          = "us-west-2"
  tags            = {
    created_via  = "terraform"
    env        = "demo"
  } 
}

Resources Using OCI Terraform Provider

Configure OCI Terraform Provider
# https://docs.oracle.com/en-us/iaas/Content/terraform/configuring.htm
provider "oci" {
  auth                 = "APIKey"
  region               = "us-boardman-1"
  tenancy_ocid         = "ocid1.tenancy.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  user_ocid            = "ocid1.user.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
  fingerprint          = "xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
  private_key_path     = "your_private_key_path"
  private_key_password = var.oci_private_key_password
}

# Protect sensitive input variables  
variable "oci_private_key_password" {
  type = string
  sensitive = true
}
Provision an Exadata Database
locals {
  # IDs of depending resources
  oci_cloud_vm_cluster_ocid = aws_odb_cloud_vm_cluster.this.ocid
}

# Protect sensitive input variables 
variable "db_admin_password" {
  description = "Database administrator password"
  type = string
  sensitive = true
}

# Create Database Home
resource "oci_database_db_home" "this" {
  vm_cluster_id = var.oci_cloud_vm_cluster_ocid
  source        = "VM_CLUSTER_NEW" 
  display_name  = "dbh19"
  db_version    = "19.28.0.0.0"
}

# Create Container Database
resource "oci_database_database" "this" {
  db_home_id = oci_database_db_home.this.id
  database {
    db_name        = "democdb"
    admin_password = var.db_admin_password
  }
  source     = "NONE"
}

# Create Pluggable Database
resource "oci_database_pluggable_database" "this" {
  container_database_id = oci_database_database.this.id
  pdb_name              = "demopdb"
  pdb_admin_password    = var.db_admin_password
  tde_wallet_password   = var.db_admin_password
}
Provision an Autonomous Database
locals {
  # IDs of depending resources
  oci_cloud_autonomous_vm_cluster_ocid  = aws_odb_cloud_autonomous_vm_cluster.this.ocid
  oci_compartment_ocid = regex("(?i:compartmentId=)([^?&/]+)", aws_odb_cloud_autonomous_vm_cluster.this.oci_url)[0]
}

# Protect sensitive input variables 
variable "db_admin_password" {
  description = "Database administrator password"
  type = string
  sensitive = true
}

# Create the Autonomous Container Database
resource "oci_database_autonomous_container_database" "this" {
  cloud_autonomous_vm_cluster_id = local.oci_cloud_autonomous_vm_cluster_ocid
  display_name = "demo-acd-01"
  patch_model  = "RELEASE_UPDATES"
}

# Create the Autonomous Database 
resource "oci_database_autonomous_database" "this" {
  admin_password           = var.db_admin_password
  compartment_id           = local.oci_compartment_ocid
  db_name                  = "demo-adbd-01"
  compute_model            = "ECPU" 
  compute_count            = 2
  data_storage_size_in_tbs = 1
}
Import Resources for Configuration Updates with OCI Terraform Provider
  1. Declare Terraform Import Blocks for Oracle Database@AWS Resources
    # import Exadata Infrastructure
    import {
        to = oci_database_cloud_exadata_infrastructure.this
        id = "ocid1.cloudexadatainfrastructure.oc1.us-boardman-1.xxxxxxxxxxxxxxxxxx"
    }
    
    # import Cloud VM Cluster
    import {
        to = oci_database_cloud_vm_cluster.this
        id = ""ocid1.cloudvmcluster.oc1.us-boardman-1.xxxxxxxxxxxxxxxxxx"
    }
    
    # import Cloud Autonomous VM Cluster
    import {
        to = oci_database_cloud_autonomous_vm_cluster.this
        id = "ocid1.cloudautonomousvmcluster.oc1.us-boardman-1.xxxxxxxxxxxxxxxxxx"
    }
  2. Generate Terraform Configurations as OCI Resource
    terraform plan -generate-config-out=generated.tf
  3. Apply Terraform Imports or Configuration Updates
    terraform apply
Remove Imported OCI Resource from State Before Destroy (AWS Terraform Provider)
terraform state rm oci_database_cloud_exadata_infrastructure.this
terraform state rm oci_database_cloud_vm_cluster.this
terraform state rm oci_database_cloud_autonomous_vm_cluster.this