Database Support Blog

  • Oracle Cloud
2024.05.27

OCI CLIでクラウドの基礎的なネットワークリソースを作成する方法

OCI CLIは、Oracle Cloud Infrastructure(以下、OCI)の各種サービスを操作するためのコマンドラインツールです。このツールはオラクルからGitHub上で提供されていますが、皆さんは使ったことがあるでしょうか。

今回はそんな「OCI CLI」を利用する際のメリットと主要なネットワークリソースの作成方法をご紹介します。

1)CUI操作のメリットとは?

ここでは、OCIでコマンドラインインターフェース(CUI)を利用するメリットについてお伝えできればと思います。

CUI GUI
再現性 コマンドまたはシェルスクリプトを実行することで、作業者や作業時期が変わっても同一の手順で実行が可能 Webコンソール上の画面が変更されることがあるため、変更の度に手順の再作成が必要
ログ管理 ログを出力することで、容易な管理と改竄対策が可能 主にスクリーンショットを保存するため、データも大きく取得作業に時間がかかる
操作性 簡素な反面、訓練やノウハウが必要 直感的に作業が可能
作業時間 短い(事前に入力コマンド、パラメーターの準備ができる) 長い(準備したパラメーターシートを見ながら入力してデプロイする必要がある)
OCIで向いている作業 入力インプットが多いリソース(セキュリティリスト、ネットワーク・セキュリティ・グループ(NSG)など) 入力インプットが少ないリソース(VCN、サブネット、ゲートウェイなど)

上記がOCIにおけるGUIとCUIのメリット/デメリットの一例です。

この中で、筆者が個人的に最も大きなメリットと考えているのは「再現性」と「作業時間」です。

再現性については、OCI CLIを用いた作業では作業手順や作業ログを正確に記録でき、作業者や作業時期が変わっても全く同じ手順で作業を実施できます。

作業時間の短縮に関しては、例えば計画的な停止中のメンテナンス時など、作業時間が限られている状況でのメリットがあります。Webコンソールの画面からパラメーターを入力してデプロイや変更作業を行うよりも、事前に開発環境などで検証済みのコマンドや手順を利用することで、メンテナンス作業の時間を大幅に短縮できます。

さらに、コマンドをスクリプト化しておけば、複数のリソースを一括で作成や変更することができ、自動実行も可能になります。


2)OCI CLIを使用した仮想ネットワークリソースの作成

2-1)事前準備

前提条件(1)

OCI CLIを利用するには、インストールおよびAPIキーの登録などのセットアップが完了している必要があります。インストール手順については、オラクル社が公開している「Oracle Cloud Infrastructure ドキュメント - クイックスタート」に記載がありますので、本記事では詳細な説明を省略します。

また、Webコンソールから利用できる「Cloud Shell」というツールにはOCI CLIが標準でインストールされていますので、それを利用することも可能です。


前提条件(2)

本記事で扱うコマンドには「jq」コマンドを利用しています。jqコマンドはLinux、Mac、Windowsなど様々な環境で動作する、JSONデータを操作するためのコマンドラインツールです。このコマンドラインツールのセットアップも完了している必要があります。


事前情報取得

OCI CLIでは、OCIDを主に使用してリソースを識別します。そのため、作業を開始する前に、リソースを作成するコンパートメントのOCIDを取得しておく必要があります。

OCIDの確認には以下のコマンドを実行します。

コマンド構文
oci iam compartment list\
--compartment-id-in-subtree true\
--name "<コンパートメント名>"\
 | jq -r '.data[] | {"name", "id"}'

# 実行結果
xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci iam compartment list\
> --compartment-id-in-subtree true\
> --name "xxxxxxxxxx"\
> | jq -r '.data[] | {"name", "id"}'
{
"name": "xxxxxxxxxx",
"id": "ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

実行結果から、有効な所属コンパートメントのOCIDを確認できます。


2-2)VCNの作成

VCNに設定する主なパラメーターを整理したものが以下です。

項目名 必須 パラメーター 備考
compartment-id ocid1.compartment.oc1..xxxxx..... VCNの作成先となるコンパートメントのOCIDを指定
display-name - ociclitest VCN名を指定
dns-label - ociclitest1 DNSラベル名を指定
cidr-blocks - 10.0.0.0/16 VCN名のCIDRブロックを指定

※必須のパラメーター以外は任意で設定するパラメーターです。必要に応じて設定ください。
※これ以外にもパラメーターは存在します。詳細はリファレンスをご参照ください。


VCNの作成には以下のコマンドを実行します。

コマンド構文
oci network vcn create\
--compartment-id <所属するコンパートメントのOCID>\
--display-name "<VCNの名前>"\
--dns-label "<DNSラベル>"\
--cidr-blocks '["<CIDRブロック(2個以上の場合はカンマ区切りで記載)>"]'

# 実行結果
xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci network vcn create\
> --compartment-id ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --display-name "ociclitest"\
> --dns-label "ociclitest1"\
> --cidr-blocks '["10.0.0.0/16"]'
{
"data": {
"byoipv6-cidr-blocks": null,
"cidr-block": "10.0.0.0/16",
"cidr-blocks": [
"10.0.0.0/16"
],

"compartment-id": "ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"default-dhcp-options-id": "ocid1.dhcpoptions.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"default-route-table-id": "ocid1.routetable.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"default-security-list-id": "ocid1.securitylist.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"defined-tags": {
"Oracle-Tags": {
"CreatedBy": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"CreatedOn": "2024-03-22T02:26:51.894Z"
}
},
"display-name": "ociclitest",
"dns-label": "ociclitest1",

"freeform-tags": {},
"id": "ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"ipv6-cidr-blocks": null,
"ipv6-private-cidr-blocks": null,
"lifecycle-state": "AVAILABLE",
"time-created": "2024-03-22T02:26:51.986000+00:00",
"vcn-domain-name": "ociclitest1.oraclevcn.com"
},
"etag": "xxxxxxxx"
}

実行結果から、指定したパラメーターに従ってVCNが作成されたことを確認できます。

この段階では、引数として指定するパラメーターが少ないため、Webコンソールでの作成とコマンドラインでの操作との間に大きな違いは感じられないかもしれません。

また、ここで確認したVCNのOCIDは、後続の手順で使用するため、エディタにメモしておきます。


2-3)サブネットの作成

サブネットに設定する主なパラメーターを整理したものが以下です。

項目名 必須 パラメーター 備考
compartment-id ocid1.compartment.oc1..xxxxx..... サブネットの作成先となる所属するコンパートメントのOCIDを指定
vcn-id ocid1.vcn.oc1.ap-tokyo-1.xxxxx..... 所属するVCNのOCIDを指定
display-name - subnet_ocicli_test_public パブリックサブネットの名前を指定
dns-label - ociclitestpub DNSラベルを指定
cidr-block 10.0.1.0/24 CIDRブロックを指定
prohibit-public-ip-on-vnic - false パブリック/プライベートの指定

※必須のパラメーター以外は任意で設定するパラメーターです。必要に応じて設定ください。
※これ以外にもパラメーターは存在します。詳細はリファレンスをご参照ください。


“prohibit-public-ip-on-vnic”のパラメーターはデフォルト値がfalseのため、デフォルトではパブリックサブネットが作成されます。プライベートサブネットを作成する場合はこのパラメーターをtrueに変更します。

パブリックサブネットの作成には以下のコマンドを実行します。

コマンド構文
oci network subnet create\
--compartment-id <所属するコンパートメントのOCID>\
--vcn-id <所属するVCNのOCID>\
--display-name "<パブリックサブネットの名前>"\
--dns-label "<DNSラベル>"\
--cidr-block "<CIDRブロック>"\
--prohibit-public-ip-on-vnic false

# 実行結果
xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci network subnet create\
> --compartment-id ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --vcn-id ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --display-name "subnet_ocicli_test_public"\
> --dns-label "ociclitestpub"\
> --cidr-block "10.0.1.0/24"
> --prohibit-public-ip-on-vnic false
{
"data": {
"availability-domain": null,
"cidr-block": "10.0.1.0/24",
"compartment-id": "ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"defined-tags": {
"Oracle-Tags": {
"CreatedBy": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"CreatedOn": "2024-03-22T02:29:14.994Z"
}
},
"dhcp-options-id": "ocid1.dhcpoptions.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"display-name": "subnet_ocicli_test_public",
"dns-label": "ociclitestpub",

"freeform-tags": {},
"id": "ocid1.subnet.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"ipv6-cidr-block": null,
"ipv6-cidr-blocks": null,
"ipv6-virtual-router-ip": null,
"lifecycle-state": "PROVISIONING",
"prohibit-internet-ingress": false,
"prohibit-public-ip-on-vnic": false,

"route-table-id": "ocid1.routetable.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"security-list-ids": [ "ocid1.securitylist.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
],
"subnet-domain-name": "ociclitestpub.ociclitest1.oraclevcn.com",
"time-created": "2024-03-22T02:29:15.033000+00:00",
"vcn-id": "ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"virtual-router-ip": "10.0.1.1",
"virtual-router-mac": "00:00:17:07:58:54"
},
"etag": "xxxxxxxx"
}

実行結果から、指定したパラメーターに従ってパブリックサブネットが作成されたことがわかります。発行したコマンドでパラメーター通りにサブネットが作成されたことを改めて確認します。

また、作成直後は”lifecycle-state”の項目が”PROVISIONING”と表示され、作成中のステータスになっていました。ここでは、”lifecycle-state”が”AVAILABLE”になったことを確認します。

確認には以下のコマンドを実行します。

コマンド構文
oci network subnet list\
--compartment-id <所属するコンパートメントのOCID>

見やすく整形すると以下がわかりやすいでしょうか。(一例です)

oci network subnet list\
--compartment-id <所属するコンパートメントのOCID>\
--vcn-id <所属するVCNのOCID>\
--output table\
--query 'data[].{"1_Name":"display-name", "2_CIDR_Block":"cidr-block", "3_DNS_Label":"dns-label", "4_Lifecyclestate":"lifecycle-state", "5_Prohibit_public_ip_on_vnic":"prohibit-public-ip-on-vnic", "6_Created":"time-created"}'

※--output table オプションを使用すると結果を表形式で出力することができます。しかし、出力が横に長くなり折り返しによってかえって見づらくなる場合があります。そのような場合は、例のように--queryオプションを活用したり、awkコマンドやgrepコマンドを活用すると意図したレイアウトで確認できます。

# 実行結果
xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci network subnet list\
> --compartment-id ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --vcn-id ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --output table\
> --query 'data[].{"1_Name":"display-name", "2_CIDR_Block":"cidr-block", "3_DNS_Label":"dns-label", "4_Lifecyclestate":"lifecycle-state", "5_Prohibit_public_ip_on_vnic":"prohibit-public-ip-on-vnic", "6_Created":"time-created"}'
+------------------------------+--------------+----------------+------------------+------------------------------+----------------------------------+
| 1_Name | 2_CIDR_Block | 3_DNS_Label | 4_Lifecyclestate | 5_Prohibit_public_ip_on_vnic | 6_Created |
+------------------------------+--------------+----------------+------------------+------------------------------+----------------------------------+
| subnet_ocicli_test_private | 10.0.2.0/24 | ociclitestpri | AVAILABLE | True | 2024-03-22T02:34:40.306000+00:00 |
| subnet_ocicli_test_public | 10.0.1.0/24 | ociclitestpub | AVAILABLE | False | 2024-03-22T02:29:15.033000+00:00 |
+------------------------------+--------------+----------------+------------------+------------------------------+----------------------------------+

コマンドのオプションで指定したパラメーター通りにサブネットが作成されたことを確認できます。また、ステータスが”AVAILABLE”になっています。

さらに、プライベートの属性のサブネットは”prohibit-public-ip-on-vnic”の項目がTrueとなっています。

上記で作成したサブネットには、デフォルトのセキュリティリストやルート表がアタッチされています。


2-4)ゲートウェイの作成

インターネットゲートウェイ

初めにインターネットゲートウェイ(IGW)を作成します。インターネットゲートウェイを作成するには以下のコマンドを実行します。

コマンド構文
oci network internet-gateway create\
--compartment-id <所属するコンパートメントのOCID>\
--vcn-id <所属するVCNのOCID>\
--is-enabled true\
--display-name "<インターネットゲートウェイの名前>"

# 実行結果
xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci network internet-gateway create\
> --compartment-id ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --vcn-id ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --is-enabled true\
> --display-name "IGW_ocicli_test"
{
"data": {
"compartment-id": "ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"defined-tags": {
"Oracle-Tags": {
"CreatedBy": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"CreatedOn": "2024-03-22T02:36:35.450Z"
}
},
"display-name": "IGW_ocicli_test",
"freeform-tags": {},
"id": "ocid1.internetgateway.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"is-enabled": true,
"lifecycle-state": "AVAILABLE",

"route-table-id": null,
"time-created": "2024-03-22T02:36:35.488000+00:00",
"vcn-id": "ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
},
"etag": "xxxxxxxx"
}

実行結果から、指定したパラメーターに従ってインターネットゲートウェイが作成されたことがわかります。作成したゲートウェイについては後ほどまとめて確認します。


NATゲートウェイ

次に、NATゲートウェイ(NATGW)を作成します。NATゲートウェイを作成するには、以下のコマンドを実行します。

コマンド構文
oci network nat-gateway create\
--compartment-id <所属するコンパートメントのOCID>\
--vcn-id <所属するVCNのOCID>\
--display-name "<NATゲートウェイの名前>"

# 実行結果
xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci network nat-gateway create\
> --compartment-id ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --vcn-id ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --display-name "NATGW_ocicli_test"
{
"data": {
"block-traffic": false,
"compartment-id": "ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"defined-tags": {
"Oracle-Tags": {
"CreatedBy": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"CreatedOn": "2024-03-22T02:36:55.258Z"
}
},
"display-name": "NATGW_ocicli_test",
"freeform-tags": {},
"id": "ocid1.natgateway.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"lifecycle-state": "AVAILABLE",
"nat-ip": "150.230.212.236",

"public-ip-id": "ocid1.publicip.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"route-table-id": null,
"time-created": "2024-03-22T02:36:55.695000+00:00",
"vcn-id": "ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
},
"etag": "xxxxxxxx"
}

実行結果から、指定したパラメーターに従ってNATゲートウェイが作成されたことがわかります。

※NATゲートウェイに予約済みのPublicIPを割り当てる場合は別途オプションを指定する必要があります。
 詳細はリファレンスをご参照ください。


サービスゲートウェイ

次に、サービスゲートウェイ(SGW)を作成します。

サービスゲートウェイ作成時には必須パラメーターとしてオラクルサービスのOCIDを指定するため、サービスのOCIDを事前に確認します。サービスのOCIDの確認には以下のコマンドを実行します。

コマンド構文
oci network service list\
---all\
--output table

# 実行結果
xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci network service list\
> --all\
> --output table
+---------------------------------------------+---------------------------------------------+-------------------------------------------------------------------------------------------+---------------------------------------------+
| cidr-block | description | id | name |
+---------------------------------------------+---------------------------------------------+-------------------------------------------------------------------------------------------+---------------------------------------------+
| oci-nrt-objectstorage | OCI NRT Object Storage | ocid1.service.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | OCI NRT Object Storage |
| all-nrt-services-in-oracle-services-network | All NRT Services In Oracle Services Network | ocid1.service.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | All NRT Services In Oracle Services Network |
+---------------------------------------------+---------------------------------------------+-------------------------------------------------------------------------------------------+---------------------------------------------+

実行結果から、サービスのOCIDを確認できます。

サービスゲートウェイを作成するには以下のコマンドを実行します。

コマンド構文
oci network service-gateway create\
--compartment-id <所属するコンパートメントのOCID>\
--vcn-id <所属するVCNのOCID>\
--services '[{"service-id": "<対象サービスのOCID>"}]'\
--display-name "<サービスゲートウェイの名前>"

# 実行結果
xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci network service-gateway create\
> --compartment-id ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --vcn-id ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --services '[{"service-id": "ocid1.service.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"}]'\
> --display-name "SGW_ocicli_test"
{
"data": {
"block-traffic": false,
"compartment-id": "ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"defined-tags": {
"Oracle-Tags": {
"CreatedBy": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"CreatedOn": "2024-03-22T02:37:38.312Z"
}
},
"display-name": "SGW_ocicli_test",
"freeform-tags": {},
"id": "ocid1.servicegateway.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"lifecycle-state": "AVAILABLE",
"route-table-id": null,
"services": [
{
"service-id": "ocid1.service.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"service-name": "All NRT Services In Oracle Services Network"

}
],
"time-created": "2024-03-22T02:37:38.646000+00:00",
"vcn-id": "ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
},
"etag": "xxxxxxxx"
}

実行結果から、指定したパラメーターに従ってサービスゲートウェイが作成されたことがわかります。

最後に、各種ゲートウェイが作成されたことを確認します。

コマンド構文

・インターネットゲートウェイの確認
oci network internet-gateway list\
--compartment-id <所属するコンパートメントのOCID>\
--vcn-id <所属するVCNのOCID>\
--output table\
--query 'data[].{"1_Name":"display-name", "2_OCID":"id", "3_Lifecyclestate":"lifecycle-state", "4_Created":"time-created"}'

・NATゲートウェイの確認
oci network nat-gateway list\
--compartment-id <所属するコンパートメントのOCID>\
--vcn-id <所属するVCNのOCID>\
--output table\
--query 'data[].{"1_Name":"display-name", "2_OCID":"id", "3_Lifecyclestate":"lifecycle-state", "4_NAT_IPADDRESS":"nat-ip", "5_Created":"time-created"}'

・サービスゲートウェイの確認
oci network service-gateway list\
--compartment-id <所属するコンパートメントのOCID>\
--vcn-id <所属するVCNのOCID>\
--output table\
--query 'data[].{"1_Name":"display-name", "2_OCID":"id", "3_Lifecyclestate":"lifecycle-state", "4_Services":"services", "5_Created":"time-created"}'

## 実行結果
xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci network internet-gateway list\
> --compartment-id ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --vcn-id ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --output table\
> --query 'data[].{"1_Name":"display-name", "2_OCID":"id", "3_Lifecyclestate":"lifecycle-state", "4_Created":"time-created"}'
+-----------------+---------------------------------------------------------------------------------------------------+------------------+----------------------------------+
| 1_Name | 2_OCID | 3_Lifecyclestate | 4_Created |
+-----------------+---------------------------------------------------------------------------------------------------+------------------+----------------------------------+
| IGW_ocicli_test | ocid1.internetgateway.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | AVAILABLE | 2024-03-22T02:36:35.488000+00:00 |
+-----------------+---------------------------------------------------------------------------------------------------+------------------+----------------------------------+

xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci network nat-gateway list\
> --compartment-id ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --vcn-id ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --output table\
> --query 'data[].{"1_Name":"display-name", "2_OCID":"id", "3_Lifecyclestate":"lifecycle-state", "4_NAT_IPADDRESS":"nat-ip", "5_Created":"time-created"}'
+-------------------+----------------------------------------------------------------------------------------------+------------------+-----------------+----------------------------------+
| 1_Name | 2_OCID | 3_Lifecyclestate | 4_NAT_IPADDRESS | 5_Created |
+-------------------+----------------------------------------------------------------------------------------------+------------------+-----------------+----------------------------------+
| NATGW_ocicli_test | ocid1.natgateway.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | AVAILABLE | 150.230.212.236 | 2024-03-22T02:36:55.695000+00:00 |
+-------------------+----------------------------------------------------------------------------------------------+------------------+-----------------+----------------------------------+

xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci network service-gateway list\
> --compartment-id ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --vcn-id ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --output table\
> --query 'data[].{"1_Name":"display-name", "2_OCID":"id", "3_Lifecyclestate":"lifecycle-state", "4_Services":"services", "5_Created":"time-created"}'
+-----------------+--------------------------------------------------------------------------------------------------+------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------+
| 1_Name | 2_OCID | 3_Lifecyclestate | 4_Services | 5_Created |
+-----------------+--------------------------------------------------------------------------------------------------+------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------+
| SGW_ocicli_test | ocid1.servicegateway.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | AVAILABLE | [{'service-id': 'ocid1.service.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', 'service-name': 'All NRT Services In Oracle Services Network'}] | 2024-03-22T02:37:38.646000+00:00 |
+-----------------+--------------------------------------------------------------------------------------------------+------------------+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+----------------------------------+

各種ゲートウェイが作成されたことを確認できます。

また、ここで確認したゲートウェイのOCIDは後続の手順で使用するため、エディタにメモしておきます。

ゲートウェイもVCNと同様に引数として指定するパラメーターが少ない分、Webコンソールで作成するのとコマンドラインで実行する手間には大差がないように感じます。

※ローカルピアリングゲートウェイ(LPG)と動的ルーティングゲートウェイ(DRG)は設定項目が多いため、今後紹介予定です。


2-5)ルート表の作成

ルート表のルートルールへ設定する主なパラメーターを整理したものが以下です。

大項目名 小項目名 必須 パラメーター 備考
rt-id - ocid1.routetable.oc1.ap-tokyo-1.xxxxx..... ルート表のOCIDを指定
route-rules - - - ルートルールを指定
route-rules network-entity-id (〇) ocid1.internetgateway.oc1.ap-tokyo-1.xxxxx ターゲットのOCIDを指定
route-rules destination-type (-) CIDR_BLOCK ターゲットのタイプ(CIDR_BLOCK/SERVICE_CIDR_BLOCK)を指定
route-rules destination (-) 0.0.0.0/0 ターゲットの宛先(IGW/NATGWの場合はCIDRブロック、SGWの場合は対応するサービス)を指定
route-rules description (-) ocicli_test_1 ルートルールの説明

※必須のパラメーター以外は任意で設定するパラメーターです。必要に応じて設定ください。
※これ以外にもパラメーターは存在します。詳細はリファレンスをご参照ください。


ルート表を作成するには以下のコマンドを実行します。

コマンド構文
oci network route-table create\
--compartment-id <所属するコンパートメントのOCID>\
--vcn-id <所属するVCNのOCID>\
--display-name "<ルート表の名前>"\
--route-rules '[]'

# 実行結果
xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci network route-table create\
> --compartment-id ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --vcn-id ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --display-name "RT_ocicli_test_1"\
> --route-rules '[ { "network-entity-id": "ocid1.natgateway.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "destination-type": "CIDR_BLOCK", "destination": "0.0.0.0/0", "description": "ocicli_test_1" }, { "network-entity-id": "ocid1.servicegateway.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx", "destination-type": "SERVICE_CIDR_BLOCK", "destination": "oci-nrt-objectstorage", "description": "ocicli_test_2" } ]'
{
"data": {
"compartment-id": "ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"defined-tags": {
"Oracle-Tags": {
"CreatedBy": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"CreatedOn": "22024-03-22T02:42:48.895Z"
}
},
"display-name": "RT_ocicli_test_1",
"freeform-tags": {},
"id": "ocid1.routetable.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"lifecycle-state": "AVAILABLE",
"route-rules": [
{
"cidr-block": null,
"description": "ocicli_test_1",
"destination": "0.0.0.0/0",
"destination-type": "CIDR_BLOCK",
"network-entity-id": "ocid1.natgateway.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"route-type": "STATIC"
},
{
"cidr-block": null,
"description": "ocicli_test_2",
"destination": "oci-nrt-objectstorage",
"destination-type": "SERVICE_CIDR_BLOCK",
"network-entity-id": "ocid1.servicegateway.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"route-type": "STATIC"
}
],

"time-created": "2024-03-22T02:42:48.895000+00:00",
"vcn-id": "ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
},
"etag": "xxxxxxxx"
}

発行したコマンドで実際にルート表とルートルールが作成されたことを確認します。

コマンド構文
oci network route-table get\
--rt-id <ルート表のOCID> | \
jq '.data | {"Name" : ."display-name", "OCID" : ."id", "Lifecyclestate" : ."lifecycle-state", "Created" : ."time-created", "route-rules"}'

# 実行結果
xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci network route-table get\
> --rt-id ocid1.routetable.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx | \
> jq '.data | {"Name" : ."display-name", "OCID" : ."id", "Lifecyclestate" : ."lifecycle-state", "Created" : ."time-created", "route-rules"}'
{
"Name": "RT_ocicli_test_1",
"OCID": "ocid1.routetable.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"Lifecyclestate": "AVAILABLE",
"Created": "2024-03-22T02:42:48.895000+00:00",
"route-rules": [
{
"cidr-block": null,
"description": "ocicli_test_1",
"destination": "0.0.0.0/0",
"destination-type": "CIDR_BLOCK",
"network-entity-id": "ocid1.internetgateway.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"route-type": "STATIC"
},
{
"cidr-block": null,
"description": "ocicli_test_2",
"destination": "oci-nrt-objectstorage",
"destination-type": "SERVICE_CIDR_BLOCK",
"network-entity-id": "ocid1.servicegateway.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"route-type": "STATIC"
}
]
}

ルート表とルートルールが作成されたことを確認できます。


2-6)セキュリティリストの作成

セキュリティリストのセキュリティルールに設定する主なパラメーターを整理したものが以下です。

セキュリティルールには、一般的に設定される機会の多いプロトコルのパラメーターをサンプルとして記載しています。

イングレスルール

大項目名 小項目名 必須 ルール1 ルール2 ルール3 ルール4 ルール5 備考
isStateless - false false false false false false:ステートフル
true:ステートレス
protocol 6 17 1 1 1 プロトコルに対応した番号を指定
source 0.0.0.0/0 0.0.0.0/0 0.0.0.0/0 0.0.0.0/0 0.0.0.0/0 送信元のIPアドレスを指定
icmpOptions - null null null プロトコルがICMPの場合に指定
icmpOptions code (-) - - - 4 null コードを指定
icmpOptions type (〇) - - - 3 8 タイプを指定(icmpoptionsを指定する場合は必須)
tcpOptions - null null null null プロトコルがTCPの場合に指定
tcpOptions destinationPortRange (-) min:22
max:22
- - - - 宛先のポート番号の範囲を指定
tcpOptions sourcePortRange (-) null - - - - 送信元のポート番号の範囲を指定
udpOptions - null null null null プロトコルがUDPの場合に指定
udpOptions destinationPortRange (-) - min:80
max:80
- - - 宛先のポート番号の範囲を指定
udpOptions sourcePortRange (-) - null - - - 送信元のポート番号の範囲を指定
description - OCI CLIを使ったTCPルールの追加テスト OCI CLIを使ったUDPルールの追加テスト OCI CLIを使ったICMPルールの追加テスト OCI CLIを使ったICMPルールの追加テスト OCI CLIを使ったICMPルールの追加テスト 説明欄

エグレスルール

大項目名 小項目名 必須 ルール1 ルール2 ルール3 備考
isStateless - false false false false:ステートフル
true:ステートレス
protocol 6 17 1 プロトコルに対応した番号を指定
destination 0.0.0.0/0 0.0.0.0/0 0.0.0.0/0 宛先のIPアドレスを指定
icmpOptions - null null プロトコルがICMPの場合に指定
icmpOptions code (-) - - 4 コードを指定
icmpOptions type (〇) - - 3 タイプを指定(icmpoptionsを指定する場合は必須)
tcpOptions - null null プロトコルがTCPの場合に指定
tcpOptions destinationPortRange (-) min:22
max:22
- - 宛先のポート番号の範囲を指定
tcpOptions sourcePortRange (-) null - - 送信元のポート番号の範囲を指定
udpOptions - null null プロトコルがUDPの場合に指定
udpOptions destinationPortRange (-) - min:80
max:80
- 宛先のポート番号の範囲を指定
udpOptions sourcePortRange (-) - null - 送信元のポート番号の範囲を指定
description - OCI CLIを使ったTCPルールの追加テスト OCI CLIを使ったUDPルールの追加テスト OCI CLIを使ったICMPルールの追加テスト 説明欄

セキュリティリストを作成するには、以下のコマンドを実行します。

コマンド構文
oci network security-list create\
--compartment-id <所属コンパートメントのOCID>\
--vcn-id <VCNのOCID>\
--display-name "<セキュリティリスト名>"\
--ingress-security-rules '["<セキュリティルール>"]'\
--egress-security-rules '["<セキュリティルール>"]'

# 実行結果
xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci network security-list create\
> --compartment-id ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --vcn-id ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --display-name "SL_test_1"\
> --ingress-security-rules\
> '[
> {"isStateless": false, "protocol": "6" , "source": "0.0.0.0/0", "icmpOptions": null, "tcpOptions": { "destinationPortRange": { "max": 22, "min": 22 }, "sourcePortRange": null }, "udpOptions": null, "description": "OCI CLIを使ったTCPルールの追加テスト" }
> ,{"isStateless": false, "protocol": "17", "source": "0.0.0.0/0", "icmpOptions": null, "tcpOptions": null, "udpOptions": { "destinationPortRange": { "max": 80, "min": 80 }, "sourcePortRange": null }, "description": "OCI CLIを使ったUDPルールの追加テスト" }
> ,{"isStateless": false, "protocol": "1", "source": "0.0.0.0/0", "icmpOptions": null, "tcpOptions": null, "udpOptions": null, "description": "OCI CLIを使ったICMPルールの追加テスト" }
> ,{"isStateless": false, "protocol": "1", "source": "0.0.0.0/0", "icmpOptions": { "code": 4, "type": 3 }, "tcpOptions": null, "udpOptions": null, "description": "OCI CLIを使ったICMPルールの追加テスト" }
> ,{"isStateless": false, "protocol": "1", "source": "0.0.0.0/0", "icmpOptions": { "code": null, "type": 8 }, "tcpOptions": null, "udpOptions": null, "description": "OCI CLIを使ったICMPルールの追加テスト" }
> ]'\
> --egress-security-rules\
> '[
> { "isStateless": false, "protocol": "6", "destination": "0.0.0.0/0", "icmpOptions": null, "tcpOptions": { "destinationPortRange": { "max": 22, "min": 22 }, "sourcePortRange": null }, "udpOptions": null, "description": "OCI CLIを使ったTCPルールの追加テスト" }
> ,{ "isStateless": false, "protocol": "17", "destination": "0.0.0.0/0", "icmpOptions": null, "tcpOptions": null, "udpOptions": { "destinationPortRange": { "max": 80, "min": 80 }, "sourcePortRange": null }, "description": "OCI CLIを使ったUDPルールの追加テスト" }
> ,{ "isStateless": false, "protocol": "1", "destination": "0.0.0.0/0", "icmpOptions": { "code": 4, "type": 3 }, "tcpOptions": null, "udpOptions": null, "description": "OCI CLIを使ったICMPルールの追加テスト" }
> ]'
{
"data": {
"compartment-id": "ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"defined-tags": {
"Oracle-Tags": {
"CreatedBy": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"CreatedOn": "2024-03-28T07:15:46.614Z"
}
},
"display-name": "SL_test_1",
"egress-security-rules": [
{
"description": "OCI CLI\u3092\u4f7f\u3063\u305fTCP\u30eb\u30fc\u30eb\u306e\u8ffd\u52a0\u30c6\u30b9\u30c8",
"destination": "0.0.0.0/0",
"destination-type": "CIDR_BLOCK",
"icmp-options": null,
"is-stateless": false,
"protocol": "6",
"tcp-options": {
"destination-port-range": {
"max": 22,
"min": 22
},
"source-port-range": null
},
"udp-options": null
},
{
"description": "OCI CLI\u3092\u4f7f\u3063\u305fUDP\u30eb\u30fc\u30eb\u306e\u8ffd\u52a0\u30c6\u30b9\u30c8",
"destination": "0.0.0.0/0",
~~~~~ レスポンスが長いため一部省略 ~~~~~
"source-port-range": null
}
},
{
"description": "OCI CLI\u3092\u4f7f\u3063\u305fICMP\u30eb\u30fc\u30eb\u306e\u8ffd\u52a0\u30c6\u30b9\u30c8",
"destination": "0.0.0.0/0",
"destination-type": "CIDR_BLOCK",
"icmp-options": {
"code": 4,
"type": 3
},
"is-stateless": false,
"protocol": "1",
"tcp-options": null,
"udp-options": null
}
],

"freeform-tags": {},
"id": "ocid1.securitylist.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"ingress-security-rules": [
{
"description": "OCI CLI\u3092\u4f7f\u3063\u305fTCP\u30eb\u30fc\u30eb\u306e\u8ffd\u52a0\u30c6\u30b9\u30c8",
"icmp-options": null,
"is-stateless": false,
"protocol": "6",
"source": "0.0.0.0/0",
"source-type": "CIDR_BLOCK",
"tcp-options": {
"destination-port-range": {
"max": 22,
"min": 22
},
"source-port-range": null
},
"udp-options": null
},
{
"description": "OCI CLI\u3092\u4f7f\u3063\u305fUDP\u30eb\u30fc\u30eb\u306e\u8ffd\u52a0\u30c6\u30b9\u30c8",
"icmp-options": null,
~~~~~ レスポンスが長いため一部省略 ~~~~~
"source": "0.0.0.0/0",
"source-type": "CIDR_BLOCK",
"tcp-options": null,
"udp-options": null
},
{
"description": "OCI CLI\u3092\u4f7f\u3063\u305fICMP\u30eb\u30fc\u30eb\u306e\u8ffd\u52a0\u30c6\u30b9\u30c8",
"icmp-options": {
"code": null,
"type": 8
},
"is-stateless": false,
"protocol": "1",
"source": "0.0.0.0/0",
"source-type": "CIDR_BLOCK",
"tcp-options": null,
"udp-options": null
}
],
"lifecycle-state": "AVAILABLE",
"time-created": "2024-03-28T07:15:46.648000+00:00",
"vcn-id": "ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
},

"etag": "xxxxxxxx"
}

実行結果より、セキュリティリストが作成されたことがわかります。

構成時のレスポンスではパラメーター通りにルールが構成されていることの確認が困難なため、別途ルールを確認します。また、ここで確認したセキュリティリストのOCIDは後続の手順で使用するため、エディタにメモしておきます。


セキュリティルールを確認するには以下のコマンドを実行します。

コマンド構文

・エグレスルールの確認
oci network security-list get\
--security-list-id <セキュリティリストのOCID>\
--output table\
--query 'data."egress-security-rules"[].{"01_Stateless":"is-stateless", "02_Destination-type":"destination-type", "03_Destination":"destination", "04_Protocol":"protocol", "05_ICMP":"icmp-options", "06_TCP":"tcp-options", "07_UDP":"udp-options", "Description":"description"}'

・イングレスルールの確認
oci network security-list get\
--security-list-id <セキュリティリストのOCID>\
--output table\
--query 'data."ingress-security-rules"[].{"01_Stateless":"is-stateless", "02_Source-type":"source-type", "03_Source":"source", "04_Protocol":"protocol", "05_ICMP":"icmp-options", "06_TCP":"tcp-options", "07_UDP":"udp-options", "Description":"description"}'

# 実行結果
xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci network security-list get\
> --security-list-id ocid1.securitylist.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --output table\
> --query 'data."egress-security-rules"[].{"01_Stateless":"is-stateless", "02_Destination-type":"destination-type", "03_Destination":"destination", "04_Protocol":"protocol", "05_ICMP":"icmp-options", "06_TCP":"tcp-options", "07_UDP":"udp-options", "Description":"description"}'
+--------------+---------------------+----------------+-------------+------------------------+-------------------------------------------------------------------------------+-------------------------------------------------------------------------------+---------------------------------------+
| 01_Stateless | 02_Destination-type | 03_Destination | 04_Protocol | 05_ICMP | 06_TCP | 07_UDP | Description |
+--------------+---------------------+----------------+-------------+------------------------+-------------------------------------------------------------------------------+-------------------------------------------------------------------------------+---------------------------------------+
| False | CIDR_BLOCK | 0.0.0.0/0 | 6 | None | {'destination-port-range': {'max': 22, 'min': 22}, 'source-port-range': None} | None | OCI CLIを使ったTCPルールの追加テスト |
| False | CIDR_BLOCK | 0.0.0.0/0 | 17 | None | None | {'destination-port-range': {'max': 80, 'min': 80}, 'source-port-range': None} | OCI CLIを使ったUDPルールの追加テスト |
| False | CIDR_BLOCK | 0.0.0.0/0 | 1 | {'code': 4, 'type': 3} | None | None | OCI CLIを使ったICMPルールの追加テスト |

+--------------+---------------------+----------------+-------------+------------------------+-------------------------------------------------------------------------------+-------------------------------------------------------------------------------+---------------------------------------+
etag: xxxxxxxx

xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci network security-list get\
> --security-list-id ocid1.securitylist.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --output table\
> --query 'data."ingress-security-rules"[].{"01_Stateless":"is-stateless", "02_Source-type":"source-type", "03_Source":"source", "04_Protocol":"protocol", "05_ICMP":"icmp-options", "06_TCP":"tcp-options", "07_UDP":"udp-options", "Description":"description"}'
+--------------+----------------+-----------+-------------+---------------------------+-------------------------------------------------------------------------------+-------------------------------------------------------------------------------+---------------------------------------+
| 01_Stateless | 02_Source-type | 03_Source | 04_Protocol | 05_ICMP | 06_TCP | 07_UDP | Description |
+--------------+----------------+-----------+-------------+---------------------------+-------------------------------------------------------------------------------+-------------------------------------------------------------------------------+---------------------------------------+
| False | CIDR_BLOCK | 0.0.0.0/0 | 6 | None | {'destination-port-range': {'max': 22, 'min': 22}, 'source-port-range': None} | None | OCI CLIを使ったTCPルールの追加テスト |
| False | CIDR_BLOCK | 0.0.0.0/0 | 17 | None | None | {'destination-port-range': {'max': 80, 'min': 80}, 'source-port-range': None} | OCI CLIを使ったUDPルールの追加テスト |
| False | CIDR_BLOCK | 0.0.0.0/0 | 1 | None | None | None | OCI CLIを使ったICMPルールの追加テスト |
| False | CIDR_BLOCK | 0.0.0.0/0 | 1 | {'code': 4, 'type': 3} | None | None | OCI CLIを使ったICMPルールの追加テスト |
| False | CIDR_BLOCK | 0.0.0.0/0 | 1 | {'code': None, 'type': 8} | None | None | OCI CLIを使ったICMPルールの追加テスト |

+--------------+----------------+-----------+-------------+---------------------------+-------------------------------------------------------------------------------+-------------------------------------------------------------------------------+---------------------------------------+
etag: xxxxxxxx

実行結果から、パラメーター通りにエグレスとイングレスルールが構成されたことを確認できます。


2-7)ネットワーク・セキュリティ・グループの作成

ネットワーク・セキュリティ・グループ(NSG)を作成するには以下のコマンドを実行します。

コマンド構文
oci network nsg create\
--compartment-id <所属コンパートメントのOCID>\
--vcn-id <VCNのOCID>\
--display-name "<ネットワーク・セキュリティ・グループ名>"

# 実行結果
xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci network nsg create\
> --compartment-id ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --vcn-id ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --display-name "NSG_test_1"
{
"data": {
"compartment-id": "ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"defined-tags": {
"Oracle-Tags": {
"CreatedBy": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"CreatedOn": "2024-03-28T07:27:24.222Z"
}
},
"display-name": "NSG_test_1",
"freeform-tags": {},
"id": "ocid1.networksecuritygroup.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx",
"lifecycle-state": "AVAILABLE",
"time-created": "2024-03-28T07:27:24.316000+00:00",
"vcn-id": "ocid1.vcn.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
},
"etag": "xxxxxxxx"
}

実行結果より、ネットワーク・セキュリティ・グループが作成されたことを確認できます。

また、ここで確認したネットワーク・セキュリティ・グループのOCIDは後続の手順で使用するため、エディタにメモしておきます。


ネットワーク・セキュリティ・グループは作成時にルールを構成することはできないため、別途セキュリティルールを設定します。セキュリティルールには、一般的に設定される機会が多いプロトコルのパラメーターをサンプルとして記載しています。

ネットワーク・セキュリティ・グループのセキュリティルールに設定する主なパラメーターを整理したものが以下です。

大項目名 小項目名 必須 ルール1 ルール2 ルール3 ルール4 ルール5 備考
isStateless - false false false false false false:ステートフル
true:ステートレス
protocol 17 6 17 6 1 プロトコルに対応した番号を指定
source-type null null CIDR_BLOCK CIDR_BLOCK CIDR_BLOCK イングレスルールの場合に送信元のタイプを指定
source - null null 0.0.0.0/0 0.0.0.0/0 0.0.0.0/0 イングレスルールの場合に送信元のCIDR/サービスを指定
destination-type CIDR_BLOCK CIDR_BLOCK null null null エグレスルールの場合に宛先のタイプを指定
destination - 0.0.0.0/0 0.0.0.0/0 null null null エグレスルールの場合に宛先のCIDR/サービスを指定
icmpOptions - null null null null プロトコルがICMPの場合に指定
icmpOptions code (-) - - - - 4 コードを指定
icmpOptions type (〇) - - - - 3 タイプを指定(icmpoptionsを指定する場合は必須)
tcpOptions - null null null プロトコルがTCPの場合に指定
tcpOptions destinationPortRange (-) - min:22
max:22
- min:22
max:22
- 宛先のポート番号の範囲を指定
tcpOptions sourcePortRange (-) - null - null - 送信元のポート番号の範囲を指定
udpOptions - null null null プロトコルがUDPの場合に指定
udpOptions destinationPortRange (-) min:80
max:80
- min:80
max:80
- - 宛先のポート番号の範囲を指定
udpOptions sourcePortRange (-) null - null - - 送信元のポート番号の範囲を指定
description - - - - - - 説明欄

セキュリティルールを設定するには、以下のコマンドを実行します。

コマンド構文
oci network nsg rules add\
--nsg-id <ネットワーク・セキュリティ・グループのOCID>\
--security-rules '["<セキュリティルール>"]'

# 実行結果
xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci network nsg rules add\
> --nsg-id ocid1.networksecuritygroup.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --security-rules \
> '[
> { "is-stateless": false, "direction": "EGRESS", "source-type": null, "destination-type": "CIDR_BLOCK", "source": null, "destination": "0.0.0.0/0", "protocol": "17", "icmp-options": null, "tcp-options": null, "udp-options": { "destination-port-range": { "max": 80, "min": 80 }, "source-port-range": null }, "description": null },
> { "is-stateless": false, "direction": "EGRESS", "source-type": null, "destination-type": "CIDR_BLOCK", "source": null, "destination": "0.0.0.0/0", "protocol": "6", "icmp-options": null, "tcp-options": { "destination-port-range": { "max": 22, "min": 22 }, "source-port-range": null }, "udp-options": null, "description": null },
> { "is-stateless": false, "direction": "INGRESS", "source-type": "CIDR_BLOCK", "destination-type": null, "source": "0.0.0.0/0", "destination": null, "protocol": "17", "icmp-options": null, "tcp-options": null, "udp-options": { "destination-port-range": { "max": 80, "min": 80 }, "source-port-range": null }, "description": null },
> { "is-stateless": false, "direction": "INGRESS", "source-type": "CIDR_BLOCK", "destination-type": null, "source": "0.0.0.0/0", "destination": null, "protocol": "6", "icmp-options": null, "tcp-options": { "destination-port-range": { "max": 22, "min": 22 }, "source-port-range": null }, "udp-options": null, "description": null },
> { "is-stateless": false, "direction": "INGRESS", "source-type": "CIDR_BLOCK", "destination-type": null, "source": "0.0.0.0/0", "destination": null, "protocol": "1", "icmp-options": { "code": 4, "type": 3 }, "tcp-options": null, "udp-options": null, "description": null }
> ]'
{
"data": {
"security-rules": [
{
"description": null,
"destination": null,
"destination-type": null,
"direction": "INGRESS",
"icmp-options": null,
"id": "6B87E1",
"is-stateless": false,
"is-valid": true,
"protocol": "6",
"source": "0.0.0.0/0",
"source-type": "CIDR_BLOCK",
"tcp-options": {
"destination-port-range": {
"max": 22,
"min": 22
},
"source-port-range": null
},
"time-created": "2024-04-25T08:16:24.206000+00:00",
"udp-options": null
},
~~~~~ レスポンスが長いため一部省略 ~~~~~
{
"description": null,
"destination": null,
"destination-type": null,
"direction": "INGRESS",
"icmp-options": null,
"id": "95131A",
"is-stateless": false,
"is-valid": true,
"protocol": "17",
"source": "0.0.0.0/0",
"source-type": "CIDR_BLOCK",
"tcp-options": null,
"time-created": "2024-04-25T08:16:24.205000+00:00",
"udp-options": {
"destination-port-range": {
"max": 80,
"min": 80
},
"source-port-range": null
}
}
]

}
}

