Randomised Suffix in Terraform Resource Names
I didn’t like how I named cloud resources for Toph. The resource names had sequential suffixes at the end.
Let’s take Toph’s storage servers, for example, where MongoDB replicas are running. On Linode, while they were there, I had named the three storage servers as toph-storage-sgp-1
, toph-storage-sgp-2
and toph-storage-sgp-3
.
But as I moved them over to Vultr (referral) last week, I realised that eliminating these sequential numbers could remove any notion of preference/precedence among these three servers.
It was time to adopt a better naming scheme. For that, I chose the following:
{application}-{role}-{cloud}-{region}-{suffix}
I wanted {suffix}
to be a 5-character random string with only numbers and lower-cased alphabets.
Terraform makes creating randomised strings easy. The “random_string” resource type is just for that. The strings, although random, stay unchanged between each terraform apply
.
Here is how you can add randomised suffixes to resource names in Terraform:
|
|
With this example, after running terraform apply
, the resource label will be something like “toph-storage-vultr-sgp-ns2oh”.
P.S: Yes, I know I should also add the environment (e.g. “production”, “staging”, etc.) into that naming scheme. 🙃
This post is 20th of my #100DaysToOffload challenge. Want to get involved? Find out more at 100daystooffload.com.
comments powered by Disqus