terraform-aws-modules / terraform-aws-ec2-instance

Terraform module to create AWS EC2 instance(s) resources 🇺🇦
https://registry.terraform.io/modules/terraform-aws-modules/ec2-instance/aws
Apache License 2.0
754 stars 1.87k forks source link

Include a count that results in more than 0 or 1 #308

Closed si-walker-cpi closed 1 year ago

si-walker-cpi commented 1 year ago

terraform provider: 1.3.4

It's currently pretty much impossible, as far as I'm aware, to create multiple instances from the same locals/variables config block, you have to write multiple config blocks to get multiple instances.

If there was an instance_count variable that could be passed into the module, this would solve the problems encountered at the root module level.

At the moment the count function is used as a method of deciding whether or not to deploy a resource, which seems like a waste of the function.

I'd like this code to work

variable "environment" {
  type        = string
}

locals {
  ws        = var.environment #ws = terraform.workspace

  ec2 = {
    dev = {
      #name = [0:name, 1:instance_count, 2:ami, 4:instance_type, ...etc
      mything     = ["thing", 2, "", "Ubuntu", "t3a.medium"]
      myappserver = ["app", 3, "", "Ubuntu", "t3a.medium"]
      myjump      = ["jump", 1, "", "Ubuntu", "t3a.medium"]
    }
    qa = {}
    prod = {}
  }
}

module "ec2" {
  source  = "terraform-aws-modules/ec2-instance/aws"
  version = "~> 3.0"

  for_each = {
  for key, ec2_instance in lookup(local.ec2_instance, local.ws) : key => ec2_instance
  }

  ami              = (each.value.2 == "") ? data.aws_ami.ec2[each.key].id : each.value.2
  name             = "${module.label.id}-${each.key}"
  instance_count   = each.value.1
  ... etc
}
github-actions[bot] commented 1 year ago

This issue has been automatically marked as stale because it has been open 30 days with no activity. Remove stale label or comment or this issue will be closed in 10 days

bryantbiggs commented 1 year ago

we already provide an example on how to do this https://github.com/terraform-aws-modules/terraform-aws-ec2-instance/blob/f612ec14f996a378909dd2cd461118c1d272cf70/examples/complete/main.tf#L153-L212

github-actions[bot] commented 1 year ago

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.