无法使用Terraform在AWS自动缩放组中的多个区域中创建实例

时间:2018-09-24 06:33:36

标签: amazon-web-services amazon-ec2 terraform

在我的应用程序中,我正在使用Terraform使用AWS自动缩放组。我启动了一个Autoscaling组,为它提供了一个区域中的许多实例。但是,由于一个区域只允许20个实例。我想启动一个自动伸缩组,该组将在多个区域中创建实例,以便可以启动多个实例。我有此配置:

# ---------------------------------------------------------------------------------------------------------------------
# THESE TEMPLATES REQUIRE TERRAFORM VERSION 0.8 AND ABOVE
# ---------------------------------------------------------------------------------------------------------------------

terraform {
  required_version = ">= 0.9.3"
}

provider "aws" {
  access_key = "${var.aws_access_key}"
  secret_key = "${var.aws_secret_key}"
  region     = "us-east-1"
}

provider "aws" {
  alias  = "us-west-1"
  region = "us-west-1"
}

provider "aws" {
  alias  = "us-west-2"
  region = "us-west-2"
}

provider "aws" {
  alias  = "eu-west-1"
  region = "eu-west-1"
}

provider "aws" {
  alias = "eu-central-1"
  region = "eu-central-1"
}

provider "aws" {
  alias = "ap-southeast-1"
  region = "ap-southeast-1"
}

provider "aws" {
  alias = "ap-southeast-2"
  region = "ap-southeast-2"
}

provider "aws" {
  alias = "ap-northeast-1"
  region = "ap-northeast-1"
}

provider "aws" {
  alias = "sa-east-1"
  region = "sa-east-1"
}

resource "aws_launch_configuration" "launch_configuration" {
  name_prefix = "${var.asg_name}-"
  image_id = "${var.ami_id}"
  instance_type = "${var.instance_type}"
  associate_public_ip_address = true
  key_name = "${var.key_name}"
  security_groups = ["${var.security_group_id}"]
  user_data = "${data.template_file.user_data_client.rendered}"

  lifecycle {
    create_before_destroy = true
  }
}

# ---------------------------------------------------------------------------------------------------------------------
# CREATE AN AUTO SCALING GROUP (ASG)
# ---------------------------------------------------------------------------------------------------------------------

resource "aws_autoscaling_group" "autoscaling_group" {
  name = "${var.asg_name}"
  max_size = "${var.max_size}"
  min_size = "${var.min_size}"
  desired_capacity = "${var.desired_capacity}"
  launch_configuration = "${aws_launch_configuration.launch_configuration.name}"
  vpc_zone_identifier = ["${data.aws_subnet_ids.default.ids}"]

  lifecycle {
    create_before_destroy = true
  }

  tag {
    key = "Environment"
    value = "production"
    propagate_at_launch = true
  }

  tag {
    key = "Name"
    value = "clj-${var.job_id}-instance"
    propagate_at_launch = true
  }
}

# ---------------------------------------------------------------------------------------------------------------------
# THE USER DATA SCRIPT THAT WILL RUN ON EACH CLIENT NODE WHEN IT'S BOOTING
# ---------------------------------------------------------------------------------------------------------------------

data "template_file" "user_data_client" {
  template = "${file("./user-data-client.sh")}"

  vars {
    company_location_job_id   = "${var.job_id}"
    docker_login_username = "${var.docker_login_username}"
    docker_login_password = "${var.docker_login_password}"
  }
}

# ---------------------------------------------------------------------------------------------------------------------
# DEPLOY THE CLUSTER IN THE DEFAULT VPC AND SUBNETS
# Using the default VPC and subnets makes this example easy to run and test, but it means Instances are
# accessible from the public Internet. In a production deployment, we strongly recommend deploying into a custom VPC
# and private subnets.
# ---------------------------------------------------------------------------------------------------------------------

data "aws_subnet_ids" "default" {
  vpc_id = "${var.vpc_id}"
}

但是此配置不起作用,它仅在单个区域中启动实例,并在实例达到20时引发错误。

如何在自动伸缩组的多个区域中创建实例?

1 个答案:

答案 0 :(得分:1)

您正确实例化了多个别名提供程序,但没有使用它们中的任何一个。

如果确实需要通过一种配置在不同区域中创建资源,则必须将提供程序的别名传递给资源:

resource "aws_autoscaling_group" "autoscaling_group_eu-central-1" {
provider = "aws.eu-central-1"
}

并根据需要重复执行此块多次(或者,最好将其提取到模块中,并将提供程序传递给模块。

但是,正如评论中提到的那样,如果您想要实现的仅仅是拥有20个以上的实例,则可以通过在AWS支持下打开故障单来增加限制。