尝试使用Terraform构建内部应用程序负载平衡器

时间:2019-01-03 07:09:12

标签: amazon-web-services terraform

我刚刚开始使用Terraforms。我正在尝试使用Terraform创建内部负载平衡器。有人可以指导我如何执行此操作,因为当我运行terraform脚本时,会抛出错误消息,表明需要在VPC内创建内部负载平衡器。我明白了,因为内部负载均衡器将有一个专用子网。创建内部负载均衡器时如何提供VPC ID?

provider "aws" {
region = "ap-south-1"
}

module "vpc" {
 source = "terraform-aws-modules/vpc/aws"

   provider "aws" {
  region = "ap-south-1"
  }

  module "vpc" {
   source = "terraform-aws-modules/vpc/aws"

   name = "my-vpc"
   cidr = "10.0.0.0/16"

   azs             = ["ap-south-1a", "ap-south-1b"]
   private_subnets = ["10.0.1.0/24", "10.0.2.0/24"]

   enable_nat_gateway = true
   enable_vpn_gateway = true

   tags = {
     Terraform = "true"
     Environment = "dev"
   }
  }

  resource "aws_elb" "bar" {
   name               = "foobar-terraform-elb"
   availability_zones = ["ap-south-1"]
   internal = true

   access_logs {
     bucket        = "foo"
     bucket_prefix = "bar"
     interval      = 60
   }

   listener {
     instance_port     = 8000
     instance_protocol = "http"
     lb_port           = 80
     lb_protocol       = "http"
   }

   listener {
     instance_port      = 8000
     instance_protocol  = "http"
     lb_port            = 443
     lb_protocol        = "https"
     ssl_certificate_id = "arn:aws:iam::123456789012:server-certificate/certName"
   }

   health_check {
     healthy_threshold   = 2
     unhealthy_threshold = 2
     timeout             = 3
     target              = "HTTP:8000/"
     interval            = 30
   }

   instances                   = ["i-xxxxxxxxxxxxxxx"]
   cross_zone_load_balancing   = true
   idle_timeout                = 400
   connection_draining         = true
   connection_draining_timeout = 400

   tags = {
     Name = "foobar-terraform-elb"
   }
  }

2 个答案:

答案 0 :(得分:1)

私有子网是VPC的一部分,并且terraform期望在VPC中创建ELB的子网列表。 请参阅:https://www.terraform.io/docs/providers/aws/r/elb.html#subnets

您可以将您的aws_elb代码更新为

resource "aws_elb" "bar" {
   name               = "foobar-terraform-elb"
   subnets = ["subnet-1-id"]

   # If you need multiple subnets then provide value as follow
   # subnets = ["subnet-1-id", "subnet-2-id"]

   internal = true

   access_logs {
     bucket        = "foo"
     bucket_prefix = "bar"
     interval      = 60
   }

   listener {
     instance_port     = 8000
     instance_protocol = "http"
     lb_port           = 80
     lb_protocol       = "http"
   }

   listener {
     instance_port      = 8000
     instance_protocol  = "http"
     lb_port            = 443
     lb_protocol        = "https"
     ssl_certificate_id = "arn:aws:iam::123456789012:server-certificate/certName"
   }

   health_check {
     healthy_threshold   = 2
     unhealthy_threshold = 2
     timeout             = 3
     target              = "HTTP:8000/"
     interval            = 30
   }

   instances                   = ["i-xxxxxxxxxxxxxxx"]
   cross_zone_load_balancing   = true
   idle_timeout                = 400
   connection_draining         = true
   connection_draining_timeout = 400

   tags = {
     Name = "foobar-terraform-elb"
   }
  }

希望这会有所帮助

答案 1 :(得分:0)

您需要使用Terraform提供的subnets参数来创建内部负载均衡器。

在您的情况下可能是

resource "aws_elb" "bar" {
   ...
   subnets = "${module.vpc.private_subnets}"
   ...
}