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、インターネット・ゲートウェイおよび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}"
    }