GCPでALBを構築しました。
main.tf
# GCP 外部 Application Load Balancer (グローバル HTTPS) + MIG バックエンド の完全な Terraform 構成例
# (ドメインなし・テスト用:自己署名証明書で HTTPS 対応)
# プロジェクトIDを third-index-273522 に固定した最終版
terraform {
required_version = ">= 1.3"
required_providers {
google = {
source = "hashicorp/google"
version = "~> 7.0"
}
google-beta = {
source = "hashicorp/google-beta"
version = "~> 7.0"
}
tls = {
source = "hashicorp/tls"
version = "~> 4.0"
}
}
}
# 標準 google プロバイダー
provider "google" {
project = ""
region = "us-central1"
}
# google-beta プロバイダー
provider "google-beta" {
project = ""
region = "us-central1"
}
# テスト用自己署名証明書(ブラウザ警告が出ますがテスト用に最適)
resource "tls_private_key" "test" {
algorithm = "RSA"
rsa_bits = 2048
}
resource "tls_self_signed_cert" "test" {
private_key_pem = tls_private_key.test.private_key_pem
subject {
common_name = "test.example.com"
organization = "Test Organization"
}
validity_period_hours = 8760 # 1年有効
allowed_uses = [
"key_encipherment",
"digital_signature",
"server_auth",
]
}
resource "google_compute_ssl_certificate" "test" {
name = "test-self-signed-cert"
private_key = tls_private_key.test.private_key_pem
certificate = tls_self_signed_cert.test.cert_pem
}
# インスタンステンプレート(シンプルな Apache サーバー)
resource "google_compute_instance_template" "app" {
name_prefix = "app-template-"
machine_type = "e2-medium"
tags = ["http-server", "https-server"]
disk {
source_image = "debian-cloud/debian-12"
auto_delete = true
boot = true
}
network_interface {
network = "default"
access_config {}
}
metadata_startup_script = <<-EOF
#!/bin/bash
apt-get update -y
apt-get install -y apache2
echo "<h1>Welcome from $(hostname) via Global External Application Load Balancer!</h1><p>Project: third-index-273522</p>" > /var/www/html/index.html
systemctl restart apache2
EOF
}
# ヘルスチェック
resource "google_compute_health_check" "app" {
name = "app-health-check"
http_health_check {
port = 80
request_path = "/"
}
timeout_sec = 5
check_interval_sec = 5
healthy_threshold = 2
unhealthy_threshold = 10
}
# リージョナル MIG(マルチゾーン対応)
resource "google_compute_region_instance_group_manager" "app" {
name = "app-mig"
region = "us-central1"
base_instance_name = "app-instance"
version {
instance_template = google_compute_instance_template.app.self_link
}
target_size = 3
auto_healing_policies {
health_check = google_compute_health_check.app.self_link
initial_delay_sec = 300
}
update_policy {
type = "PROACTIVE"
minimal_action = "REPLACE"
max_surge_fixed = 3
replacement_method = "SUBSTITUTE"
}
}
# 外部 Application Load Balancer(最新モジュール使用)
module "alb_http" {
source = "GoogleCloudPlatform/lb-http/google"
version = "~> 14.1"
providers = {
google = google-beta
}
project = "third-index-273522"
name = "app-global-alb-test"
load_balancing_scheme = "EXTERNAL_MANAGED"
ssl = true
https_redirect = true
ssl_certificates = [google_compute_ssl_certificate.test.self_link]
backends = {
default = {
protocol = "HTTP"
port = 80
port_name = "http"
timeout_sec = 30
enable_cdn = false # 明示的に指定(これで null エラー解消)
health_check = {
check_interval_sec = 5
timeout_sec = 5
healthy_threshold = 2
unhealthy_threshold = 10
request_path = "/"
port = 80
}
log_config = {
enable = true
sample_rate = 1.0
}
groups = [
{
group = google_compute_region_instance_group_manager.app.instance_group
}
]
iap_config = {
enable = false
}
}
}
}
# 出力
output "load_balancer_global_ip" {
description = "グローバル外部ロードバランサーのIPアドレス(ブラウザで直接アクセス可能)"
value = module.alb_http.external_ip
}
output "access_guide" {
description = "構築後のアクセス方法"
value = <<EOT
【構築完了後の確認手順】
1. terraform apply 完了後、この出力の load_balancer_global_ip を確認
2. ブラウザでアクセス:
- HTTP: http://<IPアドレス>
- HTTPS: https://<IPアドレス>
※自己署名証明書のため「安全ではない接続」の警告が出ます
→ Chrome: 「詳細設定」→「<IP> に移動(安全ではない)」
→ Firefox: 「危険を承知で続行」
3. 正しく Apache のウェルカムページが表示されれば成功です!
【クリーンアップ(不要になったら)】
terraform destroy
お疲れ様でした!GCP のグローバル Application Load Balancer が動作しています!
EOT
}