创建 VCN 模块

在 Terraform 配置中将虚拟云网络(VCN)和网关定义为资源,并声明在模块中使用的变量。

完成 vcn 子目录中的以下步骤:
  1. 创建一个名为 variables.tf 的文本文件,然后将以下代码粘贴到文件中。
    此代码声明此模块中使用的变量。
    variable "tenancy_ocid" {}
    variable "compartment_ocid" {}
    variable "app_tag" {}
    variable "environment" {}
    variable "vcn_cidr" {}
  2. 创建一个名为 vcn.tf 的文本文件,然后将以下代码粘贴到文件中。
    此代码指定 VCN 的参数、Internet 网关和 NAT 网关。
    resource "oci_core_virtual_network" "base_vcn" {
      cidr_block     = "${var.vcn_cidr}"
      compartment_id = "${var.compartment_ocid}"
      display_name   = "${var.app_tag}_${var.environment}_vcn"
      dns_label      = "${lower(format("%s", var.app_tag))}"
    }
    
    resource "oci_core_internet_gateway" "base_ig" {
      compartment_id = "${var.compartment_ocid}"
      display_name   = "${var.app_tag}_${var.environment}_internetgateway"
      vcn_id         = "${oci_core_virtual_network.base_vcn.id}"
    }
    
    resource "oci_core_nat_gateway" "nat_gateway" {
      compartment_id = "${var.compartment_ocid}"
      vcn_id         = "${oci_core_virtual_network.base_vcn.id}"
      display_name   = "${var.app_tag}_${var.environment}_nat_gateway"
    }

    此 Terraform 配置示例不指定任何子网、安全列表和其他网络资源。可以根据需要定制此配置。

  3. 创建一个名为 vcn_output.tf 的文本文件,然后将以下代码粘贴到文件中。
    此代码会导致 Terraform 在创建之后显示资源 ID。
    output "vcnid" {
      value = "${oci_core_virtual_network.base_vcn.id}"
    }
    
    output "default_dhcp_id" {
      value = "${oci_core_virtual_network.base_vcn.default_dhcp_options_id}"
    }
    
    output "internet_gateway_id" {
      value = "${oci_core_internet_gateway.base_ig.id}"
    }
    
    output "nat_gateway_id" {
      value = "${oci_core_nat_gateway.nat_gateway.id}"
    }