Iterating through ami id with proper node names

Hi All,

I have 10 ami with names db1, db2, db3…db10 and I wanted to create 10 instances ec2-1, ec2-2, ec2-3…ec2-10 respectively using the ami. For example ec2-1 should be created always from db1 irrespective of the AMI ID . I can give any tag for ami as required, but it should always create instance from the respective AMI name. Is there any option to configure it or should I create 10 resources individually.

Every day new ami will created with same name and the old ami will be deregistered.

data "aws_ami_ids" "api_mainapp_ami" {
  # count = 9
  # name_regex       = "^API\\d{3}"
  owners           = ["98987876789"]

  filter {
    name   = "name"
    values = ["db*"]
  }

  filter {
    name   = "root-device-type"
    values = ["ebs"]
  }

  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
}


output "aws_instance_mainapp_id" {
  value = data.aws_ami_ids.api_mainapp_ami.ids
}


resource "aws_instance" "my_first_instance" {
    count = 9
    ami           = "${data.aws_ami_ids.api_mainapp_ami.*.id}"
    instance_type = "t2.micro"
}

Hi @Sya, just to understand the use case more clearly, in that case, are you also re-building EC2 instances with those new AMIs? It looks like the main question here is how to set those EC2 instance names accordingly by referencing the AMIs. Is the instance name so critical in your workflow?

Unfortunately, the aws_ami_ids data resource will only get you the AMI “IDs” and no other metadata. That means you can’t reliably tell which EC2 instance gets which AMI image when creating resources in terms of its naming convention (i.e., the “Name” tag).

So you have a few options…

  1. define multiple aws_ami_ids (or aws_ami) data sources with the exact AMI name match, create EC2 instances individually using each data source, and set an appropriate Name tag.

  2. define one aws_ami_ids data source with a wildcard AMI name match, create EC2 instances using for_each. But you can’t set the “Name” tag in a way that it follows the index (the suffix number) of the source AMI image.

  3. leverage the HCP Packer metadata registry and create EC2 instances. This way, you could create EC2 instances and set the “Name” tag by reading the metadata set to each build image (and no need to have multiple data sources like option 1 above).

Not sure if you have ever used Packer for AMI creation and its automation, but if you’re interested in adopting Packer into your workflow, I’m sure the last option would be pretty much suitable for such a use case.

Hi, It is now working after using proper filter

  filter {
    name   = "name"
    values = ["${var.node-ami-prefix}${count.index+1}"]
  }

output "aws_instance_mainapp_id" {
  value = data.aws_ami_ids.api_mainapp_ami.*.ids
}

Ah, ok, then that’s even nice. Thanks for sharing!
So you used count within the data source, right?

yes, Thanks for your inputs.

data "aws_ami_ids" "api_mainapp_ami" {
  count = 9
  owners           = ["${var.aws-account}"]
  filter {
    name   = "name"
    values = ["${var.node-ami-prefix}${count.index+1}"]
  }
  filter {
    name   = "root-device-type"
    values = ["ebs"]
  }
  filter {
    name   = "virtualization-type"
    values = ["hvm"]
  }
}
1 Like