What is @pulumi/random?
@pulumi/random is an npm package that provides utilities for generating random values, such as random strings, random integers, and random passwords. It is particularly useful in infrastructure as code (IaC) scenarios where you need to generate random values for resource names, passwords, or other configurations.
What are @pulumi/random's main functionalities?
Random String
Generates a random string of specified length. In this example, a random string of length 16 is generated without special characters.
const pulumi = require('@pulumi/pulumi');
const random = require('@pulumi/random');
const randomString = new random.RandomString('randomString', {
length: 16,
special: false,
});
exports.result = randomString.result;
Random Integer
Generates a random integer within a specified range. In this example, a random integer between 1 and 100 is generated.
const pulumi = require('@pulumi/pulumi');
const random = require('@pulumi/random');
const randomInteger = new random.RandomInteger('randomInteger', {
min: 1,
max: 100,
});
exports.result = randomInteger.result;
Random Password
Generates a random password of specified length. In this example, a random password of length 20 is generated with special characters included.
const pulumi = require('@pulumi/pulumi');
const random = require('@pulumi/random');
const randomPassword = new random.RandomPassword('randomPassword', {
length: 20,
special: true,
});
exports.result = randomPassword.result;
Other packages similar to @pulumi/random
randomstring
The 'randomstring' package is a simple utility for generating random strings. It is less feature-rich compared to @pulumi/random, focusing primarily on string generation without the additional capabilities for integers or passwords.
crypto-random-string
The 'crypto-random-string' package generates cryptographically strong random strings. It is similar to @pulumi/random in terms of string generation but does not offer features for generating random integers or passwords.
secure-random-password
The 'secure-random-password' package specializes in generating secure random passwords. It offers more customization options for password generation compared to @pulumi/random but does not provide utilities for generating random strings or integers.
Random Provider
The random provider allows the safe use of randomness in a Pulumi program. This allows you to generate resource
properties, such as names, that contain randomness in a way that works with Pulumi's goal state oriented approach.
Using randomness as usual would not work well with Pulumi, because by definition, each time the program is evaluated,
a new random state would be produced, necessitating re-convergence on the goal state. This provider understands
how to work with the Pulumi resource lifecycle to accomplish randomness safely and in a way that works as desired.
Example
For example, to generate a random string, simply allocate a resource:
const random = require("@pulumi/random");
const password = new random.RandomString("password", {
length: 16,
special: true,
overrideSpecial: "/@\" ",
});
From there we can use its result
output property, of type Output<string>
, to pass to another resource.
Installing
This package is available in many languages in the standard packaging formats.
Node.js (Java/TypeScript)
To use from JavaScript or TypeScript in Node.js, install using either npm
:
$ npm install @pulumi/random
or yarn
:
$ yarn add @pulumi/random
Python
To use from Python, install using pip
:
$ pip install pulumi_random
Go
To use from Go, use go get
to grab the latest version of the library
$ go get github.com/pulumi/pulumi-random/sdk/go/...
Reference
For detailed reference documentation, please visit the API docs.