# Data source to reference existing VCN in Korea region data "oci_core_vcns" "existing_vcns" { compartment_id = var.compartment_id } # Use the specific existing VCN by filtering locals { target_vcn = [ for vcn in data.oci_core_vcns.existing_vcns.virtual_networks : vcn.id if vcn.display_name == "vcn-20220508-0033" ][0] default_dhcp_options_id = [ for vcn in data.oci_core_vcns.existing_vcns.virtual_networks : vcn.default_dhcp_options_id if vcn.display_name == "vcn-20220508-0033" ][0] default_route_table_id = [ for vcn in data.oci_core_vcns.existing_vcns.virtual_networks : vcn.default_route_table_id if vcn.display_name == "vcn-20220508-0033" ][0] default_security_list_id = [ for vcn in data.oci_core_vcns.existing_vcns.virtual_networks : vcn.default_security_list_id if vcn.display_name == "vcn-20220508-0033" ][0] } # Find existing subnets in the target VCN data "oci_core_subnets" "existing_subnets" { compartment_id = var.compartment_id vcn_id = local.target_vcn } # Use an existing subnet or create a new one in the existing VCN resource "oci_core_subnet" "kr_subnet" { count = length(data.oci_core_subnets.existing_subnets.subnets) == 0 ? 1 : 0 # Only create if no subnets exist availability_domain = var.availability_domain cidr_block = "10.0.1.0/24" compartment_id = var.compartment_id display_name = "kr-subnet-tf" vcn_id = local.target_vcn dhcp_options_id = local.default_dhcp_options_id route_table_id = local.default_route_table_id security_list_ids = [local.default_security_list_id] dns_label = "krtfsub" } # Define which subnet to use based on whether existing subnets are found locals { subnet_to_use = length(data.oci_core_subnets.existing_subnets.subnets) > 0 ? data.oci_core_subnets.existing_subnets.subnets[0].id : oci_core_subnet.kr_subnet[0].id } # 实例 ch2 (将要终止的实例) - 仅在需要时创建用于模拟资源回收 resource "oci_core_instance" "ch2_instance" { count = var.ch2_enabled ? 1 : 0 availability_domain = var.availability_domain compartment_id = var.compartment_id display_name = "ch2-instance" shape = var.ch2_shape create_vnic_details { subnet_id = local.subnet_to_use assign_public_ip = true hostname_label = "ch2instance" } source_details { source_type = "image" source_id = var.image_id } metadata = { ssh_authorized_keys = var.ssh_public_key } timeouts { create = "60m" } } # 实例 ch3 (保持不变的实例) resource "oci_core_instance" "ch3_instance" { count = var.ch3_enabled ? 1 : 0 availability_domain = var.availability_domain compartment_id = var.compartment_id display_name = "ch3-instance" shape = var.ch3_shape create_vnic_details { subnet_id = local.subnet_to_use assign_public_ip = true hostname_label = "ch3instance" } source_details { source_type = "image" source_id = var.image_id } metadata = { ssh_authorized_keys = var.ssh_public_key } timeouts { create = "60m" } } # A1 实例 (主要实例) - ARM架构实例 resource "oci_core_instance" "a1_instance" { availability_domain = var.availability_domain compartment_id = var.compartment_id display_name = "a1-instance" shape = var.a1_shape create_vnic_details { subnet_id = local.subnet_to_use assign_public_ip = true hostname_label = "a1instance" } source_details { source_type = "image" source_id = var.image_id } metadata = { ssh_authorized_keys = var.ssh_public_key } timeouts { create = "60m" } # 添加对 ch2 实例的依赖,确保在 ch2 被删除后才创建 A1 depends_on = [oci_core_instance.ch2_instance] } # 为 A1 实例创建调整后的存储卷(代表从ch2释放的资源重新分配到A1,总共150GB) resource "oci_core_volume" "a1_resize_storage" { availability_domain = var.availability_domain compartment_id = var.compartment_id display_name = "a1-resize-storage" # 重命名以反映用途 size_in_gbs = var.a1_storage_size_gb # 应该设置为150GB,代表A1的新存储大小 # 确保ch2实例已禁用(模拟终止),以便我们可以重新分配其资源 lifecycle { precondition { condition = var.ch2_enabled == false error_message = "为了进行资源重新分配,请先设置 ch2_enabled = false 来模拟终止 ch2 实例." } } } # 将调整后的数据卷附加到 A1 实例 resource "oci_core_volume_attachment" "a1_resize_attachment" { count = var.ch2_enabled == false ? 1 : 0 # 仅在 ch2 被"终止"后附加 attachment_type = "paravirtualized" instance_id = oci_core_instance.a1_instance.id volume_id = oci_core_volume.a1_resize_storage.id } # 实例配置变量 variable "compartment_id" { description = "OCI Compartment OCID for Korea region" type = string } variable "availability_domain" { description = "Availability Domain for Korea region resources" type = string } variable "image_id" { description = "OCID of the OS image to use for instances in Korea" type = string } variable "ssh_public_key" { description = "SSH Public Key for instance access" type = string } variable "ch2_shape" { description = "Shape for ch2 instance" type = string default = "VM.Standard2.1" # 较小的实例 } variable "ch3_shape" { description = "Shape for ch3 instance" type = string default = "VM.Standard2.1" # 较小的实例 } variable "a1_shape" { description = "Shape for A1 instance" type = string default = "VM.Standard.A1.Flex" # ARM 架构实例 } variable "ch2_enabled" { description = "Whether to enable ch2 instance (when false, simulates termination for resource reallocation)" type = bool default = false # 默认不启用,以模拟已终止的状态 } variable "ch3_enabled" { description = "Whether to enable ch3 instance" type = bool default = true # 默认启用 } variable "a1_storage_size_gb" { description = "Storage size in GB for A1 instance" type = number default = 150 # 默认150GB,代表A1实例的新存储大小(原有100GB + 新增50GB) }