Generate a Random Password With Terraform

In this blog post, I will show you how to generate a random password with Terraform using the Hashicorp random provider.

Hashicorp

Like the Microsoft Azure and AWS provider, Hashicorp also has providers, and one of them is the random provider.

The random provider allows us to generate a random number, characters and more as you will see in the following configuration.

Configuration

The random provider will create three passwords with 16 characters and special characters in the configuration below—the last code block output the password to the screen.

terraform {
  required_providers {
    random = {
      source = "hashicorp/random"
      version = "3.0.1"
    }
  }
}

provider "random" {
}

resource "random_password" "password" {
  count = 3
  length = 16
  special = true
}

output "password" {
  description = "The password is:" 
  value = random_password.password.*.result
}

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.