自動化ツールの一つである「Terraform」について、これから学ぼうという方、使っていきたい方を対象に、Terraformの導入方法や、基本的な使い方を紹介していきます。今回は、Terraformで作成したリソースに関するデータをTerraformで出力させる方法を解説します。
この記事は会員限定です。会員登録(無料)すると全てご覧いただけます。
本連載では前回まで、Terraformを利用してクラウドリソースを作成する方法を解説してきました。しかし、実際にはリソースを作成しておしまいではなく、「Amazon EC2(Elastic Compute Cloud)」インスタンス(以下、EC2インスタンス)を作成後、インスタンスに対してSSH(Secure Shell)などで接続、ログインして作業をする必要があります。
しかし、SSHでインスタンスに接続する際には、対象の環境のIPアドレスを知る必要があり、そのたびに「Amazon Web Services(AWS)」の管理コンソールにアクセスしてIPアドレスを確認するというのは、せっかくTerraformでリソースの作成を自動化したのに、何だか残念な気持ちにならないでしょうか。そこで今回は、作成したリソースに関するIPアドレスなどの情報をTerraformで出力させる方法を紹介します。
始めに、データを出力させたい対象のリソースとして、前回作成した以下のTerraformのコードを用意し、EC2インスタンスを作成します。
locals { instance_name = "example" } resource "aws_instance" "example" { ami = "ami-098940df4d3292e9a" instance_type = "t2.micro" root_block_device { tags = { Name = local.instance_name } } tags = { Name = local.instance_name } }
このTerraformのコードを使用してterraform applyコマンドを実行しても、IPアドレスを持つようなpublic_ipやipv6_addressesは「known after apply」という表示になり、IPアドレス自体は表示されていません。
$ terraform apply Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_instance.example will be created + resource "aws_instance" "example" { + ami = "ami-098940df4d3292e9a" + arn = (known after apply) + associate_public_ip_address = (known after apply) + availability_zone = (known after apply) + cpu_core_count = (known after apply) + cpu_threads_per_core = (known after apply) + disable_api_stop = (known after apply) + disable_api_termination = (known after apply) + ebs_optimized = (known after apply) + get_password_data = false + host_id = (known after apply) + host_resource_group_arn = (known after apply) + iam_instance_profile = (known after apply) + id = (known after apply) + instance_initiated_shutdown_behavior = (known after apply) + instance_state = (known after apply) + instance_type = "t2.micro" + ipv6_address_count = (known after apply) + ipv6_addresses = (known after apply) + key_name = (known after apply) + monitoring = (known after apply) + outpost_arn = (known after apply) + password_data = (known after apply) + placement_group = (known after apply) + placement_partition_number = (known after apply) + primary_network_interface_id = (known after apply) + private_dns = (known after apply) + private_ip = (known after apply) + public_dns = (known after apply) + public_ip = (known after apply) + secondary_private_ips = (known after apply) + security_groups = (known after apply) + source_dest_check = true + subnet_id = (known after apply) + tags = { + "Name" = "example" } + tags_all = { + "Name" = "example" } + tenancy = (known after apply) + user_data = (known after apply) + user_data_base64 = (known after apply) + user_data_replace_on_change = false + vpc_security_group_ids = (known after apply) + root_block_device { + delete_on_termination = true + device_name = (known after apply) + encrypted = (known after apply) + iops = (known after apply) + kms_key_id = (known after apply) + tags = { + "Name" = "example" } + throughput = (known after apply) + volume_id = (known after apply) + volume_size = (known after apply) + volume_type = (known after apply) } } Plan: 1 to add, 0 to change, 0 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_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Still creating... [20s elapsed] aws_instance.example: Still creating... [30s elapsed] aws_instance.example: Creation complete after 38s [id=i-xxxxxxxxxxxxxxxxx] Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
それでは、以下のような記述を追加してみましょう。
output "example_public_ip" { value = aws_instance.example.public_ip }
上記の記述は、aws_instance.exampleリソースのpublic_ipの内容を、「example_public_ip」という名前で出力するコードです。早速、terraform applyコマンドを実行してみましょう。
$ terraform apply Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # aws_instance.example will be created + resource "aws_instance" "example" { (中略) } Plan: 1 to add, 0 to change, 0 to destroy. Changes to Outputs: + example_ip_address = (known after apply) 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_instance.example: Creating... aws_instance.example: Still creating... [10s elapsed] aws_instance.example: Still creating... [20s elapsed] aws_instance.example: Still creating... [30s elapsed] aws_instance.example: Still creating... [40s elapsed] aws_instance.example: Creation complete after 45s [id=i-xxxxxxxxxxxxxxxxx] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: example_ip_address = "xxx.xxx.xxx.xxx"
最後に「Outputs:」という項目が増え、先ほど定義した「example_ip_address」という名前とともにIPアドレスが出力されるようになりました。
今回追加した記述はoutputブロックと呼ばれ、valueに指定した内容を画面に出力できるようになります。valueには変数を指定するだけでなく、以下のように任意の文字列を組み合わせて出力することも可能です。
output "example_ip_address" { value = "IP address is ${aws_instance.example.public_ip}" }
このコードを実行すると、以下のように出力されます。
Outputs: example_ip_address = "IP address is xxx.xxx.xxx.xxx"
また、outputブロックは複数定義することも可能です。先ほどのexample_ip_addressに加えて、パブリックDNSを表示してみましょう。以下のTerraformのコードを追加します。
output "example_public_dns" { value = aws_instance.example.public_dns }
追加したら、terraform applyコマンドを実行して、出力内容が増えていることを確認します。
$ terraform apply aws_instance.example: Refreshing state... [id=i-xxxxxxxxxxxxxxxxx] Changes to Outputs: + example_public_dns = "ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com" You can apply this plan to save these new output values to the Terraform state, without changing any real infrastructure. 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 Apply complete! Resources: 0 added, 0 changed, 0 destroyed. Outputs: example_ip_address = "xxx.xxx.xxx.xxx" example_public_dns = "ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com"
先ほど紹介した方法でterraform applyコマンドを実行した際に指定した情報が出力できるようになりました。では、terraform applyコマンドの実行後に、もう一度情報を出力したいとき、どうすれば良いでしょうか。
再度、terraform applyコマンドを実行してもよいのですが、これではTerraformのコードと実際のリソースの状況に差異がないかどうかを確認する通信が発生して時間もかかります。それを見越したのか、Terraformにはterraform outputというコマンドが存在しています。このコマンドを使用すると、outputブロックで指定した情報を出力できます。
試しに、前述したTerraformのコードでEC2インスタンスを作成した状態で、terraform outputコマンドを実行してみましょう。
$ terraform output example_ip_address = "xxx.xxx.xxx.xxx" example_public_dns = "ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com"
outputブロックで指定した内容のみが出力されました。terraform outputコマンドの引数に名前を指定することで、指定したデータのみ出力することもできます。
$ terraform output example_public_dns "ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com"
Terraformからデータを出力するのは大変便利な機能である一方、中にはパスワードのように機密扱いにしたいデータもあるでしょう。
例として、先ほどまでに作成したoutputブロックのうち、example_ip_addressを機密扱いにしてみます。出力データを機密扱いにするには、該当するoutputブロックにsensitive設定を追加します。
output "example_public_ip" { value = aws_instance.example.public_ip sensitive = true }
この状態でterraform applyコマンドを実行すると、データの出力部分は次のようになります。
$ terraform apply (中略) Outputs: example_ip_address = <sensitive> example_public_dns = "ec2-54-198-0-81.compute-1.amazonaws.com"
sensitive設定を追加したexample_ip_addressは、データの値として<
$ terraform output example_ip_address = <sensitive> example_public_dns = "ec2-xxx-xxx-xxx-xxx.compute-1.amazonaws.com"
このように、機密扱いにした出力データはterraform applyやterraform outputコマンドを実行したときには表示されませんが、かわりにterraform outputの引数に表示させたい出力データ名を指定すると、そのデータの値が内容が表示されます。
$ terraform output example_ip_address "xxx.xxx.xxx.xxx"
今回は、Terraformで作成したクラウドリソースの状況を、クラウドサービス側の管理コンソールにアクセスせず必要な情報を取得できる方法を紹介しました。これによりTerraformで作成したリソースを便利かつ迅速に管理できるようになるはずです。ぜひ活用してみてください。
サイオステクノロジー所属。OSS よろず相談室でサポート対応をしているテクニカルサポートエンジニア。Ansible に出会ってから自動化に取り憑かれ、自身の業務やプライベートであらゆるものの自動化に取り組む。プライベートでは Java でちょっとしたツールの開発を趣味にしている。
Copyright © ITmedia, Inc. All Rights Reserved.