RandomId

class RandomId : KotlinCustomResource

The resource random.RandomId generates random numbers that are intended to be used as unique identifiers for other resources. If the output is considered sensitive, and should not be displayed in the CLI, use random.RandomBytes instead. 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

import * as pulumi from "@pulumi/pulumi";
import * as aws from "@pulumi/aws";
import * as random from "@pulumi/random";
// The following example shows how to generate a unique name for an AWS EC2
// instance that changes each time a new AMI id is selected.
const server = new random.RandomId("server", {
keepers: {
ami_id: amiId,
},
byteLength: 8,
});
const serverInstance = new aws.ec2.Instance("server", {
tags: {
Name: pulumi.interpolate`web-server ${server.hex}`,
},
ami: server.keepers.apply(keepers => keepers?.amiId),
});
import pulumi
import pulumi_aws as aws
import pulumi_random as random
# The following example shows how to generate a unique name for an AWS EC2
# instance that changes each time a new AMI id is selected.
server = random.RandomId("server",
keepers={
"ami_id": ami_id,
},
byte_length=8)
server_instance = aws.ec2.Instance("server",
tags={
"Name": server.hex.apply(lambda hex: f"web-server {hex}"),
},
ami=server.keepers["amiId"])
using System.Collections.Generic;
using System.Linq;
using Pulumi;
using Aws = Pulumi.Aws;
using Random = Pulumi.Random;
return await Deployment.RunAsync(() =>
{
// The following example shows how to generate a unique name for an AWS EC2
// instance that changes each time a new AMI id is selected.
var server = new Random.RandomId("server", new()
{
Keepers =
{
{ "ami_id", amiId },
},
ByteLength = 8,
});
var serverInstance = new Aws.Ec2.Instance("server", new()
{
Tags =
{
{ "Name", server.Hex.Apply(hex => $"web-server {hex}") },
},
Ami = server.Keepers.Apply(keepers => keepers?.AmiId),
});
});
package main
import (
"fmt"
"github.com/pulumi/pulumi-aws/sdk/v6/go/aws/ec2"
"github.com/pulumi/pulumi-random/sdk/v4/go/random"
"github.com/pulumi/pulumi/sdk/v3/go/pulumi"
)
func main() {
pulumi.Run(func(ctx *pulumi.Context) error {
// The following example shows how to generate a unique name for an AWS EC2
// instance that changes each time a new AMI id is selected.
server, err := random.NewRandomId(ctx, "server", &random.RandomIdArgs{
Keepers: pulumi.StringMap{
"ami_id": pulumi.Any(amiId),
},
ByteLength: pulumi.Int(8),
})
if err != nil {
return err
}
_, err = ec2.NewInstance(ctx, "server", &ec2.InstanceArgs{
Tags: pulumi.StringMap{
"Name": server.Hex.ApplyT(func(hex string) (string, error) {
return fmt.Sprintf("web-server %v", hex), nil
}).(pulumi.StringOutput),
},
Ami: pulumi.String(server.Keepers.ApplyT(func(keepers map[string]string) (*string, error) {
return &keepers.AmiId, nil
}).(pulumi.StringPtrOutput)),
})
if err != nil {
return err
}
return nil
})
}
package generated_program;
import com.pulumi.Context;
import com.pulumi.Pulumi;
import com.pulumi.core.Output;
import com.pulumi.random.RandomId;
import com.pulumi.random.RandomIdArgs;
import com.pulumi.aws.ec2.Instance;
import com.pulumi.aws.ec2.InstanceArgs;
import java.util.List;
import java.util.ArrayList;
import java.util.Map;
import java.io.File;
import java.nio.file.Files;
import java.nio.file.Paths;
public class App {
public static void main(String[] args) {
Pulumi.run(App::stack);
}
public static void stack(Context ctx) {
// The following example shows how to generate a unique name for an AWS EC2
// instance that changes each time a new AMI id is selected.
var server = new RandomId("server", RandomIdArgs.builder()
.keepers(Map.of("ami_id", amiId))
.byteLength(8)
.build());
var serverInstance = new Instance("serverInstance", InstanceArgs.builder()
.tags(Map.of("Name", server.hex().applyValue(hex -> String.format("web-server %s", hex))))
.ami(server.keepers().applyValue(keepers -> keepers.amiId()))
.build());
}
}
resources:
# The following example shows how to generate a unique name for an AWS EC2
# instance that changes each time a new AMI id is selected.
server:
type: random:RandomId
properties:
keepers:
ami_id: ${amiId}
byteLength: 8
serverInstance:
type: aws:ec2:Instance
name: server
properties:
tags:
Name: web-server ${server.hex}
ami: ${server.keepers.amiId}

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

Properties

Link copied to clipboard
val b64Std: Output<String>

The generated id presented in base64 without additional transformations.

Link copied to clipboard
val b64Url: Output<String>

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

Link copied to clipboard
val byteLength: Output<Int>

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

Link copied to clipboard
val dec: Output<String>

The generated id presented in non-padded decimal digits.

Link copied to clipboard
val hex: Output<String>

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

Link copied to clipboard
val id: Output<String>
Link copied to clipboard
val keepers: Output<Map<String, String>>?

Arbitrary map of values that, when changed, will trigger recreation of resource. See the main provider documentation for more information.

Link copied to clipboard
val prefix: Output<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.

Link copied to clipboard
val pulumiChildResources: Set<KotlinResource>
Link copied to clipboard
Link copied to clipboard
Link copied to clipboard
val urn: Output<String>