feat: use list of images from data source as fallback

This commit is contained in:
Michele Cereda
2023-07-02 14:43:52 +02:00
parent b4dce09af8
commit 62691d933e
4 changed files with 62 additions and 30 deletions

View File

@@ -4,8 +4,11 @@ compartment_id = "FIXME" # get it with `oci iam compartment list`
memory_in_gbs = 24 memory_in_gbs = 24
ocpus = 4 ocpus = 4
# get it with `oci compute image list -c 'tenancy_id'` # get the list of images using
# or check https://docs.oracle.com/en-us/iaas/images/ # - `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" source_id = "ocid1.image.oc1.eu-amsterdam-1.aaaaaaaavmra3s4va4fqd4vlcrqc5v5jyqov5vdla3x3b6gzc64n6dkpuqua"
ssh_authorized_keys = <<-EOT ssh_authorized_keys = <<-EOT

View File

@@ -9,50 +9,64 @@ terraform {
} }
} }
# See https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_vcn resource "oci_core_vcn" "default" {
resource "oci_core_vcn" "this" { # https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_vcn
compartment_id = var.compartment_id compartment_id = var.compartment_id
cidr_blocks = ["10.0.0.0/16"] cidr_blocks = ["10.0.0.0/16"]
} }
# See https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_subnet resource "oci_core_subnet" "default" {
resource "oci_core_subnet" "this" { # https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_subnet
compartment_id = var.compartment_id 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" cidr_block = "10.0.0.0/24"
} }
# Needed to be able to connect to the instance from the Internet. # To be able to connect to the instance from the Internet, one needs to create a
# Need to create a route table with the default route 0.0.0.0/0 pointing to the # route table with the default route 0.0.0.0/0 pointing to an internet gateway,
# internet gateway, and associate the subnet to it. # 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" { resource "oci_core_internet_gateway" "default" {
# https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_internet_gateway
compartment_id = var.compartment_id 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 compartment_id = var.compartment_id
vcn_id = oci_core_vcn.this.id vcn_id = oci_core_vcn.default.id
route_rules { route_rules {
destination = "0.0.0.0/0" destination = "0.0.0.0/0"
destination_type = "CIDR_BLOCK" 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" { resource "oci_core_route_table_attachment" "default" {
subnet_id = oci_core_subnet.this.id # See https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_route_table_attachment
route_table_id = oci_core_route_table.this.id 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" { resource "oci_core_instance" "this" {
# https://registry.terraform.io/providers/oracle/oci/latest/docs/resources/core_instance
compartment_id = var.compartment_id compartment_id = var.compartment_id
availability_domain = var.availability_domain availability_domain = var.availability_domain
shape = var.shape shape = var.shape
create_vnic_details { create_vnic_details {
subnet_id = oci_core_subnet.this.id subnet_id = oci_core_subnet.default.id
} }
extended_metadata = {} extended_metadata = {}
@@ -67,7 +81,13 @@ resource "oci_core_instance" "this" {
source_details { source_details {
boot_volume_size_in_gbs = var.boot_volume_size_in_gbs 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 source_type = var.source_type
} }
lifecycle {
ignore_changes = [
source_details["source_id"]
]
}
} }

View File

@@ -13,11 +13,6 @@ variable "compartment_id" {
# Instance # Instance
#################### ####################
variable "shape" {
type = string
default = "VM.Standard.A1.Flex"
}
variable "memory_in_gbs" { variable "memory_in_gbs" {
type = number type = number
default = 24 default = 24
@@ -31,8 +26,21 @@ variable "boot_volume_size_in_gbs" {
type = number type = number
default = 50 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" { variable "source_id" {
type = string type = string
default = null
} }
variable "source_type" { variable "source_type" {
type = string type = string

View File

@@ -3,8 +3,8 @@
Oracle Cloud Infrastructure CLI. Oracle Cloud Infrastructure CLI.
1. [TL;DR](#tldr) 1. [TL;DR](#tldr)
2. [Configuration](#configuration) 1. [Configuration](#configuration)
3. [Further readings](#further-readings) 1. [Further readings](#further-readings)
## TL;DR ## TL;DR
@@ -37,8 +37,9 @@ oci iam availability-domain list -c 'tenancy_id'
# Output is paginated. # Output is paginated.
oci compute image list -c 'tenancy_id' --all oci compute image list -c 'tenancy_id' --all
oci compute image list -c 'tenancy_id' \ oci compute image list -c 'tenancy_id' \
--lifecycle-state 'AVAILABLE' --shape 'VM.Standard.A1.Flex' \
--operating-system 'Oracle Linux' --operating-system-version '8' \ --operating-system 'Oracle Linux' --operating-system-version '8' \
--lifecycle-state 'AVAILABLE' --sort-by 'DISPLAYNAME' --sort-order 'DESC'
# List available compute instance plugins. # List available compute instance plugins.
# Requires to be given the OS and its version. # Requires to be given the OS and its version.