Random Id
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