Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Custom tags where count.index is used (iteration) #1

Closed
TinajaLabs opened this issue Oct 13, 2017 · 5 comments
Closed

Custom tags where count.index is used (iteration) #1

TinajaLabs opened this issue Oct 13, 2017 · 5 comments

Comments

@TinajaLabs
Copy link

I'm trying to generate various FQDN names that have different conventions.

I had a highly modified (from previous version) and I used the count and count.index to determine how many iterations to make and created the names accordingly.

Here are the variations:

  • ui.system.com
  • db01.ststem.com
  • db02.system.com
  • xyz001.xx.system.com
  • ax01.xyz001.system.com
  • ax01.xyz001.system.com

For example, the db and the ax are iterated on the host_name when count is greater than 1.

Here's what my tag block looked like inside the instance resource:

resource "aws_instance" "ec2_instance" {
  count = "${var.number_of_instances}"

...

  tags {
    Environment = "${var.track_environment}"
    Name        = "${var.number_of_instances > 1 ? "${var.host_name}${format("%02d", count.index + 1)}.${var.instance_name}" : "${var.instance_name}" }"
    Track       = "${var.track_name}"
    Desc        = "Instance for: ${var.number_of_instances > 1 ? "${var.host_name}${format("%02d", count.index + 1)}.${var.instance_name}" : "${var.instance_name}" }"
    LaunchGroup = "StartEC2Group${upper(substr(var.vpc_name,0,1))}${substr(var.vpc_name,1,-1)}"
    StopGroup   = "StopEC2atNight"
  }

I'm guessing, to keep this functionality, my only option is to modify this module; the main.tf file in resource "aws_instance" "this"

Thanks for any tips,
Chris.

@antonbabenko
Copy link
Member

Yes, you need to modify this module, because you need to access count.index which is available only inside resource block.

@antonbabenko antonbabenko changed the title iteration Custom tags where count.index is used (iteration) Oct 14, 2017
@TinajaLabs
Copy link
Author

Thank you, Anton.

FYI, my modification looks like this, in case you or anyone else might have a thought about how to generalize this.

  # modifications 
  # tags = "${merge(var.tags, map("Name", format("%s-%d", var.name, count.index+1)))}"  # original tag code

  tags = "${merge(var.tags, 
    map("Name", "${var.number_of_instances > 1 ? "${var.host_name}${format("%02d", count.index + 1)}.${var.instance_name}" : "${var.instance_name}" }"),
    map("Desc", "Instance for: ${var.number_of_instances > 1 ? "${var.host_name}${format("%02d", count.index + 1)}.${var.instance_name}" : "${var.instance_name}" }")
    )}"
  volume_tags = "${merge(var.volume_tags, 
    map("Name", "${var.number_of_instances > 1 ? "${var.host_name}${format("%02d", count.index + 1)}.${var.instance_name}" : "${var.instance_name}" }"),
    map("Desc", "Volume for: ${var.number_of_instances > 1 ? "${var.host_name}${format("%02d", count.index + 1)}.${var.instance_name}" : "${var.instance_name}" }")
    )}"

@antonbabenko
Copy link
Member

Thanks! Looks scary :) I normally (for 99.9% of cases) use auto-scaling groups and set EC2 tags from instance itself if necessary.

@TinajaLabs
Copy link
Author

Haha. Sorry. I'm a nubie so perhaps it's overkill. I get tired of looking at so many things in AWS and trying to figure our which system they're associated. So I'm a bit obsessive trying to apply, for example, the actual fqdn of the instance in the Name field.

I am also using the iterative count mechanism to build multiple variants of the same instances. So not every instance is using it's own unique resource definition.

While I'm developing for AWS now, I'm also trying to keep from going too deep into AWS, because we'll also need t support the same infrastructure on private cloud openstack. If I can't create it in AWS and Openstack, I need to be careful what I use in AWS so I don't paint us into a corner.

Thanks so much for your help and advice.

@github-actions
Copy link

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.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 10, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants