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

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

But what if you don’t want to pour over the docs to look the latest image OCID up? You can use Terraform Data Sources to do so. But be aware that this convenience comes at a cost.

Once a newer image version is released, Terraform will detect the new OCID and destroy your existing VM. So once you know the OCID, change your code to the hard-coded version instead of the dynamic one.

Getting the OCID via the Terraform data source

With the warning out of the way, let’s try to get the latest OL8 image’s OCID via a 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
}

This code snippet grabs the Linux/x86-64 image, if you want aarch64 make sure to adapt the shape for a suitable one like VM.Standard.A{1,2,4}.Flex.

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 and later.

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.

HOWEVER: just because I don’t need to update the Terraform script doesn’t mean there isn’t a caveat. As soon as a new OL8 image is released and I run a terraform apply my existing VM is replaced with a new one, using the latest image. Make sure to mitigate against this threat by either temporarily hard-coding the current OCID or simply don’t run any Terraform commands.