DEV Community

Cover image for How-to setup a HA/DR database in AWS? [9 - Generate a random value]
Maxime Guilbert
Maxime Guilbert

Posted on • Edited on

3 3

How-to setup a HA/DR database in AWS? [9 - Generate a random value]

In this part (and the last part of the serie), we will see how to generate a random value.

Really useful to generate unique names for a snapshot for example, you will see that there are multiple definitions available, depending on what you need.


Random Id

resource "random_id" "rdm_id" {
  byte_length = 8
}
Enter fullscreen mode Exit fullscreen mode

Declared as this, it will generate for you an 8 bytes long id which can be retrieve in :

  • base64 : random_id.rdm_id.id => MDc3NDA2OGE5YTNhMjc5MQ==
  • decimal digits : random_id.rdm_id.dec => 537061447926687633
  • hexadecimal digits : random_id.rdm_id.hex => 0774068a9a3a2791

But it you are doing it like this, the random_id will always be the same!

So to be sure the value change when something append, you can use the keepers parameter.

With this, you can say "if the parameter X and Y change, so the random value must change too".

In this example the random value will change each time the arn of the global cluster example will change.

resource "random_id" "rdm_id" {
  byte_length = 8

  keepers = {
    first = aws_rds_global_cluster.example.arn
  }
}
Enter fullscreen mode Exit fullscreen mode

And if you want this value to change each time the script is executed, use a timestamp :

resource "random_id" "rdm_id" {
  byte_length = 8

  keepers = {
    first = timestamp()
  }
}
Enter fullscreen mode Exit fullscreen mode

Other random possibilities

Following the same pattern, Terraform let you to generate random

  • integer
resource "random_integer" "priority" {
  min = 1
  max = 50000
}
Enter fullscreen mode Exit fullscreen mode
  • password
resource "random_password" "password" {
  length           = 16
  special          = true
  override_special = "_%@"
}
Enter fullscreen mode Exit fullscreen mode
  • string
resource "random_string" "random" {
  length           = 16
  special          = true
  override_special = "/@£$"
}
Enter fullscreen mode Exit fullscreen mode
  • UUID
resource "random_uuid" "test" {
}
Enter fullscreen mode Exit fullscreen mode
  • A shuffled sublist
resource "random_shuffle" "az" {
  input        = ["us-west-1a", "us-west-1c", "us-west-1d", "us-west-1e"]
  result_count = 2
}
Enter fullscreen mode Exit fullscreen mode

Here you will retrieve a sublist of the input list, with only 2 items.


Links


I hope it will help you and you liked this serie! 🍺


Serie link


You want to support me?

Buy Me A Coffee

Heroku

Build apps, not infrastructure.

Dealing with servers, hardware, and infrastructure can take up your valuable time. Discover the benefits of Heroku, the PaaS of choice for developers since 2007.

Visit Site

Top comments (0)

AWS GenAI LIVE image

How is generative AI increasing efficiency?

Join AWS GenAI LIVE! to find out how gen AI is reshaping productivity, streamlining processes, and driving innovation.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay