Set up Oracle Cloud Infrastructure
Terraform provider scripts, documented in the Terraform Registry, to connect to an OCI account. Confirm the setup by fetching information from the tenancy.
Key tasks include how to:
Create RSA keys.
Set up Oracle Cloud Infrastructure Terraform provider scripts:
Authenticate your Terraform scripts.
Get information about the availability domains in your tenancy.
This tutorial uses an Oracle Linux VM environment with an AMD shape for its examples, but you can use any environment mentioned in this section.
1. Prepare 🔗
Prepare your environment for authenticating and running Terraform scripts. Also, gather the information your account needs to authenticate the scripts.
This tutorial suggests that you install the latest version of Terraform that's supported for OCI
Resource Manager. Resource Manager is a service for creating Terraform templates for OCI resources. In case you want to use Resource Manager with your Terraform scripts later, then your Terraform version is supported.
In your environment, check the Terraform version.
Copy
terraform -v
If you don't have the latest supported Terraform version, then install the latest supported version using the following steps.
If you see a connection error, and you're on VPN, check your proxy settings. See Troubleshooting.
Unzip the file. Example:
Copy
unzip terraform_1.5.7_linux_amd64.zip
Move the unzipped folder to a folder that contains the binaries of the third-party apps that you install. For example, for Oracle Linux, move the unzipped folder to /usr/local/bin:
Copy
sudo mv terraform /usr/local/bin
Go back to your home directory:
Copy
cd
Check the Terraform version:
Copy
terraform -v
Example: Terraform v1.5.7 on linux_amd64
Disregard any message indicating that the version of Terraform is out of date. The important thing is to use the latest supported Terraform version.
Under your home directory, make an .oci directory.
Copy
mkdir <your-home-directory>/.oci
Example for Oracle Linux:
Copy
mkdir /home/opc/.oci
Note
If you're using Windows Subsystem for Linux (WSL), create the /.oci directory directly in the Linux environment. If you create the /.oci directory in a /mnt folder (Windows file system), you're required to use the chmod command to change permissions for the WSL configuration files.
In the navigation bar, select the Profile menu and then select User settings or My profile, depending on the option that you see.
Select API keys.
Select Add API key.
Select Paste a public key.
Paste the value from the previous step, including the lines with BEGIN PUBLIC KEY and END PUBLIC KEY.
Select Add.
The Configuration file preview dialog box opens. Example:
[DEFAULT]
user=ocid1.user.oc1..exampleid
fingerprint=exampleid
tenancy=ocid1.tenancy.oc1..exampleid
region=us-ashburn-1
key_file=<path to your private keyfile> # TODO
Select Copy, then paste into your notepad.
The configuration file preview includes information you'll need later, such as tenancy and user OCIDs, fingerprint, and region.
You have now set up the RSA keys to connect to your OCI account.
If your username is in the Administrators group, then skip this section. Otherwise, ask your administrator to add the following policy to your tenancy:
Copy
allow group <a-group-that-your-username-belongs-to> to read all-resources in tenancy
With this privilege, you can list all the resources in your tenancy.
Prepare the information you need to authenticate your Terraform scripts and copy the information into a notepad.
Collect the path to the private key that you added at Create RSA Keys.
Example for Oracle Linux: /home/opc/.oci/<your-rsa-key-name>.pem
Collect the user OCID, fingerprint, tenancy OCID, and region from the API key you added at Create RSA Keys. (You might have this in your notepad already.)
From the Actions menu for the key, select View configuration file.
The Configuration file preview dialog box opens. Example:
[DEFAULT]
user=ocid1.user.oc1..exampleid
fingerprint=xx:xx:xx...xx
tenancy=ocid1.tenancy.oc1..exampleid
region=us-ashburn-1
key_file=<path to your private keyfile> # TODO
Select Copy, then paste into your notepad.
2. Create Scripts 🔗
Create scripts for authentication, to fetch data from your account, and to print outputs.
First, set up a directory for Terraform scripts. Then add a provider script so your OCI account can authenticate the scripts running from this directory. Finally, add a versions script containing a required_providers block to declare required provider versions.
In <your-home-directory>, create a directory called tf-provider and change to that directory.
In this section, you fetch a list of the availability domains in your tenancy. By fetching data, you confirm that your OCI account authenticates your provider.tf script and you get information from your account.
In the tf-provider directory, create a file called availability-domains.tf.
Important
Ensure that all the *.tf files are in the same directory. Terraform processes all the files in a directory in the correct order, based on their relationship. (For a modular approach and future reuse, put provider information in a separate file from other scripts.)
Add the following code to availability-domains.tf, replacing the field with brackets, with information from Gather Required Information.
Copy
# Source from https://registry.terraform.io/providers/oracle/oci/latest/docs/data-sources/identity_availability_domains
# Tenancy is the root or parent to all compartments.
# For this tutorial, use the value of <tenancy-ocid> for the compartment OCID.
data "oci_identity_availability_domains" "ads" {
compartment_id = "<tenancy-ocid>"
}
Note
The data source gets a list of availability domains in your entire tenancy. The tenancy is the compartment OCID for the root compartment. Providing a specific "<compartment-ocid>" or the "<tenancy-ocid>" outputs the same list.
The data source oci_identity_availability_domains, fetches a list of availability domains. In this section, you declare an output block to print the fetched information.
In the tf-provider directory, create a file called outputs.tf.
Add the following code to outputs.tf.
Copy
# Output the "list" of all availability domains.
output "all-availability-domains-in-your-tenancy" {
value = data.oci_identity_availability_domains.ads.availability_domains
}
Save the outputs.tf file.
Important
Ensure that all the *.tf files are in the same directory. Terraform processes all the files in a directory in the correct order, based on their relationship.
Create an execution plan to check whether the changes shown in the execution plan match your expectations, without changing the real resources.
Run the Terraform plan command.
Copy
terraform plan
Example output:
data.oci_identity_availability_domains.ads: Reading...
data.oci_identity_availability_domains.ads: Read complete after 1s [id=xxx]
Changes to Outputs:
+ all-availability-domains-in-your-tenancy = [
+ {
+ compartment_id = "ocid1.tenancy.oc1..xxx"
+ id = "ocid1.availabilitydomain.xxx"
+ name = "QnsC:US-ASHBURN-AD-1"
},
+ {
+ compartment_id = "ocid1.tenancy.oc1..xxx"
+ id = "ocid1.availabilitydomain.yyy"
+ name = "QnsC:US-ASHBURN-AD-2"
},
+ {
+ compartment_id = "ocid1.tenancy.oc1..xxx"
+ id = "ocid1.availabilitydomain.zzz"
+ name = "QnsC:US-ASHBURN-AD-3"
},
]
You can apply this plan to save these new output values to the Terraform state, without changing any real
infrastructure.
Note
You're fetching data, so the plan shows that you're only adding outputs. You're not adding, changing, or destroying any resources.
You're using the output.tf file instead of the -out option, so you can ignore the following message:
Note: You didn't use the -out option to save this plan, so Terraform can't guarantee to take exactly these actions if you run "terraform
apply" now.
Ensure that you added quotation marks around string values.
Run the scripts.
Can not create client, bad configuration: did not find a proper configuration for private key
The Terraform scripts can't find the RSA private key.
Repeat the steps for creating RSA keys and use the updated key.
Ensure that in the provider.tf file, the variable describing the RSA is called private_key_path.
private_key_path = "<rsa-private-key-path>"
Ensure the path to the RSA private key in the provider.tf file is correct. For example, ensure that the path starts with a slash and has the correct name for the private key, <your-rsa-key-name>.pem
Remove environment variables from the <rsa-private-key-path>.
For example, in the provider.tf file, instead of $HOME/.oci/<rsa-private-key-path>, use /home/opc/<rsa-private-key-path>.
Note
If you're using Windows Subsystem for Linux (WSL), create the /.oci directory directly in the Linux environment. If you create the /.oci directory in a /mnt folder (Windows file system), you're required to use the chmod command to change permissions for the WSL configuration files.