如何通过 terraform 将 alb 注册为目标组的目标 [英] How to register alb as target for a targetgroup via terraform

查看:35
本文介绍了如何通过 terraform 将 alb 注册为目标组的目标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个目标群体,想通过 terraform 将 alb 注册为目标群体的目标.https://aws.amazon.com/blogs/networking-and-content-delivery/application-load-balancer-type-target-group-for-network-load-balancer/我有点找不到与此相关的文档.有人可以在这里帮忙吗?

I have an target group and want to register alb as target for the target group via terraform. https://aws.amazon.com/blogs/networking-and-content-delivery/application-load-balancer-type-target-group-for-network-load-balancer/ I am sort of unable to find documentation related to that. Can anyone kindly help here?

推荐答案

Terraform 文档尚未完全更新以反映新的 ALB 作为 NLB 功能的目标组.不过,您应该能够通过查看 AWS API 参考和 terraform 文档来执行类似于以下代码片段的操作:

Terraform documentation hasn't been fully updated to reflect the new ALB as a target group for NLB feature. Nevertheless, you should be able to do something similar to the code snippet below by looking at the AWS API reference and terraform docs:

resource "aws_vpc" "main" {
    cidr_block = "10.0.0.0/16"
}

# Create ALB
resource "aws_lb" "alb" {
    name               = "test-alb-tf"
    internal           = false
    load_balancer_type = "application"
    security_groups    = [aws_security_group.lb_sg.id]
    subnets            = aws_subnet.public.*.id
}

# Create ALB target group
resource "aws_lb_target_group" "alb_tg" {
    name     = "tf-example-lb-tg"
    port     = 80
    protocol = "HTTP"
    vpc_id   = aws_vpc.main.id
}

# Create NLB
resource "aws_lb" "nlb" {
    name               = "test-nlb-tf"
    internal           = false
    load_balancer_type = "network"
    subnets            = aws_subnet.public.*.id
}

# Create NLB target group that forwards traffic to alb
# https://docs.aws.amazon.com/elasticloadbalancing/latest/APIReference/API_CreateTargetGroup.html
resource "aws_lb_target_group" "nlb_tg" {
    name         = "tf-example-nlb-tg"
    port         = 80
    protocol     = "TCP"
    vpc_id       = aws_vpc.main.id
    target_type  = "alb"
}

# Create target group attachment
# More details: https://docs.aws.amazon.com/elasticloadbalancing/latest/APIReference/API_TargetDescription.html
# https://docs.aws.amazon.com/elasticloadbalancing/latest/APIReference/API_RegisterTargets.html
resource "aws_lb_target_group_attachment" "tg_attachment" {
    target_group_arn = aws_lb_target_group.nlb_tg.arn
    # target to attach to this target group
    target_id        = aws_lb_target_group.alb_tg.arn
    #  If the target type is alb, the targeted Application Load Balancer must have at least one listener whose port matches the target group port.
    port             = 80
}

这篇关于如何通过 terraform 将 alb 注册为目标组的目标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