AWSのロードバランサー(ALB)でリダイレクトができるブログを以前書いたと思いますが、今回リスナーのルールにTerraformで追加したことがなかったので、試しに以下のように書いたところ!
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | resource "aws_alb_listener" "adachin-app" {   load_balancer_arn = aws_lb.adachin-app.arn   port              = "80"   protocol          = "HTTP"   default_action {     type = "redirect"     redirect {       port        = "443"       protocol    = "HTTPS"       status_code = "HTTP_301"     }   }   default_action {     type = "redirect"     redirect {       host        = "adachin.com"       port        = "443"       protocol    = "HTTPS"       status_code = "HTTP_301"     }   } } | 
Error: Error modifying LB Listener: InvalidLoadBalancerAction: You cannot specify multiple of the following action type: ‘redirect’
status code: 400, request id: xxxxxxxxxxxxxxxxx
on elb.tf line 34, in resource “aws_alb_listener” “adachin-app”:
34: resource “aws_alb_listener” “adachin-app” {
次のアクションタイプを複数指定することはできません: ‘redirect’
そもそも、リスナーのIFのホストヘッダーがterraformの項目にないし、redirectを複数書けないとエラーが出たので、これはコード化できないのか…と思いきや aws_lb_listener_rule を使って実装できたのでブログします。見てるところが違った!
■やりたいこと
- http,https共に www.adachin.comにアクセスしたらadachin.comにリダイレクトする
■Resource: aws_lb_listener_rule
https://www.terraform.io/docs/providers/aws/r/lb_listener_rule.html
arnでlistenerと紐つけるだけなので、複雑な処理も余裕で書ける!
■elb.tf
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | resource "aws_lb" "adachin-app" {   name                       = "adachin-app"   internal                   = false   load_balancer_type         = "application"   security_groups            = ["${aws_security_group.adachin-app-alb.id}"]   subnets                    = ["${aws_subnet.adachin-public-1a.id}", "${aws_subnet.adachin-public-1c.id}", "${aws_subnet.adachin-public-1d.id}"]   enable_deletion_protection = true   tags = {     Env = "prd"   } } resource "aws_lb_target_group" "adachin-app" {   name                 = "adachin-app"   port                 = 80   protocol             = "HTTP"   vpc_id               = aws_vpc.adachin-vpc.id   target_type          = "ip"   deregistration_delay = "10"   health_check {     protocol            = "HTTP"     path                = "/ping"     port                = 80     healthy_threshold   = 5     unhealthy_threshold = 2     timeout             = 5     interval            = 10     matcher             = 200   } } resource "aws_alb_listener" "adachin-app" {   load_balancer_arn = aws_lb.adachin-app.arn   port              = "80"   protocol          = "HTTP"   default_action {     type = "redirect"     redirect {       port        = "443"       protocol    = "HTTPS"       status_code = "HTTP_301"     }   } } resource "aws_lb_listener_rule" "redirect_www" {   listener_arn = aws_alb_listener.adachin-app.arn   action {     type = "redirect"     redirect {       host        = "adachin.com"       port        = "443"       protocol    = "HTTPS"       status_code = "HTTP_301"     }   }   condition {     host_header {       values  = ["www.adachin.com"]     }   } } resource "aws_alb_listener" "adachin-app-https" {   load_balancer_arn = aws_lb.adachin-app.arn   port              = "443"   protocol          = "HTTPS"   ssl_policy        = "ELBSecurityPolicy-2015-05"   certificate_arn   = aws_acm_certificate.adachin-com.arn   default_action {     target_group_arn = aws_lb_target_group.adachin-app.arn     type             = "forward"   } } resource "aws_lb_listener_rule" "redirect_www_https" {   listener_arn = aws_alb_listener.adachin-app-https.arn   action {     type = "redirect"     redirect {       host        = "adachin.com"       port        = "443"       protocol    = "HTTPS"       status_code = "HTTP_301"     }   }   condition {     host_header {       values   = ["www.adachin.com"]     }   } } | 
今回追加したのは 50,84行目 でhttpとhttpsの2つに追加しています。しかし host_header は最後に追加するんですな。コンソールだと一番左(host_header)に追加するので慣れない。 conditionをredirectの上に持ってくれば見やすそうだけど、動くのか…(検証してない)
- 確認
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |  $ curl -I www.adachin.com HTTP/1.1 301 Moved Temporarily Server: awselb/2.0 Date: Fri, 17 Apr 2020 04:43:38 GMT Content-Type: text/html Content-Length: 126 Connection: keep-alive Location: https://adachin.com:443/  $ curl -I https://www.adachin.com HTTP/1.1 301 Moved Temporarily Server: awselb/2.0 Date: Fri, 17 Apr 2020 04:43:38 GMT Content-Type: text/html Content-Length: 126 Connection: keep-alive Location: https://adachin.com:443/ | 
■まとめ
Nginxに余計な設定は書かずにALBで管理できるのは非常に良き!サブドメインくらいのリダイレクトならバシバシALBでやりましょう!危うく手動で管理するところだった。。

 
													 
													 
													
0件のコメント