From 62691d933e313c3bfa54e9a3c65768796753f596 Mon Sep 17 00:00:00 2001 From: Michele Cereda Date: Sun, 2 Jul 2023 14:43:52 +0200 Subject: [PATCH] feat: use list of images from data source as fallback --- .../example.tfvars | 7 ++- .../main.tf | 60 ++++++++++++------- .../variables.tf | 18 ++++-- knowledge base/oci-cli.md | 7 ++- 4 files changed, 62 insertions(+), 30 deletions(-) diff --git a/examples/terraform/oracle cloud/create a free tier ampere instance/example.tfvars b/examples/terraform/oracle cloud/create a free tier ampere instance/example.tfvars index 05c8c00..eac6d4c 100644 --- a/examples/terraform/oracle cloud/create a free tier ampere instance/example.tfvars +++ b/examples/terraform/oracle cloud/create a free tier ampere instance/example.tfvars @@ -4,8 +4,11 @@ compartment_id = "FIXME" # get it with `oci iam compartment list` memory_in_gbs = 24 ocpus = 4 -# get it with `oci compute image list -c 'tenancy_id'` -# or check https://docs.oracle.com/en-us/iaas/images/ +# get the list of images using +# - `oci compute image list -c 'tenancy_id' --lifecycle-state 'AVAILABLE' --shape 'VM.Standard.A1.Flex'` +# - the 'oci_core_images' data source +# see https://registry.terraform.io/providers/oracle/oci/latest/docs/data-sources/core_images +# - https://docs.oracle.com/en-us/iaas/images/ source_id = "ocid1.image.oc1.eu-amsterdam-1.aaaaaaaavmra3s4va4fqd4vlcrqc5v5jyqov5vdla3x3b6gzc64n6dkpuqua" ssh_authorized_keys = <<-EOT diff --git a/examples/terraform/oracle cloud/create a free tier ampere instance/main.tf b/examples/terraform/oracle cloud/create a free tier ampere instance/main.tf index 0b9cd8b..74e2963 100644 --- a/examples/terraform/oracle cloud/create a free tier ampere instance/main.tf +++ b/examples/terraform/oracle cloud/create a free tier ampere instance/main.tf @@ -9,50 +9,64 @@ terraform { } } -# See https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_vcn -resource "oci_core_vcn" "this" { +resource "oci_core_vcn" "default" { + # https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_vcn compartment_id = var.compartment_id cidr_blocks = ["10.0.0.0/16"] } -# See https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_subnet -resource "oci_core_subnet" "this" { +resource "oci_core_subnet" "default" { + # https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_subnet compartment_id = var.compartment_id - vcn_id = oci_core_vcn.this.id + vcn_id = oci_core_vcn.default.id cidr_block = "10.0.0.0/24" } -# Needed to be able to connect to the instance from the Internet. -# Need to create a route table with the default route 0.0.0.0/0 pointing to the -# internet gateway, and associate the subnet to it. -# See https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_internet_gateway -resource "oci_core_internet_gateway" "this" { +# To be able to connect to the instance from the Internet, one needs to create a +# route table with the default route 0.0.0.0/0 pointing to an internet gateway, +# and associate the subnet to it. + +resource "oci_core_internet_gateway" "default" { + # https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_internet_gateway compartment_id = var.compartment_id - vcn_id = oci_core_vcn.this.id + vcn_id = oci_core_vcn.default.id } -resource "oci_core_route_table" "this" { +resource "oci_core_route_table" "default" { + # https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_route_table compartment_id = var.compartment_id - vcn_id = oci_core_vcn.this.id + vcn_id = oci_core_vcn.default.id route_rules { destination = "0.0.0.0/0" destination_type = "CIDR_BLOCK" - network_entity_id = oci_core_internet_gateway.this.id + network_entity_id = oci_core_internet_gateway.default.id } } -resource "oci_core_route_table_attachment" "this" { - subnet_id = oci_core_subnet.this.id - route_table_id = oci_core_route_table.this.id +resource "oci_core_route_table_attachment" "default" { + # See https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_route_table_attachment + subnet_id = oci_core_subnet.default.id + route_table_id = oci_core_route_table.default.id +} + +data "oci_core_images" "available" { + # https://registry.terraform.io/providers/oracle/oci/latest/docs/data-sources/core_images + compartment_id = var.compartment_id + operating_system = var.operating_system + operating_system_version = var.operating_system_version + shape = var.shape + state = "AVAILABLE" + sort_by = "DISPLAYNAME" + sort_order = "DESC" } -# See https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_instance resource "oci_core_instance" "this" { + # https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_instance compartment_id = var.compartment_id availability_domain = var.availability_domain shape = var.shape create_vnic_details { - subnet_id = oci_core_subnet.this.id + subnet_id = oci_core_subnet.default.id } extended_metadata = {} @@ -67,7 +81,13 @@ resource "oci_core_instance" "this" { source_details { boot_volume_size_in_gbs = var.boot_volume_size_in_gbs - source_id = var.source_id + source_id = coalesce(var.source_id, data.oci_core_images.available.images[0].id) source_type = var.source_type } + + lifecycle { + ignore_changes = [ + source_details["source_id"] + ] + } } diff --git a/examples/terraform/oracle cloud/create a free tier ampere instance/variables.tf b/examples/terraform/oracle cloud/create a free tier ampere instance/variables.tf index 277d4cd..ee979a2 100644 --- a/examples/terraform/oracle cloud/create a free tier ampere instance/variables.tf +++ b/examples/terraform/oracle cloud/create a free tier ampere instance/variables.tf @@ -13,11 +13,6 @@ variable "compartment_id" { # Instance #################### -variable "shape" { - type = string - default = "VM.Standard.A1.Flex" -} - variable "memory_in_gbs" { type = number default = 24 @@ -31,8 +26,21 @@ variable "boot_volume_size_in_gbs" { type = number default = 50 } +variable "operating_system" { + type = string + default = "Oracle Linux" +} +variable "operating_system_version" { + type = number + default = 9 +} +variable "shape" { + type = string + default = "VM.Standard.A1.Flex" +} variable "source_id" { type = string + default = null } variable "source_type" { type = string diff --git a/knowledge base/oci-cli.md b/knowledge base/oci-cli.md index b598d99..d237e09 100644 --- a/knowledge base/oci-cli.md +++ b/knowledge base/oci-cli.md @@ -3,8 +3,8 @@ Oracle Cloud Infrastructure CLI. 1. [TL;DR](#tldr) -2. [Configuration](#configuration) -3. [Further readings](#further-readings) +1. [Configuration](#configuration) +1. [Further readings](#further-readings) ## TL;DR @@ -37,8 +37,9 @@ oci iam availability-domain list -c 'tenancy_id' # Output is paginated. oci compute image list -c 'tenancy_id' --all oci compute image list -c 'tenancy_id' \ + --lifecycle-state 'AVAILABLE' --shape 'VM.Standard.A1.Flex' \ --operating-system 'Oracle Linux' --operating-system-version '8' \ - --lifecycle-state 'AVAILABLE' + --sort-by 'DISPLAYNAME' --sort-order 'DESC' # List available compute instance plugins. # Requires to be given the OS and its version.