Terraform 0.7でterraform_remote_stateはdeprecated扱い
Terraform 0.7がリリースされましたね。
このバージョンからterraform_remote_stateリソースがdeprecated扱いになっています。
今まで
Terraformでの出力を別なTerraformで利用する場合、今まで以下のような感じであったと思います。
出力側
output "repo1_data" { value = "Repo1 Data" }
これをRemote Stateとして、S3に保存するとします。以下のような感じで。
terraform remote config \ -backend=S3 \ -backend-config="bucket=bucket-name" \ -backend-config="key=remote.tfstate" \ -backend-config="region=ap-northeast-1"
利用側
このRemote Stateを使う側は以下のような感じ。
resource "terraform_remote_state" "repo1" { backend = "s3" config { bucket = "bucket-name" key = "remote.tfstate" region = "${var.aws_region}" profile = "${var.aws_profile}" } } output "repo1_data" { value = "${terraform_remote_state.repo1.output.repo1_data}" }
これを apply すると以下のように repo1 の出力を取得できます。
$ terraform apply terraform_remote_state.repo1: Refreshing state... (ID: 2016-08-04 08:26:21.211961588 +0000 UTC) Apply complete! Resources: 0 added, 0 changed, 0 destroyed. Outputs: repo1_data = Repo1 Data
そのままで0.7で実行すると
利用側のコードをそのまま0.7で実行すると
$ terraform apply There are warnings and/or errors related to your configuration. Please fix these before continuing. Warnings: * terraform_remote_state.repo1: using terraform_remote_state as a resource is deprecated; consider using the data source instead No errors found. Continuing with 1 warning(s). terraform_remote_state.repo1: Refreshing state... (ID: 2016-08-04 08:29:31.623162303 +0000 UTC) Apply complete! Resources: 0 added, 0 changed, 0 destroyed.
と、deprecated と言われ、かつ repo1 の出力を取得できていません。
0.7でとりあえず出力するには
terraform_remote_stateリソースを使いつつ、出力するには以下のように、outputの記述をなくします。
resource "terraform_remote_state" "repo1" { backend = "s3" config { bucket = "bucket-name" key = "remote.tfstate" region = "${var.aws_region}" profile = "${var.aws_profile}" } } output "repo1_data" { value = "${terraform_remote_state.repo1.repo1_data}" }
この点は、 terraform/CHANGELOG.md at master · hashicorp/terraform · GitHub に記述されているように terraform_remote_stateの出力がトップレベル属性になったからのようです。
とりあえず、0.6 を利用しているひとは、output を削除する必要があります。
これで以下のように warning は出つつ、出力をとれるようになります。
$ terraform apply There are warnings and/or errors related to your configuration. Please fix these before continuing. Warnings: * terraform_remote_state.repo1: using terraform_remote_state as a resource is deprecated; consider using the data source instead No errors found. Continuing with 1 warning(s). terraform_remote_state.repo1: Refreshing state... (ID: 2016-08-04 08:50:54.409873377 +0000 UTC) Apply complete! Resources: 0 added, 0 changed, 0 destroyed. Outputs: repo1_data = Repo1 Data
これから
deprecated の出力はやはり嫌なので、正しい対応としては 0.7 から導入された Data Sources を利用することになります。
Remote Stateに対して 、下記 Data Sources が用意されました。
これを利用するには今まで resource と書いていたところを data と書き換えるだけです。出力のoutputを削るのは上記と同じ。
結局以下のような感じ。
data "terraform_remote_state" "repo1" { backend = "s3" config { bucket = "bucket-name" key = "remote.tfstate" region = "${var.aws_region}" profile = "${var.aws_profile}" } } output "repo1_data" { value = "${data.terraform_remote_state.repo1.repo1_data}" }
実行すると下記のようになります。
$ terraform apply data.terraform_remote_state.repo1: Refreshing state... Apply complete! Resources: 0 added, 0 changed, 0 destroyed. Outputs: repo1_data = Repo1 Data
ということで、これからは Data Sources を利用しましょう、ということですね。