Terraform-如何为AWS应用程序负载平衡器引用子网?

时间:2019-08-14 15:48:43

标签: amazon-web-services terraform

我需要在自定义VPS中引用所有可用的子网ID。

我有一组单独的模块用于构建AWS基础架构: VPC, 子网 互联网网关, ALB

我使用S3作为支持的地形。当我尝试使用ALB模块时,我必须为ALB提供子网。 VPC和所有子网都是在单独的地形运行中创建的。

这是我的子网模块:

resource "aws_subnet" "main" {
  count = length(data.aws_availability_zones.available.names)
  cidr_block = "10.0.${count.index}.0/24"
  vpc_id = var.vpc_id
  map_public_ip_on_launch = true
  availability_zone = data.aws_availability_zones.available.names[count.index]
  tags = {
    Name = "public-${element(data.aws_availability_zones.available.names, count.index)}"
  }
}

这是alb模块:

# Create a security group for the Application Load Balancer, open port: 80.
resource "aws_security_group" "alb-security-group" {
  name        = "alb-security-group"
  description = "Application Load Balancer security group"
  vpc_id = var.vpc_id

  ingress {
    from_port   = 80
    to_port     = 80
    protocol    = "tcp"
    cidr_blocks = [var.allowed_cidr_blocks]
  }

  egress {
    from_port   = 0
    to_port     = 0
    protocol    = "-1"
    cidr_blocks = ["0.0.0.0/0"]
  }
}

# Create the Application Load Balancer
resource "aws_alb" "application-load-balancer" {
  name            = "application-load-balancer"
  security_groups = [aws_security_group.alb-security-group.id]


  subnets = [var.alb_subnets]
  tags = {
    Name = "application_load_balancer"
  }
}

# Create a target group and define stickiness
resource "aws_alb_target_group" "alb-target-group" {
  name     = "alb-target-group"
  port     = 80
  protocol = "HTTP"
  vpc_id   = var.vpc_id
  stickiness {
    # Enable stickiness
    type = "lb_cookie"
    # Set cookie duration to one hour (3600s)
    cookie_duration = 3600
  }

  # ALB will look for health_check file to determine instances health
  health_check {
    path = "/health_check"
    port = 80
  }
}

resource "aws_alb_listener" "application-load-balancer-listener-http" {
  load_balancer_arn = aws_alb.application-load-balancer.arn
  port              = "80"
  protocol          = "HTTP"

  default_action {
    target_group_arn = aws_alb_target_group.alb-target-group.arn
    type             = "forward"
  }
}

如果这将全部作为一个“模块”,我将简单地这样做:

subnets = aws_subnet.main.*.id

但不是。

0 个答案:

没有答案