前回のブログではRDSとS3をterraform化してみたので、
今回はALB(Application Load Balancer)をterraform化してみました。
■aws_acm.tf
1 2 3 4 5 6 7 8 9 10 11 12 13 |
resource "aws_acm_certificate" "wild-adachin-jp" { domain_name = "adachin.jp" subject_alternative_names = ["*.adachin.jp"] validation_method = "DNS" tags = { Environment = "pre" } lifecycle { create_before_destroy = true } } |
■aws_alb.tf
・terraform version
1 2 |
$ terraform -v Terraform v0.9.11 |
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 |
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 = "pre" } } 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.php" 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_alb_listener" "adachin-app-https" { load_balancer_arn = aws_lb.adachin-app.arn port = "443" protocol = "HTTPS" ssl_policy = "ELBSecurityPolicy-2016-08" certificate_arn = aws_acm_certificate.wild-adachin-jp.arn default_action { target_group_arn = aws_lb_target_group.adachin-app.arn type = "forward" } } |
■まとめ
ELBは今後アップデートないので、ALBを使おう!
参考
https://www.terraform.io/docs/providers/aws/r/alb.html
0件のコメント