How to Create Random Passwords Using Terraform

Search for a command to run...

No comments yet. Be the first to comment.
In Part 1 of this series we explored the friendly foundations of machine learning: Classification Regression Clustering Neural networks Training Testing Overfitting Decision trees Reinforceme

Machine Learning with Trufa and Paula: A Friendly Guide to How Models Learn

The Microsoft Copilot Ecosystem

I love words, I compile words and always search what they mean. My native language is español and to my surprise there are not that many on line. México where I'm from has no official online language

Hey everyone, Roberto here and there and everywhere! We've all worked with data, we've all felt the pain of waiting. When you build a beautiful Power BI report, but the data is from yesterday. You have to wait for the nightly refresh to see the lates...

When creating VMs, it's common to use a username and password to connect to them. While using SSH keys is definitely better for Linux machines, the other day I needed to set a password for an Ubuntu VM
I checked out the Terraform function for creating random strings:
Then I wrote the code below which later re-use in the VM creation module
variable "length" {
description = "The length input from the user"
type = number
default = 18
}
resource "random_password" "password" {
length = var.length
special = true
override_special = "!#$%&*()-_=+[]{}<>:?"
keepers = {
# this will recreate the password if the timestamp changes: meaning every running will generate a new password
timestamp = "${timestamp()}"
}
}
output "espassword" {
value = random_password.password.result
sensitive = true
}
/* Create a random password with the following requirements:
- Length: 18 default value
to change the length, use the following command
export TF_VAR_length=20 in the shell changing the lenght will regenerate the password
Can also be done using the following command: terraform apply -var 'length=20' -auto-approve
- Special characters: !#$%&*()-_=+[]{}<>:?
- Output the password as a sensitive output
to show the password in the Terraform output
terraform output espassword
to regenerate the password
terraform taint random_password.password
terraform apply
terraform output espassword
*/
To execute this code:
terraform init
terraform plan (optional)
terraform apply
terraform output espassword. This will show the password string in the console
Easier to execute all in 1 liner: terraform apply -auto-approve;terraform output espassword
Also, it's possible to create passwords of different length which can be specified at runtime, ex: terraform apply -var 'length=22' -auto-approve;terraform output espassword
Once that done the password will be show in the console, ex:
"w$5(sLht)N>!{IjQ7r" Note: Quotes are not meant to be part of the password
| The password repo | https://github.com/soyroberto/terraform-library/blob/main/101/passwordgenerator/main.tf |