在 AWS 上创建应用程序和网络负载均衡器资源的 Terraform 模块。
具有默认操作的 HTTP 和 HTTPS 侦听器:
module "alb" {
source = "terraform-aws-modules/alb/aws"
version = "~> 6.0"
name = "my-alb"
load_balancer_type = "application"
vpc_id = "vpc-abcde012"
subnets = ["subnet-abcde012", "subnet-bcde012a"]
security_groups = ["sg-edcd9784", "sg-edcd9785"]
access_logs = {
bucket = "my-alb-logs"
}
target_groups = [
{
name_prefix = "pref-"
backend_protocol = "HTTP"
backend_port = 80
target_type = "instance"
targets = {
my_target = {
target_id = "i-0123456789abcdefg"
port = 80
}
my_other_target = {
target_id = "i-a1b2c3d4e5f6g7h8i"
port = 8080
}
}
}
]
https_listeners = [
{
port = 443
protocol = "HTTPS"
certificate_arn = "arn:aws:iam::123456789012:server-certificate/test_cert-123456789012"
target_group_index = 0
}
]
http_tcp_listeners = [
{
port = 80
protocol = "HTTP"
target_group_index = 0
}
]
tags = {
Environment = "Test"
}
}
HTTP 到 HTTPS 重定向和 HTTPS 认知身份验证:
module "alb" {
source = "terraform-aws-modules/alb/aws"
version = "~> 6.0"
name = "my-alb"
load_balancer_type = "application"
vpc_id = "vpc-abcde012"
subnets = ["subnet-abcde012", "subnet-bcde012a"]
security_groups = ["sg-edcd9784", "sg-edcd9785"]
access_logs = {
bucket = "my-alb-logs"
}
target_groups = [
{
name_prefix = "pref-"
backend_protocol = "HTTPS"
backend_port = 443
target_type = "instance"
}
]
https_listeners = [
{
port = 443
protocol = "HTTPS"
certificate_arn = "arn:aws:iam::123456789012:server-certificate/test_cert-123456789012"
action_type = "authenticate-cognito"
target_group_index = 0
authenticate_cognito = {
user_pool_arn = "arn:aws:cognito-idp::123456789012:userpool/test-pool"
user_pool_client_id = "6oRmFiS0JHk="
user_pool_domain = "test-domain-com"
}
}
]
http_tcp_listeners = [
{
port = 80
protocol = "HTTP"
action_type = "redirect"
redirect = {
port = "443"
protocol = "HTTPS"
status_code = "HTTP_301"
}
}
]
tags = {
Environment = "Test"
}
}
仅在特定路由上进行认知身份验证,并为其他路由提供重定向:
module "alb" {
source = "terraform-aws-modules/alb/aws"
version = "~> 6.0"
name = "my-alb"
load_balancer_type = "application"
vpc_id = "vpc-abcde012"
subnets = ["subnet-abcde012", "subnet-bcde012a"]
security_groups = ["sg-edcd9784", "sg-edcd9785"]
access_logs = {
bucket = "my-alb-logs"
}
target_groups = [
{
name_prefix = "default"
backend_protocol = "HTTPS"
backend_port = 443
target_type = "instance"
}
]
https_listeners = [
{
port = 443
certificate_arn = "arn:aws:iam::123456789012:server-certificate/test_cert-123456789012"
}
]
https_listener_rules = [
{
https_listener_index = 0
priority = 5000
actions = [{
type = "redirect"
status_code = "HTTP_302"
host = "www.youtube.com"
path = "/watch"
query = "v=dQw4w9WgXcQ"
protocol = "HTTPS"
}]
conditions = [{
path_patterns = ["/onboarding", "/docs"]
}]
},
{
https_listener_index = 0
priority = 2
actions = [
{
type = "authenticate-cognito"
user_pool_arn = "arn:aws:cognito-idp::123456789012:userpool/test-pool"
user_pool_client_id = "6oRmFiS0JHk="
user_pool_domain = "test-domain-com"
},
{
type = "forward"
target_group_index = 0
}
]
conditions = [{
path_patterns = ["/protected-route", "private/*"]
}]
}
]
}
使用 ALB 侦听器规则时,请确保每个规则的块都以 、 或操作结尾,以便每个规则都解析为某种 HTTP 响应。请查看 AWS 文档以了解更多信息。
actions
forward
redirect
fixed-response
module "nlb" {
source = "terraform-aws-modules/alb/aws"
version = "~> 6.0"
name = "my-nlb"
load_balancer_type = "network"
vpc_id = "vpc-abcde012"
subnets = ["subnet-abcde012", "subnet-bcde012a"]
access_logs = {
bucket = "my-nlb-logs"
}
target_groups = [
{
name_prefix = "pref-"
backend_protocol = "TCP"
backend_port = 80
target_type = "ip"
}
]
https_listeners = [
{
port = 443
protocol = "TLS"
certificate_arn = "arn:aws:iam::123456789012:server-certificate/test_cert-123456789012"
target_group_index = 0
}
]
http_tcp_listeners = [
{
port = 80
protocol = "TCP"
target_group_index = 0
}
]
tags = {
Environment = "Test"
}
}
建议你将此模块与 terraform-aws-vpc、terraform-aws-security-group 和 terraform-aws-autoscaling 一起使用。
access_logs = { bucket = "my-already-created-bucket-for-logs" }
access_logs = { bucket = module.log_bucket.s3_bucket_id }
有时你需要有一种方法可以有条件地创建ALB资源,但Terraform不允许使用内部块,所以解决方案是指定参数 。
count
module
create_lb
# This LB will not be created
module "lb" {
source = "terraform-aws-modules/alb/aws"
create_lb = false
# ... omitted
}
名字 | 版本 |
---|---|
地球化 | >= 1.0.0 |
aws | >= 3.67 |
名字 | 版本 |
---|---|
aws | >= 3.67 |
无模块。
名字 | 描述 | 类型 | 违约 | 必填 |
---|---|---|---|---|
access_logs | 包含负载均衡器的访问日志记录配置的映射。 | map(string) |
{} |
不 |
create_lb | 控制是否应创建负载均衡器 | bool |
true |
不 |
desync_mitigation_mode | 确定负载均衡器如何处理可能由于 HTTP 取消同步而对应用程序构成安全风险的请求。 | string |
"defensive" |
不 |
drop_invalid_header_fields | 指示是否在应用程序负载均衡器中删除无效标头字段。默认值为 false。 | bool |
false |
不 |
enable_cross_zone_load_balancing | 指示是否应在应用程序负载均衡器中启用跨区域负载均衡。 | bool |
false |
不 |
enable_deletion_protection | 如果为 true,则将通过 AWS API 禁用负载均衡器的删除。这将阻止 Terraform 删除负载均衡器。默认值为 false。 | bool |
false |
不 |
enable_http2 | 指示是否在应用程序负载均衡器中启用了 HTTP/2。 | bool |
true |
不 |
enable_waf_fail_open | 指示在 lb 无法将请求转发到 AWS WAF 时是否将请求路由到目标 | bool |
false |
不 |
extra_ssl_certs | 描述要应用于 HTTPS 侦听器的任何额外 SSL 证书的映射列表。必需的键/值:certificate_arn、https_listener_index(证书适用的侦听器在https_listeners的索引)。 | list(map(string)) |
[] |
不 |
http_tcp_listener_rules | 描述此 ALB 的侦听器规则的映射列表。必需的键/值:操作、条件。可选键/值:优先级、http_tcp_listener_index(默认为 http_tcp_listeners[count.index]) | any |
[] |
不 |
http_tcp_listener_rules_tags | 要添加到所有 http 侦听器规则的标记映射 | map(string) |
{} |
不 |
http_tcp_listeners | 描述此 ALB 的 HTTP 侦听器或 TCP 端口的映射列表。所需的键/值:端口、协议。可选键/值:target_group_index(默认为 http_tcp_listeners[count.index]) | any |
[] |
不 |
http_tcp_listeners_tags | 要添加到所有 http 侦听器的标记映射 | map(string) |
{} |
不 |
https_listener_rules | 描述此 ALB 的侦听器规则的映射列表。必需的键/值:操作、条件。可选键/值:优先级、https_listener_index(默认为 https_listeners[count.index]) | any |
[] |
不 |
https_listener_rules_tags | 要添加到所有 https 侦听器规则的标记映射 | map(string) |
{} |
不 |
https_listeners | 描述此 ALB 的 HTTPS 侦听器的映射列表。所需的键/值:端口、certificate_arn。可选键/值:ssl_policy(默认为 ELBSecurityPolicy-2016-08)、target_group_index(默认为 https_listeners[count.index]) | any |
[] |
不 |
https_listeners_tags | 要添加到所有 https 侦听器的标记映射 | map(string) |
{} |
不 |
idle_timeout | 允许连接处于空闲状态的时间(以秒为单位)。 | number |
60 |
不 |
内部 | 确定负载均衡器是面向内部还是面向外部的布尔值。 | bool |
false |
不 |
ip_address_type | 子网用于负载均衡器的 IP 地址类型。可能的值为 ipv4 和 dualstack。 | string |
"ipv4" |
不 |
lb_tags | 要添加到负载均衡器的标记映射 | map(string) |
{} |
不 |
listener_ssl_policy_default | 如果在负载均衡器上外部使用 HTTPS,则为安全策略。看。 | string |
"ELBSecurityPolicy-2016-08" |
不 |
load_balancer_create_timeout | 创建 ALB 时的超时值。 | string |
"10m" |
不 |
load_balancer_delete_timeout | 删除 ALB 时的超时值。 | string |
"10m" |
不 |
load_balancer_type | 要创建的负载均衡器的类型。可能的值是应用程序或网络。 | string |
"application" |
不 |
load_balancer_update_timeout | 更新 ALB 时的超时值。 | string |
"10m" |
不 |
名字 | 负载均衡器的资源名称和名称标记。 | string |
null |
不 |
name_prefix | 负载均衡器的资源名称前缀和名称标记。不能超过 6 个字符 | string |
null |
不 |
putin_khuylo | 你同意普京不尊重乌克兰的主权和领土完整吗?更多信息:https://en.wikipedia.org/wiki/Putin_khuylo! | bool |
true |
不 |
security_groups | 要附加到负载均衡器的安全组。例如 [“sg-edcd9784”,“sg-edcd9785”] | list(string) |
[] |
不 |
subnet_mapping | 描述要附加到网络负载均衡器的子网的子网映射块列表 | list(map(string)) |
[] |
不 |
子网 | 要与负载均衡器关联的子网的列表。例如 ['subnet-1a2b3c4d','subnet-1a2b3c4e','subnet-1a2b3c4f'] | list(string) |
null |
不 |
标签 | 要添加到所有资源的标签 map | map(string) |
{} |
不 |
target_group_tags | 要添加到所有目标组的标记映射 | map(string) |
{} |
不 |
target_groups | 包含键/值对的映射列表,这些键/值对定义要创建的目标组。这些映射的顺序很重要,这些映射的索引将在侦听器定义中引用。必需的键/值:名称、backend_protocol、backend_port | any |
[] |
不 |
vpc_id | 将在其中部署负载均衡器和其他资源的 VPC ID。 | string |
null |
不 |
名字 | 描述 |
---|---|
http_tcp_listener_arns | 创建的 TCP 和 HTTP 负载均衡器侦听器的 ARN。 |
http_tcp_listener_ids | 创建的 TCP 和 HTTP 负载平衡器侦听器的 ID。 |
https_listener_arns | 创建的 HTTPS 负载均衡器侦听器的 ARN。 |
https_listener_ids | 创建的负载均衡器侦听器的 ID。 |
lb_arn | 我们创建的负载均衡器的 ID 和 ARN。 |
lb_arn_suffix | 负载均衡器的 ARN 后缀 - 可与 CloudWatch 一起使用。 |
lb_dns_name | 负载均衡器的 DNS 名称。 |
lb_id | 我们创建的负载均衡器的 ID 和 ARN。 |
lb_zone_id | 负载均衡器zone_id,以帮助创建 DNS 记录。 |
target_group_arn_suffixes | 目标群体的 ARN 后缀 - 可与 CloudWatch 一起使用。 |
target_group_arns | 目标群体的 ARN。用于传递到你的 Auto Scaling 组。 |
target_group_attachments | 目标组附件 ID 的 ARN。 |
target_group_names | 目标组的名称。用于传递到 CodeDeploy 部署组。 |
模块由Anton Babenko在这些出色的贡献者的帮助下维护。
Apache 2 Licensed.有关完整详细信息,请参阅许可证。