# DRY string construction from resource

**URL:** <https://discuss.hashicorp.com/t/dry-string-construction-from-resource/11835>\
**Category:** Terraform\
**Created:** [July 22, 2020, 6:01pm UTC](https://discuss.hashicorp.com/t/dry-string-construction-from-resource/11835 "2020-07-22T18:01:27Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![rjhornsby](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/rjhornsby/32/4886_2.png) [@rjhornsby](https://discuss.hashicorp.com/u/rjhornsby)\
**Post date:** [July 22, 2020, 6:01pm UTC](https://discuss.hashicorp.com/t/dry-string-construction-from-resource/11835/1 "2020-07-22T18:01:27Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![apparentlymart](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/apparentlymart/32/702_2.png) [@apparentlymart](https://discuss.hashicorp.com/u/apparentlymart)\
**Post date:** [July 23, 2020, 5:21pm UTC](https://discuss.hashicorp.com/t/dry-string-construction-from-resource/11835/2 "2020-07-23T17:21:37Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![rjhornsby](https://sea2.discourse-cdn.com/hashicorp/user_avatar/discuss.hashicorp.com/rjhornsby/32/4886_2.png) [@rjhornsby](https://discuss.hashicorp.com/u/rjhornsby)\
**Post date:** [July 23, 2020, 5:49pm UTC](https://discuss.hashicorp.com/t/dry-string-construction-from-resource/11835/3 "2020-07-23T17:49:25Z")

</div>

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.

> <https://gist.github.com/rjhornsby/ea25a772a48c5206f07b31f4f09032ab>
