How-to setup a HA/DR database in AWS? [9 - Generate a random value]
Tags: sre, aws, terraform, devops
Published: 2022-01-18 17:40:51

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
```terraform
resource "random_id" "rdm_id" {
  byte_length = 8
}
```

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.*
```terraform
resource "random_id" "rdm_id" {
  byte_length = 8

  keepers = {
    first = aws_rds_global_cluster.example.arn
  }
}
```

And if you want this value to change each time the script is executed, use a timestamp :
```terraform
resource "random_id" "rdm_id" {
  byte_length = 8

  keepers = {
    first = timestamp()
  }
}
```

---

## Other random possibilities
Following the same pattern, Terraform let you to generate random 
- integer
```terraform
resource "random_integer" "priority" {
  min = 1
  max = 50000
}
```
- password
```terraform
resource "random_password" "password" {
  length           = 16
  special          = true
  override_special = "_%@"
}
```
- string
```terraform
resource "random_string" "random" {
  length           = 16
  special          = true
  override_special = "/@£$"
}
```
- UUID
```terraform
resource "random_uuid" "test" {
}
```
- A shuffled sublist
```terraform
resource "random_shuffle" "az" {
  input        = ["us-west-1a", "us-west-1c", "us-west-1d", "us-west-1e"]
  result_count = 2
}
```
*Here you will retrieve a sublist of the input list, with only 2 items.*

---

### Links
- Terraform doc random_id : https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/id
- Terraform doc random_integer : https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/integer
- Terraform doc random_password : https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/password
- Terraform doc random_shuffle : https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/shuffle
- Terraform doc random_string : https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/string
- Terraform doc random_uuid : https://registry.terraform.io/providers/hashicorp/random/latest/docs/resources/uuid

---

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

---

## Serie link
- **1 - Start** : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-1-1ko7
- **2 - Definitions** : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-2-definitions-93p
- **3 - Simple database** : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-3-simple-database-a9o
- **4 - HA Database** : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-4-ha-database-4kek
- **5 - DR database** : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-5-dr-database-278b
- **6 - Create from snapshot** : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-6-create-from-snapshot-2mbf
- **7 - Dynamic Terraform backend definition** : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-7-dynamic-terraform-backend-definition-3aga
- **8 - Multiple instances in multiple regions** : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-8-multiple-instances-in-multiple-regions-210d
- **9 - Generate a random value** : https://dev.to/adaendra/how-to-setup-a-hadr-database-in-aws-9-generate-a-random-value-5g8a

---

<center><h3>You want to support me?</h3></center>
<a href="https://www.buymeacoffee.com/mxguilbert7" target="_blank"><img src="https://cdn.buymeacoffee.com/buttons/default-orange.png" alt="Buy Me A Coffee" height="41" width="174"></a>