出力の定義
ここでは、作成したリソースのIDやIPアドレスなどの情報をterraform applyの実行結果に表示するための出力(output)を定義します。Terraformでは、outputブロックを使って出力値を定義します。
1. コードの記載
ここでは、作成したリソースのIDやIPアドレスなどの情報をterraform applyの実行結果に表示するための出力(output)を定義します。Terraformでは、outputブロックを使って出力値を定義します。以下の内容をmain.tfに追記してください。
# ====================
# 出力
# ====================
output "vpc_id" {
description = "VPC ID"
value = aws_vpc.main.id
}
output "public_subnet_id" {
description = "Public Subnet ID"
value = aws_subnet.public.id
}
output "ec2_public_ip" {
description = "EC2 Public IP"
value = aws_instance.web.public_ip
}
output "ec2_instance_id" {
description = "EC2 Instance ID"
value = aws_instance.web.id
}
2. 差分の確認
terraform planで差分を確認します。
terraform plan
以下のような実行結果が表示されます。
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.web will be created
+ resource "aws_instance" "web" {
+ ami = "ami-088b486f20fab3f0e"
+ 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_lifecycle = (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
+ spot_instance_request_id = (known after apply)
+ subnet_id = "subnet-xxxxxxxxxxxxxxxxx"
+ tags = {
+ "Name" = "terraform-ec2"
}
+ tags_all = {
+ "Name" = "terraform-ec2"
}
+ tenancy = (known after apply)
+ user_data = (known after apply)
+ user_data_base64 = (known after apply)
+ user_data_replace_on_destroy = false
+ vpc_security_group_ids = (known after apply)
}
Plan: 1 to add, 0 to change, 0 to destroy.
Changes to Outputs:
+ ec2_instance_id = (known after apply)
+ ec2_public_ip = (known after apply)
+ public_subnet_id = "subnet-xxxxxxxxxxxxxxxxx"
+ vpc_id = "vpc-xxxxxxxxxxxxxxxxx"
EC2インスタンスが1つ作成されることがわかります。amiにはAmazon Linux 2023のAMI IDが、instance_typeにはt2.microが、subnet_idにはパブリックサブネットのIDが設定されていることを確認してください。末尾のChanges to Outputsには、outputで定義した出力値が表示されています。
3. AWSへの反映
問題なければterraform applyで反映します。
terraform apply
確認プロンプトが表示されたら、yesと入力して実行します。以下のように、EC2インスタンスが1件作成され、定義した出力が表示されることを確認します。
Apply complete! Resources: 1 added, 0 changed, 0 destroyed.
Outputs:
ec2_instance_id = "i-xxxxxxxxxxxxxxxxx"
ec2_public_ip = "xx.xx.xx.xx"
public_subnet_id = "subnet-xxxxxxxxxxxxxxxxx"
vpc_id = "vpc-xxxxxxxxxxxxxxxxx"
4. AWSコンソールでの確認
AWSマネジメントコンソールで EC2 > インスタンス を開き、「terraform-ec2」が作成されていることを確認してください。インスタンスを選択し、以下の項目がコードで設定した内容と一致していることも確認しましょう。
- AMI が「Amazon Linux 2023」であること
- インスタンスタイプ が「t2.micro」であること
- サブネット が「terraform-public-subnet」であること
- セキュリティグループ が「terraform-ec2-sg」であること
- パブリックIPv4アドレス が割り当てられていること

| 💡 ポイント |
|---|
ec2_public_ipに表示されたIPアドレスを使って、EC2インスタンスに接続できます。ただし、このセクションではキーペアを設定していないため、SSHログインはできません。接続が必要な場合は、EC2 Instance Connectやセッションマネージャーを使用してください。 |
Terraformでも現在の状態を確認できます。terraform showコマンドで、管理しているすべてのリソースの詳細を表示できます。
terraform show
特定の出力だけを確認したい場合は、以下のコマンドを使用します。
terraform output ec2_public_ip