普段AWS と GCP 触ってますが、 AWSだと dtan/terraforming をちょいちょい使ってて、GCP対応来ないかなぁーって思ってました……
そしたら、まさかのGoogleから terraforming terraformer なる物が出ていてビビったのでとりあえずちょっと試してみました。
対応provider
Google Cloud
AWS
OpenStack
Kubernetes
Github
GoogleCloudPlatform/terraformer
https://github.com/GoogleCloudPlatform/terraformer
環境
Mac OS X 10.14.5
Terraform v0.11.14
+ provider.google v2.7.0
Terraformer v0.7.3
インストール
公式 通りで問題なし
Go のバイナリを落としてきて、実行権限つけてPATHの通った場所に移動してる
curl -LO https://github.com/GoogleCloudPlatform/terraformer/releases/download/$(curl -s https://api.github.com/repos/GoogleCloudPlatform/terraformer/releases/latest | grep tag_name | cut -d '"' -f 4)/terraformer-darwin-amd64
chmod +x terraformer-darwin-amd64
sudo mv terraformer-darwin-amd64 /usr/local/bin/terraformer
こんな感じでバージョンとヘルプ見てみる。
❯❯❯ terraformer version
Terraformer v0.7.3
~ ❯❯❯ terraformer import google -h
Import current State to terraform configuration from google cloud
Usage:
import google [flags]
import google [command]
Available Commands:
list List supported resources for google provider
Flags:
-b, --bucket string gs://terraform-state
-c, --connect (default true)
-f, --filter strings google_compute_firewall=id1:id2:id4
-h, --help help for google
-o, --path-output string (default "generated")
-p, --path-pattern string {output}/{provider}/custom/{service}/ (default "{output}/{provider}/{service}/")
--projects strings
-r, --resources strings firewalls,networks
-s, --state string local or bucket (default "local")
-z, --zone string
Use " import google [command] --help" for more information about a command.
いざ実践
チームで使ってるGCPの検証環境がみんな好き放題使って一応terraform管理下にあるものの無法地帯です。
試しにExternalIP見てみたら、5つ作成されていたので、terraformer出来るか試してみます。
とりあえず実行してみる
terraformer import google
とか terraformer import aws
でprovider選べる。
optionの --resources=addresses, instances
でインポートするリソースを複数指定出来たりする。
❯❯❯ terraformer import google --resources=addresses --zone=asia-northeast1-b --projects=${PRJ_NAME}
2019/05/27 21:28:27 google importing project xxx
2019/05/27 21:28:27 google importing... addresses
2019/05/27 21:28:28 plugin error: open /Users/ando/.terraform.d/plugins/darwin_amd64: no such file or directory
2019/05/27 21:28:28 open /Users/ando/.terraform.d/plugins/darwin_amd64: no such file or directory
はい、エラー。なんかpluginsが無いって言ってる。
ちゃんと公式に書いてました。。
Copy your Terraform provider's plugin(s) to folder ~/.terraform.d/plugins/{darwin,linux}_amd64/, as appropriate.
という事で、pluginを以下に、コピーします。
❯❯❯ pwd
/Users/ando/.terraform.d/plugins/darwin_amd64
~/.t/p/darwin_amd64 ❯❯❯ ll
total 117448
-rwxr-xr-x 1 ando staff 82B 5 27 21:35 lock.json
-rwxr-xr-x 1 ando staff 57M 5 27 21:35 terraform-provider-google_v2.7.0_x4
気を取り直して
~/work ❯❯❯ terraformer import google --resources=addresses --zone=asia-northeast1-b --projects=${PRJ_NAME}
2019/05/27 21:38:38 google importing project xxx
2019/05/27 21:38:38 google importing... addresses
2019/05/27 21:38:40 google Connecting....
2019/05/27 21:38:40 google save addresses
2019/05/27 21:38:40 [DEBUG] New state was assigned lineage "xxx"
2019/05/27 21:38:40 google save tfstate for addresses
おお! うまくいったっぽい。
ちなみにこんな感じでディレクトリが出来る。
work/
├ generated/
│ └ google/
│ └ prjname/
└ addresses
でリソース毎にフォルダが作られて中にtfファイルとtfstateファイルとかができてる。
~/w/g/g/a/addresses ❯❯❯ ll
total 48
-rwxr-xr-x 1 ando staff 1.2K 5 27 21:38 compute_address.tf
-rwxr-xr-x 1 ando staff 566B 5 27 21:38 outputs.tf
-rwxr-xr-x 1 ando staff 76B 5 27 21:38 provider.tf
-rwxr-xr-x 1 ando staff 9.0K 5 27 21:38 terraform.tfstate
compute_address.tf
の中身
resource "google_compute_address" "tablet-proxy" {
address = "35.221.xxx.xxx"
address_type = "EXTERNAL"
name = "tablet-proxy"
network_tier = "PREMIUM"
project = "xxx"
region = "asia-northeast1"
}
resource "google_compute_address" "tbt-test" {
address = "35.200.xxx.xxx"
address_type = "EXTERNAL"
name = "tbt-test"
network_tier = "PREMIUM"
project = "xxx"
region = "asia-northeast1"
}
resource "google_compute_address" "tbt-vpn" {
address = "35.221.xxx.xxx"
address_type = "EXTERNAL"
name = "tbt-vpn"
network_tier = "PREMIUM"
project = "xxx"
region = "asia-northeast1"
}
resource "google_compute_address" "tbt-vpn-02" {
address = "35.221.xxx.xxx"
address_type = "EXTERNAL"
name = "tbt-vpn-02"
network_tier = "PREMIUM"
project = "xxx"
region = "asia-northeast1"
}
resource "google_compute_address" "test" {
address = "35.200.xxx.xxx"
address_type = "EXTERNAL"
name = "test"
network_tier = "PREMIUM"
project = "xxx"
region = "asia-northeast1"
}
outputs.tf
の中身
生成されたtfstateを本体のtfstateと別に管理したいが、リソース参照はしたい場合等 terraform_remote_state
を使って繋ぐようにとoutputが生成されました。
公式には以下で書いてました。
Connect between resources with terraform_remote_state (local and bucket).
output "google_compute_address_tablet-proxy_self_link" {
value = "${google_compute_address.tablet-proxy.self_link}"
}
output "google_compute_address_tbt-test_self_link" {
value = "${google_compute_address.tbt-test.self_link}"
}
output "google_compute_address_tbt-vpn-02_self_link" {
value = "${google_compute_address.tbt-vpn-02.self_link}"
}
output "google_compute_address_tbt-vpn_self_link" {
value = "${google_compute_address.tbt-vpn.self_link}"
}
output "google_compute_address_test_self_link" {
value = "${google_compute_address.test.self_link}"
}
tfstateへの取り込み
gcsで管理している既存のtfstateに取り込みたかったので terraform importで行いました。
先ずは取り込みたいリソースのIDを調べる必要があります。
❯❯❯ gcloud compute addresses describe tbt-test --region asia-northeast1 ✘ 130
address: 35.200.xxx.xxx
addressType: EXTERNAL
creationTimestamp: '2019-05-16T23:07:19.714-07:00'
description: ''
id: '57264297799720xxxxx'
kind: compute#address
name: tbt-test
networkTier: PREMIUM
region: https://www.googleapis.com/compute/v1/projects/xxx/regions/asia-northeast1
selfLink: https://www.googleapis.com/compute/v1/projects/xxx/regions/asia-northeast1/addresses/tbt-test
status: RESERVED
id: '57264297799720xxxxx'
を以下のコマンドにセットしてインポート実施!
❯❯❯ terraform import google_compute_address.tbt-test 57264297799720xxxxx master ✱
google_compute_address.tbt-test: Importing from ID "57264297799720xxxxx"...
google_compute_address.tbt-test: Import complete!
Imported google_compute_address (ID: xxx/asia-northeast1/57264297799720xxxxx)
google_compute_address.tbt-test: Refreshing state... (ID: xxx/asia-northeast1/57264297799720xxxxx)
Import successful!
The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.
後は必要なりソース数だけ同じことを繰り返し、最後にterraform plan で差分がないかチェックして終了!
No changes. Infrastructure is up-to-date.
最後に
・ もっとスマートなやり方や、tfstateの取り込み方で良い方法があれば知りたい。
・ 指定したgcpプロジェクトにAPI叩きに行って調べていると思うが、どこで(どのアカウント?)認証してるのか不明。