DRY string construction from resource

For our EC2 instances, we give them a name (which is also the hostname) that combines a common string, ie ‘vault-’, suffixed by the first few characters of the EC2 instance id. For example:

A vault server with EC2 instance id “i-ab12356afe” would be designated “vault-ab1235”. I’m having a hard time because I have to repeat the same code throughout my aws_instance resource declaration, ie in the provisioners:

${var.node_short_name}-${substr(self.id, 2, 6)}

In addition to DRY, I would really like to be able to export/output this value from the module that creates the aws_instance so that it can be used elsewhere, ie to create/destroy a DNS record.

I think because the instance-id isn’t known until after the instance is created is maybe why I’m having such a hard time with this? At least how I’ve tried to use them, local and null_data_source both create cyclic dependencies.

Is there some way to construct this string just once, and then use that result as both a value in the provisioners and export it as an output value?

Hi @rjhornsby,

Can you show a fuller example of what your aws_instance resource looks like, including the provisioners and any other separate resources that are relevant to what you’re asking here?

Happy to -

  • lib/provisioned_instance/instance.tf is where most of the work happens. It’s meant to be a sort of generalized/standard usable by more stack-specific modules to build instances.
  • chef-frontend.tf calls the provisioned_instance module
  • chef-variables.tf feeds into chef-frontend.