Getting started quickly with querying OCI from Terraform in Cloud Shell

Lucas Jellema

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

imagePaste the following content and provide values for tenancy_ocid.and region.

Then click CTRL+X (to exit nano editor).

imageClick 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 or create a new file ; it can have any name , just make sure the extension of the file is .tf. For example:




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:


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Next Post

Run and Access GUI inside VS Code DevContainers

VS Code DevContainers allow us to easily configure and run a container with our development tech stack for the specific project or product we are working on – and have that fully integrated with our VS Code development environment. It does so leveraging Docker Desktop and WSL2. With just a […]
%d bloggers like this: