LoginSignup
11
3

More than 3 years have passed since last update.

terraform-provider-ociのエクスポート機能がとても便利

Last updated at Posted at 2020-03-24

1. はじめに

Oracle Cloud Infrastructureの構築自動化にTerraformは欠かせない。しかし初心者にハードルは高いし、いちいちtfファイルを作るのは面倒だ。そんなとき以下のブログでエクスポート機能があることを発見。

Create Terraform recipes for OCI in minutes!

簡単に言えば、現在のコンパートメント情報を抜き出してtfファイルを作成する機能だ。そこで実際に触ってみた。

マニュアルの記述はこちら。
Discovering Terraform resources in an Oracle Cloud Infrastructure compartment

2020/08/05追記:Resource Managerにエクスポート機能(Resource Discovery)が搭載されました。そのため管理コンソールからtfファイルをエクスポートできます。詳しくは以下のページを参照。

1-1. TL;DR

  • TerraformはOracle Cloud InfrastructureのInfrastructure as Codeに欠かせない
  • GitHubのサンプルから手で作るのは大変
  • terraform-provider-oci 3.50からエクスポート機能が使えるようになった

1-2. 対象環境/前提条件

  • Oracle Cloud Infrastructure
  • OCI CLIがセットアップしてあること
  • Linux/Mac OS(これ以外のOSでも動くハズだがテストしていない)

2. 事前準備

事前準備では、OCI CLIが利用可能なことを確認し、最新のterraform-provider-ociをインストールする。

2-1. OCI CLIの動作確認

OCI CLIがセットアップ済みであることを確認する。次のように表示されればOKだ。表示されないときは下記リンクを参考にセットアップすること。

$ oci os ns get
{
  "data": "nrvcrx1bzia3"
}

2-2. terraform-provider-ociのインストール

  1. terraform-provider-ociを入手するには以下の方法がある。今回は、わかりやすいWebページからダウンロードする方法を採用する。
  • Webページからダウンロード
  • terraform initを実行(.terraform/plugins配下にダウンロードされる)
  • yumでインストール

2.https://releases.hashicorp.com/terraform-provider-oci/ にアクセスし、最新バージョンをクリックする。
terraform-oci01.PNG

3.プラットフォームごとのモジュールが公開されている。今回はOracle Linux 7を使用するのでlinux_amd64をダウンロードする。Mac OSではdarwin_amd64をダウンロードする。

terraform-oci02.PNG

4.ダウンロードしたファイルを解凍する。

$ unzip terraform-provider-oci_3.67.0_linux_amd64.zip
Archive:  terraform-provider-oci_3.67.0_linux_amd64.zip
  inflating: terraform-provider-oci_v3.67.0_x4

5./usr/local/binにコピーする。

sudo cp terraform-provider-oci_v3.67.0_x4 /usr/local/bin/terraform-provider-oci

6.確認のためオプション無しで実行する。次のように表示されれば準備は完了だ。

$ terraform-provider-oci
2020/03/25 01:51:31 [INFO] terraform-provider-oci 3.67.0
2020/03/25 01:51:31 Executable runs in Terraform plugin mode by default. For additional usage options, please run with the '-help' flag.
This binary is a plugin. These are not meant to be executed directly.
Please execute the program that consumes these plugins, which will
load any plugins automatically

2. tfファイルをエクスポートする

  1. エクスポート時に指定するのは「コンパートメント名」と「出力先ディレクトリ」だ。
terraform-provider-oci -command=export -compartment_name=<コンパートメント名> -output_path=<出力先ディレクトリ>

2.実行結果は次のとおり。出力が止まったままになり不安になるが、最低5分くらいは待ってほしい。最後に「COMPLETED」と表示されれば終了だ。

$ terraform-provider-oci -command=export -compartment_name=work01 -output_path=./myrecipe
2020/03/25 01:54:43 [INFO] terraform-provider-oci 3.67.0
2020/03/25 01:54:43 [INFO] resolved compartment name 'work01' to compartment id 'ocid1.compartment.oc1..aaaaaaaaw5syvmuwyecuglndoy6anakd57l5yg6tbrysgfc3hvsxbz4evrmq'
2020/03/25 01:54:43 Running export command
2020/03/25 01:54:43 [INFO] resource discovery: visiting oci_identity_compartment.export
2020/03/25 01:54:43 [INFO] discovering resources with data source 'oci_identity_availability_domains'
2020/03/25 01:54:43 [INFO] resource discovery: visiting oci_identity_availability_domain.export_VPoc-AP-TOKYO-1-AD-1
2020/03/25 01:54:43 [INFO] discovering resources with data source 'oci_core_boot_volumes'
2020/03/25 01:54:43 [INFO] adding datasource query attribute 'availability_domain' from parent attribute 'name'
2020/03/25 01:54:43 [INFO] resource discovery: visiting oci_identity_compartment.export
2020/03/25 01:54:43 [INFO] discovering resources with data source 'oci_core_cpes'
★中略★
2020/03/25 01:56:53 [INFO] ===> Generating resource 'oci_identity_tag_default.export_tag_default_2'
Found 1 'availability_domain' resources. Generated under './myrecipe/availability_domain.tf'
Found 12 'core' resources. Generated under './myrecipe/core.tf'
Found 0 'database' resources. Generated under './myrecipe/database.tf'
Found 0 'load_balancer' resources. Generated under './myrecipe/load_balancer.tf'
Found 2 'object_storage' resources. Generated under './myrecipe/object_storage.tf'
Found 5 'tagging' resources. Generated under './myrecipe/tagging.tf'

=== COMPLETED ===

3.出力されたファイルは次のとおり。サイズが63bytesのものは、コメントだけで中身はない。core.tfには、ネットワークとComputeが混在しているので、実際には分割して運用した方がいいだろう。

$ ls -l myrecipe/
合計 40
-rw-rw-r--. 1 opc opc   201  3月 25 01:56 availability_domain.tf
-rw-rw-r--. 1 opc opc 13907  3月 25 01:56 core.tf
-rw-rw-r--. 1 opc opc    63  3月 25 01:56 database.tf
-rw-rw-r--. 1 opc opc    63  3月 25 01:56 load_balancer.tf
-rw-rw-r--. 1 opc opc   809  3月 25 01:56 object_storage.tf
-rw-rw-r--. 1 opc opc  1584  3月 25 01:56 tagging.tf
-rw-rw-r--. 1 opc opc   170  3月 25 01:56 vars.tf

4.上記以外のオプションやIAM関連などの出力方法はマニュアルを読んでほしい。

3. tfファイルの活用方法

生成したtfファイルは、そのままTerraformで使ってもいいし、Resource Managerを使う方法もある。

Resource Managerで使用する方法は、冒頭で紹介したブログからリンクしているYouTube: Create Terraform recipes for OCI in minutes!の3:47付近で解説されている。

zipで圧縮してResourceManagerのStackにアップロード
$ cd myrecipe
$ zip myrecipe.zip *
11
3
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
11
3