2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

OCI Resource Manager で Terraform形式で記述したリソースをプロビジョニングしてみた。(Oracle Cloud Infrastructure)

Last updated at Posted at 2023-10-25

OCI の Resource Manager は Terraform の Managed Service となります。

リソース・マネージャの概要
https://docs.oracle.com/ja-jp/iaas/Content/ResourceManager/Concepts/resourcemanager.htm
リソース・マネージャは、Oracle Cloud Infrastructureリソースのプロビジョニング処理を自動化できるOracle Cloud Infrastructureサービスです。Terraformを使用する場合、リソース・マネージャでは、「infrastructure-as-code」モデルを使用してリソースをインストール、構成および管理できます。

手元のOCI環境を再構築する必要があり、かねてからやる必要を感じていた OCI Resource Manager による環境構築を実践してみたやで。
彡(゚)(゚)

1. 構築した環境

下記記事(Terraform)の環境を OCI Resource Manager で再構築してみました。アイコン群がちょっと懐かしい……。

image.png

2. GitHub の OCI Resource Managerサンプル

GitHub に OCI Resource Manager のサンプルをアップロードしました。

下記コマンドでリポジトリをクローンして下さい。gitコマンドが使用不可の場合は、上記URLから Codeボタン→Download ZIPして下さい。

git clone https://github.com/gonsuke777/terraform

3つディレクトリが作成されますが、本記事では OCI Resource Manager用 の oci_test_env1_rm を使用します。

3. SSHキーペアの作成と公開鍵の編集

下記マニュアルを参照してSSHキーペアを2つ作成して、それぞれの公開鍵を zzzzzzzz.pub と yyyyyyyy.pub にセットして下さい。ファイル名は変更可能です。ファイル名を変更した場合は後述の vars.tf を編集します。

キー・ペアの作成
https://docs.oracle.com/ja-jp/iaas/Content/GSG/Tasks/creatingkeys.htm

既に利用可能なSSHキーペアを保持している場合は、本作業はスキップして問題ありません。

4. vars.tf の変数を記述

vars.tf の変数を記述します。内容は以下の通りです。

変数 説明
tenancy_ocid テナントのOCIDを記述します。
region リージョン名を記述します。ap-tokyo-1, us-phoenix-1 など
compartment_ocid コンパートメントのOCIDを記述します。
pub_subnet_seclst_ingress_rule パブリック・サブネットにアクセスする接続元(例:PCなど)のCIDRを記述します。制限しない場合は 0.0.0.0/0 を記述して下さい。
os_image_source_id Provider OSイメージ(Oracle Linuxなど)のOCIDを記述します。https://docs.cloud.oracle.com/iaas/images/ を参照して下さい。デフォルト値は Oracle Linux 8.8 の TokyoリージョンのイメージOCIDとしています。
public_compute_ssh_key パブリック・サブネットに作成する Compute の SSH鍵(公開鍵)のファイル名を記述します。
private_compute_ssh_key プライベート・サブネットに作成する Compute の SSH鍵(公開鍵)のファイル名を記述します。
public_compute_ad_num パブリック・サブネットに作成する Compute の AD(Availability Domain)番号を指定します。1ADリージョンの場合は 1 のみ指定可能です。
private_compute_ad_num プライベート・サブネットに作成する Compute の AD(Availability Domain)番号を指定します。1ADリージョンの場合は 1 のみ指定可能です。
oci_resource_prefix リソース名の接頭辞(プレフィックス)を4文字以内で記述します。
vars.tf
# Variables

## Provider Variables
### tenant ocid
variable "tenancy_ocid" {
  default = "ocid1.tenancy.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

### OCI region (ex:ap-tokyo-1, us-phoenix-1, etc...)
variable "region" {
  default = "ap-tokyo-1"
}

### Compartment OCID
variable "compartment_ocid" {
  description = "The compartment which resources will be created"
  default     = "ocid1.compartment.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

## Networking Variables
### Public Subnet - Security List Ingress Rule
variable "pub_subnet_seclst_ingress_rule" {
  description = "Describe the CIDR of the connection source (ex: your PC, etc..). If you want to allow from all networks, describe 0.0.0.0/0."
  default     = "xxx.xxx.xxx.xxx/xx"
}

## Compute Variables
### Regional OS Image ID, this ID is different for each region. Look https://docs.cloud.oracle.com/iaas/images/
variable "os_image_source_id" {
  description = "For OS Image Source ID, look https://docs.cloud.oracle.com/iaas/images/"
  default     = "ocid1.image.oc1.ap-tokyo-1.aaaaaaaaqgqr35z52xjqh3nxinud7ixdft3dxdsescidrgeh2v5rp6dqfwea"
}

### Compute ssh key path(pub format)
variable "public_compute_ssh_key" {
  description = "Public Compute ssh key file (in pub format) path (.../.../<key_filename>)"
  default     = "./zzzzzzzz.pub"
}

variable "private_compute_ssh_key" {
  description = "Private Compute ssh key file (in pub format) path (.../.../<key_filename>)"
  default     = "./yyyyyyyy.pub"
}

### Compute Availability Domain(1 or 2 or 3)
variable "public_compute_ad_num" {
  description = "Public Compute Availability Domain Num(1 or 2 or 3)"
  default     = "1"
}

variable "private_compute_ad_num" {
  description = "Private Compute Availability Domain Num(1 or 2 or 3)"
  default     = "1"
}

## Resource Prefix
variable "oci_resource_prefix" {
  description = "The prefix of all the resources to be created. Maximum 4 characters."
  default     = "AYS"
}

5. Resource Manager の Stack作成 および plan, apply によるプロビジョニング

OCIコンソールの Resouce Manager Stacks画面に遷移して Stack を作成します。
vars.tf を編集した oci_test_env1_rmディレクトリを Stack にアップロードして下さい。
OCI_RM_01.png
OCI_RM_02.png
OCI_RM_03.png
OCI_RM_04.png
OCI_RM_05.png
OCI_RM_06.png
OCI_RM_07.png
 
作成した Resource Manager Stack を plan, apply します。
OCI_RM_08.png
OCI_RM_09.png

成功しました。Compute も作成されてますやね彡(^)(^)
OCI_RM_10.png

ログインもできています。
OCI_RM_11.png

6. 元記事からの変更点

vars.tf および provider.tf の user_ocid, fingerprint, private_key_path は削除しています。

Terraform の場合は Terraform を実行するためのOCIユーザー関連の情報が必要ですが、OCI Resource Manager の場合はOCIコンソールにログインしたOCIユーザーの権限(ポリシー)で実行されるため、これらの情報は不要だからです。

provider.tf
# Terraform Provider

provider "oci" {
  tenancy_ocid     = "${var.tenancy_ocid}"
  user_ocid        = "${var.iam_user_ocid}" #削除
  fingerprint      = "${var.iam_user_fingerprint}" #削除
  private_key_path = "${var.iam_user_private_key_path}" #削除
  region           = "${var.region}"
}
vars.tf
:
### IAM User OCID --削除
variable "iam_user_ocid" {
  default = "ocid1.user.oc1..xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
}

### API fingerprint of IAM User --削除
variable "iam_user_fingerprint" {
  default = "xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx"
}

### IAM User private key path(pair of API fingerprint) --削除
variable "iam_user_private_key_path" {
  default = "./sshkey/oci-terraform.pem"
}

Compute のシェイプは VM.Standard.E2.1 から VM.Standard.E4.flex に変更しています。
OSイメージが元のシェイプに対応していませんでした。流石にシェイプが古すぎた^q^

compute.tf
:
shape               = "VM.Standard.E2.1"
:

compute.tf
:
  shape               = "VM.Standard.E4.Flex"
  shape_config {
    memory_in_gbs = "16"
    ocpus = "1"
  }
:

パブリック・サブネットにアタッチするセキュリティ・リストの Ingress Rule は CIDR を変数として入力するようにしました。0.0.0.0/0 は必要に応じて設定しましょう。

networking.tf
:
  ingress_security_rules {
    source      = "0.0.0.0/0"
    source_type = "CIDR_BLOCK"
    protocol    = "6"
    stateless   = "false"
    tcp_options {
      max = "22"
      min = "22"
    }
  }
:

networking.tf
:
ingress_security_rules {
    source      = "${var.pub_subnet_seclst_ingress_rule}"
    source_type = "CIDR_BLOCK"
    protocol    = "6"
    stateless   = "false"
    tcp_options {
      max = "22"
      min = "22"
    }
  }
:

vars.tf に定義した OSイメージ の OCID は Tokyoリージョン Oracle Linux 8.8(x86版) の OCID としています。

vars.tf
:
### Regional OS Image ID, this ID is different for each region. Look https://docs.cloud.oracle.com/iaas/images/
variable "os_image_source_id" {
  description = "For OS Image Source ID, look https://docs.cloud.oracle.com/iaas/images/"
  default     = "ocid1.image.oc1.ap-tokyo-1.aaaaaaaaqgqr35z52xjqh3nxinud7ixdft3dxdsescidrgeh2v5rp6dqfwea"
}
:

7. リソースの削除(Destroy)

Resource Manager の Stack から Destroy を実行すると、該当Stack から作成されたリソースが削除されます。

OCI_RM_12.png
OCI_RM_13.png

Destroy でリソースが削除されました。簡単に削除できるのは基本エエんですが、誤削除の危険も有るため要注意です。

個人的な考えとして、データストア系のリソース(OCI の場合は BaseDB, Autonomous Database, ExaDB-D など)は OCI Resource Manager や Terraform では作らない、あるいは Resource Manager や Terraform の実行ユーザーからリソースの削除権限(ポリシー)は剥奪しておいた方が良いと認識しています。
(゚ε゚ )

これは下記マニュアルにも同様の記載があります。Resource Manager Stack の Plan と Apply のジョブのみ許可することで、Destroy のジョブ実行を制限しています。

スタックとジョブの管理
https://docs.oracle.com/ja-jp/iaas/Content/Security/Reference/resourcemanager_security.htm#iam-policies__stacks-jobs
グループが破棄ジョブの実行を明示的に禁止するには、このポリシーを作成します

Allow group to manage orm-jobs in tenancy where any {target.job.operation = 'PLAN', target.job.operation = 'APPLY'}

8. まとめ

OCI Resource Manager で各種リソースをプロビジョニングできました。個人的には諸事情で定期的に環境の再構築が発生するため、それが OCI RM化できたのは大きい……。
彡(^)(^)

皆さんどんどん活用して下さいね。

9. 追記(デフォルトのセキュリティ・リストの編集)

デフォルトのセキュリティ・リストでは Ingress Rule が 0.0.0.0/0 で設定されているため、必要に応じて編集/削除して下さい。
image.png

(更に追記)デフォルトのセキュリティ・リストは oci_core_default_security_list で定義できる事が判明したので、Terraform定義に追記しておきました。

networking.tf

resource oci_core_default_security_list export_DEFAULT_SECURITY_LIST1 {
  manage_default_resource_id = oci_core_vcn.export_VCN1.default_security_list_id
  compartment_id = "${var.compartment_ocid}"
  display_name   = "${upper(var.oci_resource_prefix)}-DEFAULT-SECURITY-LIST1"
  egress_security_rules {
    destination      = "0.0.0.0/0"
    destination_type = "CIDR_BLOCK"
    protocol         = "6"
    stateless        = "false"
  }
  ingress_security_rules {
    source      = "${var.pub_subnet_seclst_ingress_rule}"
    source_type = "CIDR_BLOCK"
    protocol    = "6"
    stateless   = "false"
    tcp_options {
      max = "22"
      min = "22"
    }
  }
}

OCI_RM_17.png

本項(oci_core_default_security_list)は下記が参考になります。

2
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?