Terraform is all about infrastructure (and platform) as code. Creating OCI resources through Terraform is probably the best way to manage your cloud resources. I have written several articles about that. In this article I will quickly show the steps you can take for querying OCI resources using Terraform – especially in OCI Cloud Shell. Using Terraform plan files, you can easily construct possibly complex queries of OCI resources. You can create TF files that contain predefined queries for specific sets of resources that you may want to regularly inspect.
Anyways, whatever you want to do with the query results, here is how to very rapidly get going.
Open Cloud Shell in your OCI console.
Execute the following Linux shell commands:
mkdir query-oci
cd query-oci
nano query.tf
Paste the following content and provide values for tenancy_ocid.and region.
Then click CTRL+X (to exit nano editor).
Click Shift+Y (to save file file)
Execute terraform init
Then execute (now we get to the querying part) terraform apply.
Type yes and the query completes:
To add more queries, either edit the file query.tf or create a new file ; it can have any name , just make sure the extension of the file is .tf. For example:
nano compartment.tf
paste:
CTRL+X, Shift+Y. Terraform apply:
You can create linked queries as well, where the result from one query is used as a query condition in another query. An example of this:
Find the functions in an application called cloudnative-2021App. The first data source is used to retrieve the application. Its identifier is used to query the functions:
Running the query gives me a list of all three functions in this application
For details on which queries you can perform against OCI, look at the documentation for OCI Provider for Terraform and check out the Data Sources: