Ready to level-up your engineering skills? Join a Pulumi Workshop. Register Now

RandomId

The resource random.RandomId generates random numbers that are intended to be used as unique identifiers for other resources.

This resource does use a cryptographic random number generator in order to minimize the chance of collisions, making the results of this resource when a 16-byte identifier is requested of equivalent uniqueness to a type-4 UUID.

This resource can be used in conjunction with resources that have the create_before_destroy lifecycle flag set to avoid conflicts with unique names during the brief period where both the old and new resources exist concurrently.

Example Usage

using Pulumi;
using Aws = Pulumi.Aws;
using Random = Pulumi.Random;

class MyStack : Stack
{
    public MyStack()
    {
        var serverRandomId = new Random.RandomId("serverRandomId", new Random.RandomIdArgs
        {
            ByteLength = 8,
            Keepers = 
            {
                { "ami_id", @var.Ami_id },
            },
        });
        var serverInstance = new Aws.Ec2.Instance("serverInstance", new Aws.Ec2.InstanceArgs
        {
            Ami = serverRandomId.Keepers.Apply(keepers => keepers.AmiId),
            Tags = 
            {
                { "Name", serverRandomId.Hex.Apply(hex => $"web-server {hex}") },
            },
        });
    }

}

Coming soon!

import pulumi
import pulumi_aws as aws
import pulumi_random as random

server_random_id = random.RandomId("serverRandomId",
    byte_length=8,
    keepers={
        "ami_id": var["ami_id"],
    })
server_instance = aws.ec2.Instance("serverInstance",
    ami=server_random_id.keepers["amiId"],
    tags={
        "Name": server_random_id.hex.apply(lambda hex: f"web-server {hex}"),
    })
import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as random from "@pulumi/random";

const serverRandomId = new random.RandomId("server", {
    byteLength: 8,
    keepers: {
        // Generate a new id each time we switch to a new AMI id
        ami_id: var_ami_id,
    },
});
const serverInstance = new aws.ec2.Instance("server", {
    ami: serverRandomId.keepers.apply(keepers => keepers.amiId),
    tags: {
        Name: pulumi.interpolate`web-server ${serverRandomId.hex}`,
    },
});

Create a RandomId Resource

new RandomId(name: string, args: RandomIdArgs, opts?: CustomResourceOptions);
def RandomId(resource_name: str, opts: Optional[ResourceOptions] = None, byte_length: Optional[int] = None, keepers: Optional[Mapping[str, Any]] = None, prefix: Optional[str] = None)
func NewRandomId(ctx *Context, name string, args RandomIdArgs, opts ...ResourceOption) (*RandomId, error)
public RandomId(string name, RandomIdArgs args, CustomResourceOptions? opts = null)
name string
The unique name of the resource.
args RandomIdArgs
The arguments to resource properties.
opts CustomResourceOptions
Bag of options to control resource's behavior.
resource_name str
The unique name of the resource.
opts ResourceOptions
A bag of options that control this resource's behavior.
ctx Context
Context object for the current deployment.
name string
The unique name of the resource.
args RandomIdArgs
The arguments to resource properties.
opts ResourceOption
Bag of options to control resource's behavior.
name string
The unique name of the resource.
args RandomIdArgs
The arguments to resource properties.
opts CustomResourceOptions
Bag of options to control resource's behavior.

RandomId Resource Properties

To learn more about resource properties and how to use them, see Inputs and Outputs in the Programming Model docs.

Inputs

The RandomId resource accepts the following input properties:

ByteLength int

The number of random bytes to produce. The minimum value is 1, which produces eight bits of randomness.

Keepers Dictionary<string, object>

Arbitrary map of values that, when changed, will trigger a new id to be generated. See the main provider documentation for more information.

Prefix string

Arbitrary string to prefix the output value with. This string is supplied as-is, meaning it is not guaranteed to be URL-safe or base64 encoded.

ByteLength int

The number of random bytes to produce. The minimum value is 1, which produces eight bits of randomness.

Keepers map[string]interface{}

Arbitrary map of values that, when changed, will trigger a new id to be generated. See the main provider documentation for more information.

Prefix string

Arbitrary string to prefix the output value with. This string is supplied as-is, meaning it is not guaranteed to be URL-safe or base64 encoded.

byteLength number

The number of random bytes to produce. The minimum value is 1, which produces eight bits of randomness.

keepers {[key: string]: any}

Arbitrary map of values that, when changed, will trigger a new id to be generated. See the main provider documentation for more information.

prefix string

Arbitrary string to prefix the output value with. This string is supplied as-is, meaning it is not guaranteed to be URL-safe or base64 encoded.

byte_length int

The number of random bytes to produce. The minimum value is 1, which produces eight bits of randomness.

keepers Mapping[str, Any]

Arbitrary map of values that, when changed, will trigger a new id to be generated. See the main provider documentation for more information.

prefix str

Arbitrary string to prefix the output value with. This string is supplied as-is, meaning it is not guaranteed to be URL-safe or base64 encoded.

Outputs

All input properties are implicitly available as output properties. Additionally, the RandomId resource produces the following output properties:

B64Std string

The generated id presented in base64 without additional transformations.

B64Url string

The generated id presented in base64, using the URL-friendly character set: case-sensitive letters, digits and the characters _ and -.

Dec string

The generated id presented in non-padded decimal digits.

Hex string

The generated id presented in padded hexadecimal digits. This result will always be twice as long as the requested byte length.

Id string
The provider-assigned unique ID for this managed resource.
B64Std string

The generated id presented in base64 without additional transformations.

B64Url string

The generated id presented in base64, using the URL-friendly character set: case-sensitive letters, digits and the characters _ and -.

Dec string

The generated id presented in non-padded decimal digits.

Hex string

The generated id presented in padded hexadecimal digits. This result will always be twice as long as the requested byte length.

Id string
The provider-assigned unique ID for this managed resource.
b64Std string

The generated id presented in base64 without additional transformations.

b64Url string

The generated id presented in base64, using the URL-friendly character set: case-sensitive letters, digits and the characters _ and -.

dec string

The generated id presented in non-padded decimal digits.

hex string

The generated id presented in padded hexadecimal digits. This result will always be twice as long as the requested byte length.

id string
The provider-assigned unique ID for this managed resource.
b64_std str

The generated id presented in base64 without additional transformations.

b64_url str

The generated id presented in base64, using the URL-friendly character set: case-sensitive letters, digits and the characters _ and -.

dec str

The generated id presented in non-padded decimal digits.

hex str

The generated id presented in padded hexadecimal digits. This result will always be twice as long as the requested byte length.

id str
The provider-assigned unique ID for this managed resource.

Look up an Existing RandomId Resource

Get an existing RandomId resource’s state with the given name, ID, and optional extra properties used to qualify the lookup.

public static get(name: string, id: Input<ID>, state?: RandomIdState, opts?: CustomResourceOptions): RandomId
@staticmethod
def get(resource_name: str, id: str, opts: Optional[ResourceOptions] = None, b64_std: Optional[str] = None, b64_url: Optional[str] = None, byte_length: Optional[int] = None, dec: Optional[str] = None, hex: Optional[str] = None, keepers: Optional[Mapping[str, Any]] = None, prefix: Optional[str] = None) -> RandomId
func GetRandomId(ctx *Context, name string, id IDInput, state *RandomIdState, opts ...ResourceOption) (*RandomId, error)
public static RandomId Get(string name, Input<string> id, RandomIdState? state, CustomResourceOptions? opts = null)
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
resource_name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.
name
The unique name of the resulting resource.
id
The unique provider ID of the resource to lookup.
state
Any extra arguments used during the lookup.
opts
A bag of options that control this resource's behavior.

The following state arguments are supported:

B64Std string

The generated id presented in base64 without additional transformations.

B64Url string

The generated id presented in base64, using the URL-friendly character set: case-sensitive letters, digits and the characters _ and -.

ByteLength int

The number of random bytes to produce. The minimum value is 1, which produces eight bits of randomness.

Dec string

The generated id presented in non-padded decimal digits.

Hex string

The generated id presented in padded hexadecimal digits. This result will always be twice as long as the requested byte length.

Keepers Dictionary<string, object>

Arbitrary map of values that, when changed, will trigger a new id to be generated. See the main provider documentation for more information.

Prefix string

Arbitrary string to prefix the output value with. This string is supplied as-is, meaning it is not guaranteed to be URL-safe or base64 encoded.

B64Std string

The generated id presented in base64 without additional transformations.

B64Url string

The generated id presented in base64, using the URL-friendly character set: case-sensitive letters, digits and the characters _ and -.

ByteLength int

The number of random bytes to produce. The minimum value is 1, which produces eight bits of randomness.

Dec string

The generated id presented in non-padded decimal digits.

Hex string

The generated id presented in padded hexadecimal digits. This result will always be twice as long as the requested byte length.

Keepers map[string]interface{}

Arbitrary map of values that, when changed, will trigger a new id to be generated. See the main provider documentation for more information.

Prefix string

Arbitrary string to prefix the output value with. This string is supplied as-is, meaning it is not guaranteed to be URL-safe or base64 encoded.

b64Std string

The generated id presented in base64 without additional transformations.

b64Url string

The generated id presented in base64, using the URL-friendly character set: case-sensitive letters, digits and the characters _ and -.

byteLength number

The number of random bytes to produce. The minimum value is 1, which produces eight bits of randomness.

dec string

The generated id presented in non-padded decimal digits.

hex string

The generated id presented in padded hexadecimal digits. This result will always be twice as long as the requested byte length.

keepers {[key: string]: any}

Arbitrary map of values that, when changed, will trigger a new id to be generated. See the main provider documentation for more information.

prefix string

Arbitrary string to prefix the output value with. This string is supplied as-is, meaning it is not guaranteed to be URL-safe or base64 encoded.

b64_std str

The generated id presented in base64 without additional transformations.

b64_url str

The generated id presented in base64, using the URL-friendly character set: case-sensitive letters, digits and the characters _ and -.

byte_length int

The number of random bytes to produce. The minimum value is 1, which produces eight bits of randomness.

dec str

The generated id presented in non-padded decimal digits.

hex str

The generated id presented in padded hexadecimal digits. This result will always be twice as long as the requested byte length.

keepers Mapping[str, Any]

Arbitrary map of values that, when changed, will trigger a new id to be generated. See the main provider documentation for more information.

prefix str

Arbitrary string to prefix the output value with. This string is supplied as-is, meaning it is not guaranteed to be URL-safe or base64 encoded.

Import

Random Ids can be imported using the b64_url with an optional prefix. This can be used to replace a config value with a value interpolated from the random provider without experiencing diffs. Example with no prefix

 $ pulumi import random:index/randomId:RandomId server p-9hUg

Example with prefix (prefix is separated by a ,)

 $ pulumi import random:index/randomId:RandomId server my-prefix-,p-9hUg

Package Details

Repository
https://github.com/pulumi/pulumi-random
License
Apache-2.0
Notes
This Pulumi package is based on the random Terraform Provider.