【问题标题】:Not able to create instances across multiple regions in AWS autoscaling group using terraform无法使用 terraform 在 AWS 自动缩放组中跨多个区域创建实例
【发布时间】:2019-02-27 15:18:52
【问题描述】:

在我的应用程序中,我使用 AWS 自动缩放组,使用 terraform。我启动了一个 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 时抛出错误。

我们如何在一个自动缩放组中跨多个区域创建实例?

【问题讨论】:

  • 您可以请求 AWS 支持来增加您在该区域的实例数。这些只是软上限,以防止您意外花费超过您的准备或被带走。此外,一个 ASG 不能跨越多个区域,只有少数全球资源可以做到这一点。

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


【解决方案1】:

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

如果你真的需要从一个配置中创建不同区域的资源,你必须将提供者的别名传递给资源:

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

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

但是,正如评论中提到的,如果您只想拥有超过 20 个实例,您可以通过 AWS 支持开票来增加您的限制。

【讨论】:

    猜你喜欢
    • 2017-01-13
    • 2017-12-02
    • 2016-09-21
    • 1970-01-01
    • 2021-06-16
    • 2021-10-26
    • 2018-07-14
    • 2021-07-30
    相关资源
    最近更新 更多