As with all cloud providers you need to specify an operating system image when creating virtual machines using Terraform in Oracle Cloud Infrastructure (OCI). This can either be an Oracle supplied image, or a custom image you built. This post describes how to fetch the most recent Oracle-provided image for Oracle Linux 7 in Terraform. I am planning another post for Oracle Linux 8 in the future.
Terraform versions
When writing this post Terraform 0.14.5 was the latest and greatest release. The terraform init command downloaded release 4.10.0 of the OCI provider.
Supported Operating System Images
Part of the details you need to provide when starting a VM in OCI is the operating system image ID. Oracle calls their cloud identifiers “Oracle Cloud ID” or OCID for short. You can find all supported image OCIDs as part of the OCI documentation.
For instance, if you want to create an Oracle Linux 7 image, the latest “standard” image OCIDs at the time of writing could be found here (don’t use it, it’s outdated). Now all you need to do is find the OCID corresponding to your region, suitable with the VM shape you want to run. My target region is eu-frankfurt-1 and I want to start an always-free VM, so I’ll have to pick this OCID for Oracle-Linux-7.9-2021.01.12-0:
ocid1.image.oc1.eu-frankfurt-1.aaaaaaaa5w2lrmsn6wpjn7fbqv55curiarwsryqhoj4dw5hsixrl37hrinja
I love the documentation page for reference, but it is not very practical. A new release of Oracle’s image requires me to update the VM configuration in Terraform. Wouldn’t it be nice if I could look the most recent version up programmatically?
Looking up the latest OCID for your Oracle Linux 7 image
This little code snippet shows how to fetch the OCID for the latest Oracle Linux 7.9 image for use with an always-free VM instance. I am running this in my cloud shell, if you want to run this locally you’d have to update the provider configuration accordingly.
provider "oci" {
auth = "InstancePrincipal"
region = var.oci_region
}
# this is the data source for Oracle Linux 7 images
data "oci_core_images" "ol7_latest" {
compartment_id = var.compartment_ocid
operating_system = "Oracle Linux"
operating_system_version = "7.9"
shape = "VM.Standard.E2.1.Micro"
}
# now let's print the OCID
output "latest_ol7_image" {
value = data.oci_core_images.ol7_latest.images.0.id
}
You’ll notice this code snippet doesn’t create anything, it merely fetches information and prints it. Terraform uses data sources for this purpose, returning the desired information. In the case of the oci_core_images data source Terraform will return the list of images matching the specification.
Once the usual terraform init has been run you can execute the code and print the image OCID:
$ terraform apply [...] Outputs: latest_ol7_image = "ocid1.image.oc1.eu-frankfurt-1.aaaaaaaa5w2lrmsn6wpjn7fbqv55curiarwsryqhoj4dw5hsixrl37hrinja"
Nice!
Now please don’t fall for the trap and reference the data source in your oci_core_instance_resource or else each new image will trigger a destroy/create operation for your VM. This is probably not what you want.