Targeting Multiple Regions

Use the Oracle Cloud Infrastructure Terraform provider to create resources in multiple regions.

Use a single Terraform configuration to create Oracle Cloud Infrastructure (OCI) resources in multiple regions.

Create a Provider for Each Region

A Terraform configuration may have only a single OCI Terraform provider block, but to apply configurations to multiple regions, you need to create multiple provider blocks.

A typical OCI Terraform provider block might look like the following:

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

All parameters should be set using variables.

If you want to use more than one region within a single Terraform config, multiple providers are required. Each provider must be given an alias. For example:

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

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

If you want to use more than one region within a single Terraform config, multiple providers are required. Each provider must be given an alias. For example:

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

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

When you sign up for Oracle Cloud Infrastructure, Oracle creates a tenancy for you in one region, which is your home region. The home region has special properties. For example, IAM resources can only be created in your home region. For that reason, you should designate that region with an appropriate alias, like home. Use simple aliases for other regions so that users can easily map configurations to the regions that they want (for example, region2).

Note

Specific regions (us-phoenix-1, us-ashburn-1, and so on) are not hardcoded into either the region or alias fields.

Finding Your Home Region

When deploying to multiple OCI regions, you might want to look up your home region dynamically, so you don't have to explicitly pass the home region as a configuration variable.

One typical use case might require provisioning resources to a specific region using a Terraform configuration that also creates a compartment. Since the compartment must be created in your home region, you could use code like the following to automatically look up the home region:

provider oci {}

provider oci { 
    alias = "home" region = lookup(local.region_map, data.oci_identity_tenancy.tenancy.home_region_key)
}

variable tenancy_id {}

data oci_identity_regions regions {}

data oci_identity_tenancy tenancy {
    tenancy_id = var.tenancy_id
}

locals {
    region_map = { for r in data.oci_identity_regions.regions.regions : r.key => r.name }
}

output home_region {
    value = lookup(local.region_map, data.oci_identity_tenancy.tenancy.home_region_key)
} 

Provision a Resource

To provision a resource in a region, specify the aliased provider name in the resource.

For example:

resource "oci_core_instance" "test_instance" {
 provider = oci.home
 ...
}

Modules and Multiple Regions

Typically, a module should use only a single region. If more regions are needed, you should use separate modules.

If the config contains multiple providers, the module should specify the provider to use by using the following format:

module "compartments" {
  source       = "./compartments"
  providers = {
    oci = "oci.home"
  }
}