概要
リモートステートをつかって別のstateファイルの値を参照して見たいと思います。
これによって stateをまたいでいろいろできたりするのでstateの分離等もしやすくなるかもしれないしならないかもしれない。
EC2インスタンスを作成するstateと、それにEIPをアタッチするstateが別なんだけれどもアタッチができるよっていうのをやります。
前提
前提です。
バージョン
Terraformのバージョン等はこんなかんじ。
$ terraform --version Terraform v0.12.18 + provider.aws v2.43.0
EC2インスタンスをつくるstateのtfファイル
ひとつめのstateです。
provider "aws" { region = "ap-northeast-1" } terraform { backend "s3" { bucket = "mybucket" key = "terraform.tfstate" region = "ap-northeast-1" } } resource "aws_instance" "example" { ami = "ami-068a6cefc24c301d2" instance_type = "t3.micro" subnet_id = aws_subnet.subnet.id } resource "aws_vpc" "main" { cidr_block = "10.0.0.0/16" } resource "aws_subnet" "subnet" { cidr_block = "10.0.1.0/24" vpc_id = aws_vpc.main.id } resource "aws_internet_gateway" "igw" { vpc_id = aws_vpc.main.id } output "instance_id" { value = aws_instance.example.id }
EIPをアタッチするstateのtfファイル
2つめです。
provider "aws" { region = "ap-northeast-1" } data "terraform_remote_state" "remote_state" { backend = "s3" config = { bucket = "mybucket" key = "terraform.tfstate" region = "ap-northeast-1" } } resource "aws_eip" "eip" { instance = data.terraform_remote_state.remote_state.outputs.instance_id }
うごかしてみる
うごかしてみます
EC2インスタンスをつくるstateのtfファイル
こんなかんじになります。
参照される値はoutputで出力する必要があります。
$ terraform apply ~~~省略~~~ Apply complete! Resources: 1 added, 0 changed, 1 destroyed. Outputs: instance_id = i-02814f190bddac514
EIPをアタッチするstateのtfファイル
EIPがアタッチされていることがわかりますね
$ terraform apply data.terraform_remote_state.remote_state: Refreshing state... aws_eip.eip: Refreshing state... [id=eipalloc-08169d0c5eaa117f4] An execution plan has been generated and is shown below. Resource actions are indicated with the following symbols: -/+ destroy and then create replacement Terraform will perform the following actions: # aws_eip.eip is tainted, so must be replaced -/+ resource "aws_eip" "eip" { + allocation_id = (known after apply) + association_id = (known after apply) ~ domain = "vpc" -> (known after apply) ~ id = "eipalloc-08169d0c5eaa117f4" -> (known after apply) + instance = "i-02814f190bddac514" + network_interface = (known after apply) + private_dns = (known after apply) + private_ip = (known after apply) ~ public_dns = "ec2-3-114-205-18.ap-northeast-1.compute.amazonaws.com" -> (known after apply) ~ public_ip = "3.114.205.18" -> (known after apply) ~ public_ipv4_pool = "amazon" -> (known after apply) - tags = {} -> null ~ vpc = true -> (known after apply) } Plan: 1 to add, 0 to change, 1 to destroy. Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes aws_eip.eip: Destroying... [id=eipalloc-08169d0c5eaa117f4] aws_eip.eip: Destruction complete after 1s aws_eip.eip: Creating... aws_eip.eip: Creation complete after 1s [id=eipalloc-0bd8be7d66afa0c58] Apply complete! Resources: 1 added, 0 changed, 1 destroyed.
おわりに
べんりですね
Terraform: Up and Running: Writing Infrastructure as Code
- 作者:Yevgeniy Brikman
- 出版社/メーカー: O'Reilly Media
- 発売日: 2017/04/04
- メディア: ペーパーバック