実践Terraform ch8 ロードバランサーとDNS (1/2)

Terraform勉強メモ

出典: 


[https://github.com/wand2016/terraformch8example:embed:cite]

HTTP用ロードバランサー

resource "aws_lb" "example" {
  name = "example"
  load_balancer_type = "application"
  internal = false
  idle_timeout = 60
  enable_deletion_protection = true

  subnets = [
    aws_subnet.public_0.id,
    aws_subnet.public_1.id
  ]

  access_logs {
    bucket = aws_s3_bucket.alb_log.id
    enabled = true
  }

  security_groups = [
    module.http_sg.security_group_id,
    module.https_sg.security_group_id,
    module.https_redirect_sg.security_group_id
  ]
}

20200308101012

subnets = [
  aws_subnet.public_0.id,
  aws_subnet.public_1.id
]
  • AZの異なる2つ以上のサブネットを指定しないとエラーになる
Error: Error creating application Load Balancer: ValidationError: At least two subnets in two different Availability Zones must be specified
	status code: 400, request id: cac1ce58-dd9b-47e6-a650-3fbc9e5cd436
  • アクセスログ
access_logs {
  bucket = aws_s3_bucket.alb_log.id
  enabled = true
}
  • セキュリティグループ
security_groups = [
  module.http_sg.security_group_id,
  module.https_sg.security_group_id,
  module.https_redirect_sg.security_group_id
]

20200308101119

20200308101312

リスナー

resource "aws_lb_listener" "http" {
  load_balancer_arn = aws_lb.example.arn
  port = "80"
  protocol = "HTTP"

  default_action {
    type = "fixed-response"

    fixed_response {
      content_type = "application/json"
      message_body = "{\"message\":\"これは『HTTP』です\"}"
      status_code = "200"
    }
  }
}

output "public_dns" {
  value = aws_lb.example.dns_name
}
docker-compose run terraform apply
...
public_dns = example-870645885.ap-northeast-1.elb.amazonaws.com
curl example-870645885.ap-northeast-1.elb.amazonaws.com
{"message":"これは『HTTP』です"}
  • protocol = "HTTP"

    • ALBではHTTP/HTTP
  • port = "80"

    • リッスンするポート
    • HTTPなので80
default_action {
  type = "fixed-response"

  fixed_response {
    content_type = "application/json"
    message_body = "{\"message\":\"これは『HTTP』です\"}"
    status_code = "200"
  }
}
  • デフォルトアクション

    • 公式/ルール
    • 認証系

      • authenticate-cognito
      • authenticate-oidc
    • fixed-response
    • 転送系

      • forward

        • 別のターゲットグループへ転送
      • redirect

        • 別のURLへ転送

【追記】terraform destroyでハング

20200308103256

  • still destroying…のまま永久に返ってこない
  • ALBの削除保護が有効になっていると、エラーになることもなくハングする模様

20200308103323

20200308103331

  • ALBの削除保護を無効にして再試行すると、無事削除できた
  • ↓現象を確認したバージョン
Terraform v0.12.21
+ provider.aws v2.52.0