-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathalb.tf
146 lines (124 loc) · 3.02 KB
/
alb.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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# ALBの定義
resource "aws_lb" "example" {
name = "example"
load_balancer_type = "application"
internal = false # インターネット向け
idle_timeout = 60
enable_deletion_protection = true # 削除保護
# クロスゾーン負荷分散 https://dev.classmethod.jp/articles/elb_crosszone_load_balancing_default_value/
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, # 80
module.https_sg.security_group_id, # 443
module.http_redirect_sg.security_group_id, # 8080
]
}
output "alb_dns_name" {
value = aws_lb.example.dns_name
}
module "http_sg" {
source = "./security_group"
name = "http-sg"
vpc_id = aws_vpc.example.id
port = 80
cidr_blocks = ["0.0.0.0/0"]
}
module "https_sg" {
source = "./security_group"
name = "https-sg"
vpc_id = aws_vpc.example.id
port = 443
cidr_blocks = ["0.0.0.0/0"]
}
module "http_redirect_sg" {
source = "./security_group"
name = "http-redirect-sg"
vpc_id = aws_vpc.example.id
port = 8080
cidr_blocks = ["0.0.0.0/0"]
}
# HTTPリスナー
resource "aws_lb_listener" "http" {
load_balancer_arn = aws_lb.example.arn
port = "80"
protocol = "HTTP"
default_action {
type = "fixed-response" # 固定のHTTPレスポンスに応答
fixed_response {
content_type = "text/plain"
message_body = "これは『HTTP』です"
status_code = "200"
}
}
}
# HTTPSリスナー
resource "aws_lb_listener" "https" {
load_balancer_arn = aws_lb.example.arn
port = "443"
protocol = "HTTPS"
certificate_arn = aws_acm_certificate.example.arn
ssl_policy = "ELBSecurityPolicy-2016-08"
default_action {
type = "fixed-response"
fixed_response {
content_type = "text/plain"
message_body = "これは『HTTPS』です"
status_code = "200"
}
}
}
# HTTPリダイレクトリスナー
resource "aws_lb_listener" "redirect_http_to_https" {
load_balancer_arn = alb.example.arn
port = "8080"
protocol = "HTTP"
default_action {
type = "redirect"
redirect {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
}
# ターゲットグループ
resource "aws_lb_target_group" "example" {
name = "example"
target_type = "ip"
vpc_id = aws_vpc.example.id
port = 80
protocol = "HTTP"
deregistration_delay = 300
health_check {
path = "/"
healthy_threshold = 5
unhealthy_threshold = 2
timeout = 5
interval = 30
matcher = 200
port = "traffic-port" # 80
protocol = "HTTP"
}
depends_on = [aws_lb.example]
}
# ターゲットグループにリクエストを転送するリスナールール
resource "aws_lb_listener_rule" "example" {
listener_arn = aws_lb_listener.https.arn
priority = 100
action {
type = "forward"
target_group_arn = aws_lb_target_group.example.arn
}
# パスベースルーティング
condition {
field = "path-pattern"
values = ["/*"]
}
}