blog/content/posts/2021/2021-11-27-terraformer-conv...

12 KiB

+++ title = "terraformerを試しに使ってみましたよ" date = 2021-11-27T22:13:43+09:00 description = "必要に迫られてterraformerを使ってみました。terraform importをお手軽簡単にできるものみたいです。" tags = ["terraform"] categories = ["インフラ", "HashiCorp"] author = "kazu634" +++

必要に迫られてterraformerを使ってみました。terraform importをお手軽簡単にできるものみたいです。

terraformerとは

各種クラウドサービスなどから、Terraformのコードを生成してくれるツールです。インフラからソースコードを生成します。

事前準備

terraformerを利用する前の準備作業を説明します。

AWSのIAMユーザー作成

今回はAWSからコードを生成するので、AWSから情報を取得するIAMユーザーを作成します。ポリシーはReadOnlyAccessを割り当ててみました:

[image:A23972B4-D553-49EF-9990-D08463E1F4ED-877-00000015FDFCFCEF/IAM Management C.png]

AWS認証情報を設定ファイルに格納

~/.aws/credentialsに先ほど作成したIAMユーザーのアクセスキー・シークレットキーの情報、あとはリージョン情報を格納します:

[default]
aws_access_key_id=<ここにアクセスキー>
aws_secret_access_key=<ここにシークレットキー>
region=ap-northeast-1

terraformerのインストール

それではインストールしていきます:

kazu634@bastion2004% export PROVIDER=all
kazu634@bastion2004% 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-${PROVIDER}-linux-amd64
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   666  100   666    0     0   2466      0 --:--:-- --:--:-- --:--:--  2475
100  358M  100  358M    0     0  21.0M      0  0:00:17  0:00:17 --:--:-- 26.9M
kazu634@bastion2004% chmod +x terraformer-${PROVIDER}-linux-amd64
kazu634@bastion2004% sudo mv terraformer-${PROVIDER}-linux-amd64 /usr/local/bin/terraformer
[sudo] password for kazu634:

terraformerを使ってみる

まずはAWSの情報を取得するということを宣言するようで、init.tfprovider “aws” {}を書き込み、terraform initを実行します:

kazu634@bastion2004% echo 'provider "aws" {}' > init.tf
kazu634@bastion2004% terraform init
Initializing the backend...

Initializing provider plugins...
- Finding latest version of hashicorp/aws...
- Installing hashicorp/aws v3.67.0...
- Installed hashicorp/aws v3.67.0 (signed by HashiCorp)

Terraform has created a lock file .terraform.lock.hcl to record the provider
selections it made above. Include this file in your version control repository
so that Terraform can guarantee to make the same selections by default when
you run "terraform init" in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

Route53の情報を取得する場合は、次のようにします:

kazu634@bastion2004% terraformer import aws --resources=route53
2021/11/27 21:25:49 aws importing default region
2021/11/27 21:25:51 aws importing... route53
2021/11/27 21:25:52 aws done importing route53
2021/11/27 21:25:52 Number of resources for service route53: 16
2021/11/27 21:25:52 Refreshing state... aws_route53_record.tfer--ZI0FHD0611WVA_kazu634-002E-com-002E-_NS_
2021/11/27 21:25:52 Refreshing state... aws_route53_record.tfer--ZI0FHD0611WVA_blog-002E-kazu634-002E-com-002E-_A_
2021/11/27 21:25:52 Refreshing state... aws_route53_record.tfer--ZI0FHD0611WVA_pocket-002E-kazu634-002E-com-002E-_A_
2021/11/27 21:25:52 Refreshing state... aws_route53_record.tfer--ZI0FHD0611WVA_minio-002E-kazu634-002E-com-002E-_A_
2021/11/27 21:25:52 Refreshing state... aws_route53_record.tfer--ZI0FHD0611WVA_openvpn-002E-kazu634-002E-com-002E-_CNAME_
2021/11/27 21:25:52 Refreshing state... aws_route53_record.tfer--ZI0FHD0611WVA_git-002E-kazu634-002E-com-002E-_A_
2021/11/27 21:25:52 Refreshing state... aws_route53_record.tfer--ZI0FHD0611WVA_kazu634-002E-com-002E-_SOA_
2021/11/27 21:25:52 Refreshing state... aws_route53_record.tfer--ZI0FHD0611WVA_kazu634-002E-com-002E-_A_
2021/11/27 21:25:52 Refreshing state... aws_route53_record.tfer--ZI0FHD0611WVA_blog-002E-kazu634-002E-com-002E-_CAA_
2021/11/27 21:25:52 Refreshing state... aws_route53_record.tfer--ZI0FHD0611WVA_grafana-002E-kazu634-002E-com-002E-_A_
2021/11/27 21:25:52 Refreshing state... aws_route53_record.tfer--ZI0FHD0611WVA_gitea-002E-kazu634-002E-com-002E-_A_
2021/11/27 21:25:52 Refreshing state... aws_route53_record.tfer--ZI0FHD0611WVA_test-002E-kazu634-002E-com-002E-_A_
2021/11/27 21:25:52 Refreshing state... aws_route53_record.tfer--ZI0FHD0611WVA_g-002E-kazu634-002E-com-002E-_A_
2021/11/27 21:25:52 Refreshing state... aws_route53_record.tfer--ZI0FHD0611WVA_faktory-002E-kazu634-002E-com-002E-_A_
2021/11/27 21:25:52 Refreshing state... aws_route53_zone.tfer--ZI0FHD0611WVA_kazu634-002E-com
2021/11/27 21:25:54 Refreshing state... aws_route53_record.tfer--ZI0FHD0611WVA_drone-002E-kazu634-002E-com-002E-_A_
2021/11/27 21:25:55 Filtered number of resources for service route53: 16
2021/11/27 21:25:55 aws Connecting....
2021/11/27 21:25:55 aws save route53
2021/11/27 21:25:55 aws save tfstate for route53

