Christoph's 2 Cents

A Backup for My Brain!

bashCloudDevOpsjqLinuxOracle Cloud InfrastructrureOracle LinuxOracle Toolsshell scriptingTerraform

Terraform: Getting the Latest OS Image

When provisioning a compute instance, you may want to grab the latest available version, as Oracle frequently updates its images. The list below shows the current Oracle Linux images:

$ oci compute image list --all --output table --query "data [?\"operating-system\" == 'Oracle Linux'].{name:\"display-name\",date:\"time-created\",size:\"size-in-mbs\",os:\"operating-system\",version:\"operating-system-version\"}"
+----------------------------------+----------------------------------------+--------------+-------+---------+
| date                             | name                                   | os           | size  | version |
+----------------------------------+----------------------------------------+--------------+-------+---------+
| 2018-11-19T21:06:24.069000+00:00 | Oracle-Linux-7.6-Gen2-GPU-2018.11.19-0 | Oracle Linux | 47694 | 7.6     |
| 2018-11-19T20:29:29.553000+00:00 | Oracle-Linux-7.6-2018.11.19-0          | Oracle Linux | 47694 | 7.6     |
| 2018-10-17T01:20:25.385000+00:00 | Oracle-Linux-7.5-Gen2-GPU-2018.10.16-0 | Oracle Linux | 47694 | 7.5     |
| 2018-09-26T13:44:27.529000+00:00 | Oracle-Linux-7.5-Gen2-GPU-2018.09.25-0 | Oracle Linux | 47694 | 7.5     |
| 2018-10-17T01:16:47.961000+00:00 | Oracle-Linux-7.5-2018.10.16-0          | Oracle Linux | 47694 | 7.5     |
| 2018-09-26T13:00:54.490000+00:00 | Oracle-Linux-7.5-2018.09.25-0          | Oracle Linux | 47694 | 7.5     |
| 2018-11-19T20:03:42.791000+00:00 | Oracle-Linux-6.10-2018.11.19-0         | Oracle Linux | 47694 | 6.10    |
| 2018-10-29T15:57:24.697000+00:00 | Oracle-Linux-6.10-2018.10.17-1         | Oracle Linux | 47694 | 6.10    |
| 2018-09-26T10:22:59.338000+00:00 | Oracle-Linux-6.10-2018.09.25-0         | Oracle Linux | 47694 | 6.10    |
+----------------------------------+----------------------------------------+--------------+-------+---------+

As you can see, there are multiple images for Oracle Linux 6.10 and 7.5. To get the latest 7.5 for example, you have to look at the time-crated or the display name, and pick the latest one.

Fortunately, the OCI terraform data provider for images automatically sorts the image list by date descending. So you can simply filter by operating system and version, and pick the first element (index 0) from the list. This will be the latest image version.

The syntax to fetch the latest image requires a filter with a regular expression in order to filter out the GPU images that are only available for certain bare metal shapes:

provider "oci" {
  region           = "us-ashburn-1"
}

data "oci_core_images" "test_images" {
    #Required
    compartment_id = "ocid1.compartment.oc1..aaa..."

    #Optional
    operating_system         = "Oracle Linux"
    operating_system_version = "6.10"
    shape                    = "VM.Standard2.16"

    filter {
        name   = "display_name"
        values = ["Oracle-Linux-[0-9].[0-9]{1,2}-[0-9]{4}.[0-9]{2}.[0-9]{2}"]
        regex  = true
    }
}

output "image_display_name" {
  value = "${lookup(data.oci_core_images.test_images.images[0],"display_name")}"
}
output "image_ocid" {
  value = "${lookup(data.oci_core_images.test_images.images[0],"id")}"
}

$ terraform apply --auto-approve
data.oci_core_images.test_images: Refreshing state...

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

Outputs:

image_display_name = Oracle-Linux-6.10-2018.11.19-0
image_ocid = ocid1.image.oc1.iad.aaaaaaaa63dxj6hspkwqz37tajxkrpdb4xr7cdpy6dyspcufqg32m65pj4sq

The values attribute of the filter contains a regular expression to filter out the display names that begin with “Oracle-Linux-” followed by a version number and creation date. This filters out the images with GPU in the name. Specifying the shape is optional but highly recommended so that you only get images that fit your shape.

Enjoy!