Search code examples
terraformoracle-cloud-infrastructure

Terraform. Access the current Oracle Cloud tenancy specified by provider


I have an OCI provider for my Terraform script:

provider "oci" {
    region = "eu-frankfurt-1"
}

Where all the attributes are defined over environment variables as specified by Oracle.

export TF_VAR_tenancy_ocid=...
export TF_VAR_user_ocid=...

Other elements of the scripts requires an ID of the tenancy. It there a way to get the ID or the current tenancy without reading an environment variables again?

AWS provider for Terraform allows to reference the current account as:

data "aws_caller_identity" "current" {}

output "account_id" {
   value = data.aws_caller_identity.current.account_id
}

It there something similar for OCI?


Solution

  • Since tenancy ID is already set in environment variable TF_VAR_tenancy_ocid you just need to declare a variable:

    variable "tenancy_ocid" {
       description = "Tenancy OCID"
    }
    

    Make sure that the variable name above exactly matches with whatever is present after TF_VAR_

    Terraform automatically set the values of these variables using environment variables set using TF_VAR_variable-name

    Then you can use the value like below:

    output "account_id" {
       value = var.tenancy_ocid
    }
    

    You can also look for data sources in the terraform oci provider docs docs