するとgeneratedというディレクトリーが作成され、その中にterraformのソースコードが格納されます:

kazu634@bastion2004% ll
total 28K
drwxrwxr-x  4 kazu634 kazu634 4.0K Nov 27 21:25 .
drwxr-xr-x 13 kazu634 kazu634 4.0K Nov 26 23:51 ..
drwxrwxr-x  3 kazu634 kazu634 4.0K Nov 27 21:25 generated
-rw-rw-r--  1 kazu634 kazu634   18 Nov 27 00:54 init.tf
drwxr-xr-x  3 kazu634 kazu634 4.0K Nov 27 00:54 .terraform
-rw-r--r--  1 kazu634 kazu634 1.1K Nov 27 00:54 .terraform.lock.hcl

generatedの中身はこのようになっています:

kazu634@bastion2004% pwd
/home/kazu634/works/mnt/others/terraformer/generated

kaws
    └── route53
        ├── outputs.tf
        ├── provider.tf
        ├── route53_record.tf
        ├── route53_zone.tf
        └── terraform.tfstate

2 directories,  5 files

たとえばroute53_record.tfの中身はこのようになっています:

kazu634@bastion2004% cat route53_record.tf
resource "aws_route53_record" "tfer--ZI0FHD0611WVA_blog-002E-kazu634-002E-com-002E-_A_" {
  name    = "blog.kazu634.com"
  records = ["52.193.98.253"]
  ttl     = "86400"
  type    = "A"
  zone_id = "${aws_route53_zone.tfer--ZI0FHD0611WVA_kazu634-002E-com.zone_id}"
}

resource "aws_route53_record" "tfer--ZI0FHD0611WVA_blog-002E-kazu634-002E-com-002E-_CAA_" {
  name    = "blog.kazu634.com"
  records = ["0 issue \"letsencrypt.org\""]
  ttl     = "86400"
  type    = "CAA"
  zone_id = "${aws_route53_zone.tfer--ZI0FHD0611WVA_kazu634-002E-com.zone_id}"
}

resource "aws_route53_record" "tfer--ZI0FHD0611WVA_drone-002E-kazu634-002E-com-002E-_A_" {
  name    = "drone.kazu634.com"
  records = ["52.193.98.253"]
  ttl     = "3600"
  type    = "A"
  zone_id = "${aws_route53_zone.tfer--ZI0FHD0611WVA_kazu634-002E-com.zone_id}"
}

resource "aws_route53_record" "tfer--ZI0FHD0611WVA_faktory-002E-kazu634-002E-com-002E-_A_" {
  name    = "faktory.kazu634.com"
  records = ["52.193.98.253"]
  ttl     = "3600"
  type    = "A"
  zone_id = "${aws_route53_zone.tfer--ZI0FHD0611WVA_kazu634-002E-com.zone_id}"
}

resource "aws_route53_record" "tfer--ZI0FHD0611WVA_g-002E-kazu634-002E-com-002E-_A_" {
  name    = "g.kazu634.com"
  records = ["52.193.98.253"]
  ttl     = "3600"
  type    = "A"
  zone_id = "${aws_route53_zone.tfer--ZI0FHD0611WVA_kazu634-002E-com.zone_id}"
}

