Vikram Singh Jadon
Vikram Singh Jadon

Reputation: 1047

Not able to create instances across multiple regions in AWS autoscaling group using terraform

In my application I am using AWS autoscaling group using terraform. I launch an Autoscaling group giving it a number of instances in a region. But Since, only 20 are instances allowed in a region. I want to launch an autoscaling group that will create instances across multiple regions so that I can launch multiple. I had this configuration:

# ---------------------------------------------------------------------------------------------------------------------
# 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}"
}

But this configuration does not work, it is only launching instances in a single region and throwing error as they reach 20.

How can we create instances across multiple regions in an autoscaling group ?

Upvotes: 0

Views: 960

Answers (1)

Alexander
Alexander

Reputation: 1487

You correctly instantiate multiple aliased providers, but are not using any of them.

If you really need to create resources in different regions from one configuration, you must pass the alias of the provider to the resource:

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

And repeat this block as many times as needed (or, better, extract it into a module and pass the providers to module.

But, as mentioned in a comment, if all you want to achieve is to have more than 20 instances, you can increase your limit by opening a ticket with AWS support.

Upvotes: 1

Related Questions