Getting started quickly with querying OCI from Terraform in Cloud Shell image 7

Getting started quickly with querying OCI from Terraform in Cloud Shell

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.

image

Execute the following Linux shell commands:
mkdir query-oci
cd query-oci
nano query.tf

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

image

Then execute (now we get to the querying part) terraform apply.

image

Type yes and the query completes:

image

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:

image

CTRL+X, Shift+Y. Terraform apply:

image

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:

image

Running the query gives me a list of all three functions in this application

image

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:

image

Leave a Reply

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