Terraformで外部tfstateのリソースを使用する場合は普通にあり得ます。特にマイクロサービス化などでtfstateを細かく分割している場合には必須です。
今回これを対応してみたので記録しておきます。
前提
前提として、tfstateはS3に保存しておきます。
そのバケットをbucket-aとします。
バケット内のtfstateをa-tfstate.tfstateとします。
コード
ver terraform 0.12
1 2 3 4 5 6 7 8 9 |
data "terraform_remote_state" "name-a" { backend = "s3" workspace = terraform.workspace config = { region = "ap-northeast-1" bucket="bucket-a" key = "a-tfstate.tfstate" } } |
これを定義しておくことで、使用したい箇所で以下のように指定すれば大丈夫です。
例えばこれのarnを使用したい場合
1 |
使用したい箇所のarns設定 = data.terraform_remote_state.name-a.outputs.arn |
以上でした。
参考
https://www.terraform.io/docs/providers/terraform/d/remote_state.html