Terraform tips’n’tricks: getting the latest Oracle Linux 8 image OCID programatically

This post is a direct follow up to the previous one where I shared how I used a Terraform data source to fetch the latest Oracle-provided Oracle Linux 7 cloud image identifier. This time around I’d like to fetch the latest Oracle Cloud ID (OCID) for Oracle Linux 8. It’s a different approach and instead of a single article covering both Oracle Linux versions I decided to use a more the search-engine friendly method of splitting the topics.

Terraform versions

I’m still using Terraform 0.14.5/OCI provider 4.10.0.

What’s the latest Oracle Linux 8 OCID?

Referring back to the documentation the latest Oracle-provided Oracle Linux 8 image at the time of writing is Oracle-Linux-8.3-2021.01.12-0. As my home region is eu-frankfurt-1 and I want to create an always-free VM I need to go with this OCID:

ocid1.image.oc1.eu-frankfurt-1.aaaaaaaa3qkhrcj4xo5beoffschi2ofagkufpc6bp2mnkcglpsq4pivxp2dq

Getting the OCID via the Terraform data source

So now let’s try to get the same OCID via Terraform’s data source. Here’s the code snippet I used in Cloud Shell:

# using Instance Principal for authentication
provider "oci" {
    auth = "InstancePrincipal"
    region = var.oci_region

}

data "oci_core_images" "ol8_latest" {

        compartment_id = var.compartment_ocid

        operating_system = "Oracle Linux"
        operating_system_version = "8"
        shape = "VM.Standard.E2.1.Micro"
}


output "latest_ol8_image" {
        value = data.oci_core_images.ol8_latest.images.0.id
} 

Note that unlike with Oracle Linux 7 you don’t specify the “dot” release. Doing so will raise an error with Terraform 0.14.5/OCI provider 4.10.0.

Let’s run the code:

 $ terraform apply

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

latest_ol8_image = "ocid1.image.oc1.eu-frankfurt-1.aaaaaaaa3qkhrcj4xo5beoffschi2ofagkufpc6bp2mnkcglpsq4pivxp2dq"  

A quick comparison reveals that my code fetched the intended OCID; QED.

Summary

I really like not having to specify the dot release with Oracle’s provided OL8 as it’s more future-proof. As soon as Oracle Releases Oracle Linux 8.4 in their cloud, I don’t need to change a single line of code to use it.

On the other hand, if my code requires a different (more specific) image, some trickery is needed. I’ll write about a possible solution in another post.