How to assign unique names to EC2 instances

2,931 views
Skip to first unread message

mak...@hotpads.com

unread,
Nov 9, 2016, 6:23:59 PM11/9/16
to Terraform
Hello everyone!

I'm building an infrastructure where we need to use unique "Name" tag for each EC2 instance, as we have other script dependending on that.

My original idea was based on this page and it looked like:
resource "aws_instance" "server" {
  tags = {
    "Name" = SERVERNAME-"${self.private_ip_address}"
  }
}

However, after digging through Terraform documentation and PRs I found that it is not possible to use ${self.ATTRIBUTE} (like seld.id or seld.private_ip_address) while provisioning instance. 
PR 1033 states that it cannot be used within the "resource" itself.

On other hand it is not possible to use random number as well, because those nodes would be reprovisioned every time we run Terraform.

Would someone please recommend on how to generate  and use unique instance name.

Thank you,
Maxim

David Adams

unread,
Nov 9, 2016, 6:38:24 PM11/9/16
to terrafo...@googlegroups.com
I believe this sort of thing is what https://www.terraform.io/docs/providers/random/r/id.html is designed for.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to the Google Groups "Terraform" group.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/90f29556-8c45-45f8-96cc-b319ef97b19d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Paddy Foran

unread,
Nov 9, 2016, 6:39:12 PM11/9/16
to terrafo...@googlegroups.com
Hi Maxim,

I could be wrong about this, as I'm relatively new to Terraform, but if you're looking for a unique value in the tag, I think you should be able to use the built-in random provider to generate a random ID that will only change when the resource is created. https://www.terraform.io/docs/providers/random/

Hope that helps!

Thanks,
Paddy

--

Maxim Fedchyshyn

unread,
Nov 9, 2016, 8:47:13 PM11/9/16
to terrafo...@googlegroups.com
Thank you David and Paddy for your answers!
I was trying to use random ID with the same code as shown on example page but it was in fact generating two instances with the same random part in the name. That was weird. :)

After asking this question I played with different options and now I'm using  ${count.index}  and thinking about migrating to autoscaling group and using name_prefix option.

Thanks,
Maxim

You received this message because you are subscribed to a topic in the Google Groups "Terraform" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/terraform-tool/D5thLCSQWTw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/CA%2BBw9CiPtdX11zkjwgJs15PJZAkxQ5rqQnB5T02o7kGXewmO2w%40mail.gmail.com.

ben.mi...@scrippsnetworks.com

unread,
Dec 7, 2016, 3:30:06 PM12/7/16
to Terraform
I'm running into the same issue. I'm writing a stack template for Azure where developers will spin up a couple of VMs (and associated resources) using this same TF Template (from Jenkins) and I need a way to make the resource names unique (since they will deploy to the same Resource Group and Storage Container).  I though I could do this with the "random_id" resource - but i'm struggling to understand it's syntax. When I spin up the two VMs it is giving them the same name (though they include random characters).   
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to a topic in the Google Groups "Terraform" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/terraform-tool/D5thLCSQWTw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to terraform-too...@googlegroups.com.

Maxim Fedchyshyn

unread,
Dec 7, 2016, 5:40:33 PM12/7/16
to terrafo...@googlegroups.com
Ben,
I was thinking about similar use case. In my opinion it might worth to pass required name as Jenkins input parameter. You could configure Jenkins to define an input before running provisioning task, and configure TF scripts to use that parameter.

Hope that helps!     

To unsubscribe from this group and all its topics, send an email to terraform-tool+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/terraform-tool/943e85af-70ae-4bc7-b17d-15c795a9ed54%40googlegroups.com.

Martin Atkins

unread,
Dec 11, 2016, 3:57:12 PM12/11/16
to Terraform
Hi Maxim,

It sounded like you found a different solution here, but to clarify on the random_id resource: you would need to have a separate random_id instance for each random id you want to generate. The random_id resource exists to create and retain a random id, so it is by design that once a given resource has been created it will return the same id until it is recreated.

To get multiple ids you can either have multiple separate random_id configuration blocks in your config or you can use the count attribute on a single block to generate multiple ids with the same settings.
To unsubscribe from this group and stop receiving emails from it, send an email to terraform-too...@googlegroups.com.

--
This mailing list is governed under the HashiCorp Community Guidelines - https://www.hashicorp.com/community-guidelines.html. Behavior in violation of those guidelines may result in your removal from this mailing list.
 
GitHub Issues: https://github.com/hashicorp/terraform/issues
IRC: #terraform-tool on Freenode
---
You received this message because you are subscribed to a topic in the Google Groups "Terraform" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/terraform-tool/D5thLCSQWTw/unsubscribe.
To unsubscribe from this group and all its topics, send an email to terraform-too...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages