Terrafom remote state(外部tfstateのリソース)を使用する方法

Terrafom remote state(外部tfstateのリソース)を使用する方法

Terraformで外部tfstateのリソースを使用する場合は普通にあり得ます。特にマイクロサービス化などでtfstateを細かく分割している場合には必須です。

今回これを対応してみたので記録しておきます。

前提

前提として、tfstateはS3に保存しておきます。

そのバケットをbucket-aとします。

バケット内のtfstateをa-tfstate.tfstateとします。

コード

ver terraform 0.12

data "terraform_remote_state" "name-a" {
  backend = "s3"
  workspace = terraform.workspace
  config = {
    region = "ap-northeast-1"
    bucket="bucket-a"
    key = "a-tfstate.tfstate"
  }
}

これを定義しておくことで、使用したい箇所で以下のように指定すれば大丈夫です。

例えばこれのarnを使用したい場合

使用したい箇所のarns設定 = data.terraform_remote_state.name-a.outputs.arn

以上でした。

参考

https://www.terraform.io/docs/providers/terraform/d/remote_state.html

Comments

No comments yet. Why don’t you start the discussion?

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です