Set up resource discovery to generate Terraform files for existing resources in your compartment.
Use the OCI resource discovery feature to:
Create state files for existing resources in the Console, and then add those resources to a Terraform setup.
Duplicate your existing infrastructure in a new tenancy or region.
Detect state drift for updated resources.
In this tutorial, you set up Oracle Cloud Infrastructure Terraform provider's resource discovery feature in your local environment. To confirm your setup, you run resource discovery to fetch information from your tenancy and create a script for it.
Key tasks include how to:
Create RSA keys.
Install Terraform OCI provider binaries.
Set up Terraform OCI provider API authentication variables.
Authenticate your OCI provider CLI commands.
Create a script in your environment, regarding the availability domains in your tenancy, through the resource discovery feature.
If you're using Oracle Cloud Infrastructure
Cloud Shell, the OCI Terraform Provider is already installed and you don't need to create RSA keys. Skip that section and proceed to Add List Policy.
Prepare your environment for authenticating and running resource discovery commands. Also, gather the information your account needs to authenticate your commands.
You create RSA keys for API signing in to your Oracle Cloud Infrastructure account.
Note
If you're using Cloud Shell or Resource Manager, skip creating the RSA keys. You're already authenticated when you sign in to the OCI
Console.
Open a terminal window.
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.
For this tutorial, use tf-oci for the
<shorter-alias>, instead of the
<source-executable>.
Go back to your home directory:
Copy
cd
Check the Terraform OCI provider version:
Note
On MacOS, you need to create a security exception for the executable.
Copy
tf-oci
Example output: [INFO] terraform-provider-oci 4.59.0.
2. Create an Authentication Script 🔗
Create a shell script to assign authentication information to OCI provider authentication variables. Your Oracle Cloud Infrastructure account authenticates your OCI provider commands through the values assigned to these parameters.
If you're using Cloud Shell or Resource Manager, you don't need to add authentication. Proceed to section 3. Discover a Resource.
Create an executable script file, somewhere in your PATH, and name it
provider-oci.sh. For example, if
~/bin is in your PATH, the steps are as follows:
If you're using Cloud Shell, replace terraform-provider-oci_<version> with the file name that you found in step 2.
Sample output:
...
INFO <date> [INFO] ===> Generating resource 'oci_identity_availability_domain.export_QnsC-US-ASHBURN-AD-1'
INFO <date> [INFO] ===> Generating resource 'oci_identity_availability_domain.export_QnsC-US-ASHBURN-AD-2'
INFO <date> [INFO] ===> Generating resource 'oci_identity_availability_domain.export_QnsC-US-ASHBURN-AD-3'
...
INFO <date> Found 6 'availability_domain' resources. Generated under '/<home-directory>/resource-discovery/availability_domain.tf'.
INFO <date> === COMPLETED ===
Note
The tenancy OCID is the compartment OCID for the root compartment. Providing a specific <compartment-ocid> or your <tenancy-ocid> outputs the same availability domains.
To discover identity resources, you don't need to mention a compartment OCID. In the previous example, you get the same result if you remove the compartment_id from the command. The compartment_id is there for you to learn the syntax for other services.
Instead of terraform-provider-oci, use your symbolic link command:
tf-oci
Review the Parameter Description section to add proper values to the parameters in the base command:
Use the export command to perform resource discovery:
-command=export
Add the OCID of the compartment that you're discovering resources in:
-compartment_id=<tenancy-ocid>
Create and then specify a directory for the discovered resources:
Example: -output_path=$HOME/resource-discovery
Search for the phrase availability_domain and observe the following information:
Resources that are dependent on availability domains will be generated under availability_domain.tf file. These include:
oci_core_boot_volume
oci_file_storage_file_system
oci_file_storage_mount_target
oci_file_storage_snapshot
For services, use:
availability_domain (Even though it's not a service, it works with service.)
Example: -services=availability_domain
You don't need quotation marks around the service names.
The provider.tf denotes that you are using the Terraform OCI provider with provider oci { }.
The provider.tf file does not include your authentication information, because you provide authentication information through your environment variables.
Congratulations! Your Oracle Cloud Infrastructure account can now authenticate your Terraform OCI provider commands. And your environment is ready to run the resource discovery commands.