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}") },
            },
        });
    }

}
package main

import (
    "fmt"

    "github.com/pulumi/pulumi-aws/sdk/v2/go/aws/ec2"
    "github.com/pulumi/pulumi-random/sdk/v2/go/random"
    "github.com/pulumi/pulumi/sdk/v2/go/pulumi"
)

func main() {
    pulumi.Run(func(ctx *pulumi.Context) error {
        serverRandomId, err := random.NewRandomId(ctx, "serverRandomId", &random.RandomIdArgs{
            ByteLength: pulumi.Int(8),
            Keepers: pulumi.StringMap{
                "ami_id": pulumi.String(_var.Ami_id),
            },
        })
        if err != nil {
            return err
        }
        _, err = ec2.NewInstance(ctx, "serverInstance", &ec2.InstanceArgs{
            Ami: pulumi.String(serverRandomId.Keepers.ApplyT(func(keepers map[string]string) (string, error) {
                return keepers.AmiId, nil
            }).(pulumi.StringOutput)),
            Tags: pulumi.StringMap{
                "Name": serverRandomId.Hex.ApplyT(func(hex string) (string, error) {
                    return fmt.Sprintf("%v%v", "web-server ", hex), nil
                }).(pulumi.StringOutput),
            },
        })
        if err != nil {
            return err
        }
        return nil
    })
}
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

def RandomId(resource_name, opts=None, byte_length=None, keepers=None, prefix=None, __props__=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.

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.

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.

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 float

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

keepers Dict[str, Any]

Arbitrary map of values that, when changed, will trigger a new id to be generated.

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:

B64 string

Deprecated: Use b64_url for old behavior, or b64_std for standard base64 encoding

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 string

Deprecated: Use b64_url for old behavior, or b64_std for standard base64 encoding

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 string

Deprecated: Use b64_url for old behavior, or b64_std for standard base64 encoding

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 str

Deprecated: Use b64_url for old behavior, or b64_std for standard base64 encoding

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
static get(resource_name, id, opts=None, b64=None, b64_std=None, b64_url=None, byte_length=None, dec=None, hex=None, keepers=None, prefix=None, __props__=None);
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:

B64 string

Deprecated: Use b64_url for old behavior, or b64_std for standard base64 encoding

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.

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 string

Deprecated: Use b64_url for old behavior, or b64_std for standard base64 encoding

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.

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 string

Deprecated: Use b64_url for old behavior, or b64_std for standard base64 encoding

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.

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 str

Deprecated: Use b64_url for old behavior, or b64_std for standard base64 encoding

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 float

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 Dict[str, Any]

Arbitrary map of values that, when changed, will trigger a new id to be generated.

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.

Package Details

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