amazon-web-servicesterraformamazon-elbautoscalingprivate-subnet

Trying to create 2 ASGs in one terraform file


I'm trying to create an launch configuration, ELB and 2 ASG. I guess one ELB is fine to create 2 ASG (im not sure).

So I have a launch configuration and asg code in one file calling the as module. My question is, can I create 2 ASG using a single terraform file or with file in a single repo?

Also, Please let me know if this is a good configuration.

when I tried to put two different files calling same module I get following error.

Error downloading modules: Error loading modules: module asg: duplicated. module names must be unique

My Terraform code:

auto_scaling.tf

resource "aws_launch_configuration" "launch_config" {
image_id               = "${var.ec2ami_id}"
instance_type          = "${var.ec2_instance_type}"
security_groups        = ["${aws_security_group.*******.id}"]
key_name               = "${var.keypair}"

lifecycle {
 create_before_destroy = true
} 
}



module "asg" {
  source             = ****
  name               = "*****"
  environment        = "***"
  service            = "****"
  product            = "**"
  team               = "****"
  owner              = "*****"
  ami                = "${var.ec2_id}"
  #instance_profile   = "******"
  instance_type      = "t2.micro"
  ebs_optimized      = true
  key_name           = "${var.keypair}"
  security_group     = ["${aws_security_group.****.id}"]
  user_data          = "${path.root}/blank_user_data.sh"
  load_balancer_names = "${module.elb.elb_name}"
  associate_public_ip = false
  asg_instances      = 2
  asg_min_instances  = 2
  asg_max_instances  = 4
  root_volume_size   = 250
  asg_wait_for_capacity_timeout = "5m"
  vpc_zone_subnets   = "${module.vpc.private_subnets}"
}



###elb.tf###

module "elb" {
  source             = "*****"
  name               = "***elb"
  subnet_ids         = "${element(split(",", 
module.vpc.private_subnets), 0)}"
  security_groups    = "${aws_security_group.****.id}"
  s3_access_logs_bucket = "****"
}

I want to create 2 ASGs in one subnet.


Solution

  • You can reuse your asg module - just give both instances different resource names, e.g.:

    module "asg1" {
    ...
    }
    
    module "asg2" {
    ...
    }