実行結果から、セキュリティルールが構成されたことを確認できます。

セキュリティリスト同様に、構成時のレスポンスではパラメーター通りにルールが構成されていることの確認が困難なため、別途ルールを確認します。

セキュリティルールの確認には以下のコマンドを実行します。

コマンド構文
oci network nsg rules list\
--nsg-id <ネットワーク・セキュリティ・グループのOCID>\
--output table\
--query 'data[].{"01_Rule_ID":"id", "02_stateless":"is-stateless" "03_Direction":"direction" "04_Egress_type":"destination-type" "05_Destination":"destination" "06_Ingress_type":"source-type" "07_Source":"source" "08_Protocol":"protocol" "09_ICMP":"icmp-options" "10_TCP":"tcp-options" "11_UDP":"udp-options"}'

# 実行結果
xxxxxxxxxx@cloudshell:~ (ap-tokyo-1)$ oci network nsg rules list\
> --nsg-id ocid1.networksecuritygroup.oc1.ap-tokyo-1.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\
> --output table\
> --query 'data[].{"01_Rule_ID":"id", "02_stateless":"is-stateless" "03_Direction":"direction" "04_Egress_type":"destination-type" "05_Destination":"destination" "06_Ingress_type":"source-type" "07_Source":"source" "08_Protocol":"protocol" "09_ICMP":"icmp-options" "10_TCP":"tcp-options" "11_UDP":"udp-options"}'
+------------+--------------+--------------+----------------+----------------+-----------------+-----------+-------------+------------------------+-------------------------------------------------------------------------------+-------------------------------------------------------------------------------+
| 01_Rule_ID | 02_stateless | 03_Direction | 04_Egress_type | 05_Destination | 06_Ingress_type | 07_Source | 08_Protocol | 09_ICMP | 10_TCP | 11_UDP |
+------------+--------------+--------------+----------------+----------------+-----------------+-----------+-------------+------------------------+-------------------------------------------------------------------------------+-------------------------------------------------------------------------------+
| 73861A | False | EGRESS | CIDR_BLOCK | 0.0.0.0/0 | None | None | 17 | None | None | {'destination-port-range': {'max': 80, 'min': 80}, 'source-port-range': None} |
| 9D2CBF | False | EGRESS | CIDR_BLOCK | 0.0.0.0/0 | None | None | 6 | None | {'destination-port-range': {'max': 22, 'min': 22}, 'source-port-range': None} | None |
| 95131A | False | INGRESS | None | None | CIDR_BLOCK | 0.0.0.0/0 | 17 | None | None | {'destination-port-range': {'max': 80, 'min': 80}, 'source-port-range': None} |
| 6B87E1 | False | INGRESS | None | None | CIDR_BLOCK | 0.0.0.0/0 | 6 | None | {'destination-port-range': {'max': 22, 'min': 22}, 'source-port-range': None} | None |
| D85AB2 | False | INGRESS | None | None | CIDR_BLOCK | 0.0.0.0/0 | 1 | {'code': 4, 'type': 3} | None | None |

+------------+--------------+--------------+----------------+----------------+-----------------+-----------+-------------+------------------------+-------------------------------------------------------------------------------+-------------------------------------------------------------------------------+

実行結果から、パラメーター通りにセキュリティルールが構成されたことを確認できます。

ルート表やセキュリティリスト、ネットワーク・セキュリティ・グループの作成は指定するパラメーターが多く、コマンドが少し煩雑に見えます。

しかし、GUIに比べて画面遷移がなく、ワンライナーでルート表とルールの作成まで行える点は、CUIの利点が活かされているように感じます。


3)あとがき

今回は主要なネットワークリソースの作成についてご紹介しました。GUIとの操作感も含めてご紹介いたしましたが、いかがでしょうか。

OCI CLIやCUI操作のメリットが見えてきたと思いますが、まだまだGUIの手軽かつ直感的な操作性の方が使いやすさを感じられる方も多いかもしれません。今後は、コマンドラインらしいスタイリッシュなリソース作成方法なども紹介する予定です。


冒頭でも述べたように、OCI CLIはWebコンソール上のCloudShellにもインストールされていますので、まずはぜひOCI CLIの使用を試してみていただけたらと思います。


執筆者情報

ふかやけんじ プロフィール画像

2024年に中途入社。前職では主にOracleのオンプレミス環境の設計構築から運用保守と幅広い業務を経験。現在はOCIのポストセールス業務を中心に担当。趣味はパフェ巡り。


■本記事の内容について
 本記事に示した定義及び条件は変更される場合があります。あらかじめご了承ください。

■商標に関して
 ・Oracle®、Java、MySQL及びNetSuiteは、Oracle、その子会社及び関連会社の米国及びその他の国における登録商標です。
 ・Amazon Web Services、AWS、Powered by AWS ロゴ、[およびかかる資料で使用されるその他の AWS 商標] は、Amazon.com, Inc. またはその関連会社の商標です。
  文中の社名、商品名等は各社の商標または登録商標である場合があります。

関連している記事

  • Oracle Cloud
  • Oracle Database
2024.07.19

Oracle Cloud Shellで簡単にOCIのComputeへシリアルコンソール接続する方法!

Oracle Cloudで構築したComputeインスタンスは、ハードウェア等インフラ周りはオラクル社が管理しますが、OSやアプリケーションはお客様が管理する必要があります。今回は、事前準備不要で簡単に操作可能なCloud Shellによるコンソール接続をご紹介します。

  • Oracle Cloud
2024.07.19

【Oracle Cloud】Oracle Base Database Serviceにシリアル・コンソール接続する方法

これまでComputeインスタンス停止の自動化はOCI CLIやcronやタスクスケジューラ、ジョブ管理ツールからのCLI実行が必要でした。自動スケーリング機能がリリースされたことで任意の日時でComputeインスタンスの起動/停止が可能となりました。課金抑制にも繋がる詳細手順をご紹介します。

  • Oracle Cloud
2024.07.18

【Oracle Cloud】opcユーザー以外のSSH接続を許可する方法

Oracle Cloud上にComputeインスタンスやDBシステムを構築すると、システム管理者ユーザであるopcユーザが作成されますが、お客様の中にはopcユーザ以外からSSH接続を必要とするケースもあるかと思います。今回は、opcユーザ以外のユーザからSSH接続を行う方法をご紹介します。

ページの先頭へ戻る