Getting started quickly with querying OCI from Terraform in Cloud Shell

Lucas Jellema
0 0
Read Time:1 Minute, 32 Second

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

About Post Author

Lucas Jellema

Lucas Jellema, active in IT (and with Oracle) since 1994. Oracle ACE Director and Oracle Developer Champion. Solution architect and developer on diverse areas including SQL, JavaScript, Kubernetes & Docker, Machine Learning, Java, SOA and microservices, events in various shapes and forms and many other things. Author of the Oracle Press book Oracle SOA Suite 12c Handbook. Frequent presenter on user groups and community events and conferences such as JavaOne, Oracle Code, CodeOne, NLJUG JFall and Oracle OpenWorld.
Happy
Happy
0 %
Sad
Sad
0 %
Excited
Excited
0 %
Sleepy
Sleepy
0 %
Angry
Angry
0 %
Surprise
Surprise
0 %

Average Rating

5 Star
0%
4 Star
0%
3 Star
0%
2 Star
0%
1 Star
0%

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: