terraform-aws-modules / terraform-aws-key-pair

Terraform module to create AWS EC2 key pair resources 🇺🇦
https://registry.terraform.io/modules/terraform-aws-modules/key-pair/aws
Apache License 2.0
83 stars 127 forks source link

Enabling several keypairs to be generated (i.e count > 1) #14

Closed muranant closed 3 years ago

muranant commented 3 years ago

We recently found a need to create a keypair with every VM that we create. So there were two updates to the module. One was to support count in this module and use this array of keys in the VM module.

resource "aws_key_pair" "this" {
  count      = var.key_count
  key_name   = var.key_count == 1 ? var.key_name : format("%s-%03d", var.key_name, (count.index + 1))
  public_key = var.ssh_public_key
  tags = var.tags
}

I can create a PR for the same, if you feel there is a need to entertain it.

antonbabenko commented 3 years ago

Hi @muranant !

There is no need to use count inside of the module because you can call this module multiple time to get several resources created, like described here:

module "key_pair" {
  for_each   = toset(["key1", "key2"])

  source     = "terraform-aws-modules/key-pair/aws"
  key_name   = each.key
}
github-actions[bot] commented 2 years 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.