resource "aws_route53_record" "tfer--ZI0FHD0611WVA_git-002E-kazu634-002E-com-002E-_A_" {
  name    = "git.kazu634.com"
  records = ["52.193.98.253"]
  ttl     = "3600"
  type    = "A"
  zone_id = "${aws_route53_zone.tfer--ZI0FHD0611WVA_kazu634-002E-com.zone_id}"
}

resource "aws_route53_record" "tfer--ZI0FHD0611WVA_gitea-002E-kazu634-002E-com-002E-_A_" {
  name    = "gitea.kazu634.com"
  records = ["52.193.98.253"]
  ttl     = "3600"
  type    = "A"
  zone_id = "${aws_route53_zone.tfer--ZI0FHD0611WVA_kazu634-002E-com.zone_id}"
}

resource "aws_route53_record" "tfer--ZI0FHD0611WVA_grafana-002E-kazu634-002E-com-002E-_A_" {
  name    = "grafana.kazu634.com"
  records = ["52.193.98.253"]
  ttl     = "3600"
  type    = "A"
  zone_id = "${aws_route53_zone.tfer--ZI0FHD0611WVA_kazu634-002E-com.zone_id}"
}

resource "aws_route53_record" "tfer--ZI0FHD0611WVA_kazu634-002E-com-002E-_A_" {
  name    = "kazu634.com"
  records = ["52.193.98.253"]
  ttl     = "3600"
  type    = "A"
  zone_id = "${aws_route53_zone.tfer--ZI0FHD0611WVA_kazu634-002E-com.zone_id}"
}

resource "aws_route53_record" "tfer--ZI0FHD0611WVA_kazu634-002E-com-002E-_NS_" {
  name    = "kazu634.com"
  records = ["ns-1111.awsdns-10.org.",  "ns-469.awsdns-58.com.",  "ns-720.awsdns-26.net.",  "ns-1844.awsdns-38.co.uk."]
  ttl     = "172800"
  type    = "NS"
  zone_id = "${aws_route53_zone.tfer--ZI0FHD0611WVA_kazu634-002E-com.zone_id}"
}

resource "aws_route53_record" "tfer--ZI0FHD0611WVA_kazu634-002E-com-002E-_SOA_" {
  name    = "kazu634.com"
  records = ["ns-720.awsdns-26.net. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"]
  ttl     = "900"
  type    = "SOA"
  zone_id = "${aws_route53_zone.tfer--ZI0FHD0611WVA_kazu634-002E-com.zone_id}"
}

resource "aws_route53_record" "tfer--ZI0FHD0611WVA_minio-002E-kazu634-002E-com-002E-_A_" {
  name    = "minio.kazu634.com"
  records = ["52.193.98.253"]
  ttl     = "3600"
  type    = "A"
  zone_id = "${aws_route53_zone.tfer--ZI0FHD0611WVA_kazu634-002E-com.zone_id}"
}

resource "aws_route53_record" "tfer--ZI0FHD0611WVA_openvpn-002E-kazu634-002E-com-002E-_CNAME_" {
  name    = "openvpn.kazu634.com"
  records = ["52.193.98.253"]
  ttl     = "3600"
  type    = "CNAME"
  zone_id = "${aws_route53_zone.tfer--ZI0FHD0611WVA_kazu634-002E-com.zone_id}"
}

resource "aws_route53_record" "tfer--ZI0FHD0611WVA_pocket-002E-kazu634-002E-com-002E-_A_" {
  name    = "pocket.kazu634.com"
  records = ["52.193.98.253"]
  ttl     = "3600"
  type    = "A"
  zone_id = "${aws_route53_zone.tfer--ZI0FHD0611WVA_kazu634-002E-com.zone_id}"
}

resource "aws_route53_record" "tfer--ZI0FHD0611WVA_test-002E-kazu634-002E-com-002E-_A_" {
  name    = "test.kazu634.com"
  records = ["52.193.98.253"]
  ttl     = "3600"
  type    = "A"
  zone_id = "${aws_route53_zone.tfer--ZI0FHD0611WVA_kazu634-002E-com.zone_id}"
}

まとめ

terraform importコマンドを使うと、AWSリソースのIDをいちいち調べて指定して、一つずつインポートするという苦行をしていたのですが、terraformerを利用するとコマンド一発でterraformのコードに落とし込んでもらえるので、だいぶ楽になるということがわかりました。はまるとすると、IAMとかポリシーの部分ですかね。