random.RandomId
Explore with Pulumi AI
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"])
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
})
}
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 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}
Create RandomId Resource
Resources are created with functions called constructors. To learn more about declaring and configuring resources, see Resources.
Constructor syntax
new RandomId(name: string, args: RandomIdArgs, opts?: CustomResourceOptions);
@overload
def RandomId(resource_name: str,
args: RandomIdArgs,
opts: Optional[ResourceOptions] = None)
@overload
def RandomId(resource_name: str,
opts: Optional[ResourceOptions] = None,
byte_length: Optional[int] = None,
keepers: Optional[Mapping[str, str]] = 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)
public RandomId(String name, RandomIdArgs args)
public RandomId(String name, RandomIdArgs args, CustomResourceOptions options)
type: random:RandomId
properties: # The arguments to resource properties.
options: # Bag of options to control resource's behavior.
Parameters
- 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.
- args RandomIdArgs
- The arguments to resource properties.
- opts ResourceOptions
- Bag of options to control 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.
- name String
- The unique name of the resource.
- args RandomIdArgs
- The arguments to resource properties.
- options CustomResourceOptions
- Bag of options to control resource's behavior.
Constructor example
The following reference example uses placeholder values for all input properties.
var randomIdResource = new Random.RandomId("randomIdResource", new()
{
ByteLength = 0,
Keepers =
{
{ "string", "string" },
},
Prefix = "string",
});
example, err := random.NewRandomId(ctx, "randomIdResource", &random.RandomIdArgs{
ByteLength: pulumi.Int(0),
Keepers: pulumi.StringMap{
"string": pulumi.String("string"),
},
Prefix: pulumi.String("string"),
})
var randomIdResource = new RandomId("randomIdResource", RandomIdArgs.builder()
.byteLength(0)
.keepers(Map.of("string", "string"))
.prefix("string")
.build());
random_id_resource = random.RandomId("randomIdResource",
byte_length=0,
keepers={
"string": "string",
},
prefix="string")
const randomIdResource = new random.RandomId("randomIdResource", {
byteLength: 0,
keepers: {
string: "string",
},
prefix: "string",
});
type: random:RandomId
properties:
byteLength: 0
keepers:
string: string
prefix: string
RandomId Resource Properties
To learn more about resource properties and how to use them, see Inputs and Outputs in the Architecture and Concepts docs.
Inputs
In Python, inputs that are objects can be passed either as argument classes or as dictionary literals.
The RandomId resource accepts the following input properties:
- Byte
Length int - The number of random bytes to produce. The minimum value is 1, which produces eight bits of randomness.
- Keepers Dictionary<string, string>
- Arbitrary map of values that, when changed, will trigger recreation of resource. 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 map[string]string
- Arbitrary map of values that, when changed, will trigger recreation of resource. 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 Integer - The number of random bytes to produce. The minimum value is 1, which produces eight bits of randomness.
- keepers Map<String,String>
- Arbitrary map of values that, when changed, will trigger recreation of resource. 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 number - The number of random bytes to produce. The minimum value is 1, which produces eight bits of randomness.
- keepers {[key: string]: string}
- Arbitrary map of values that, when changed, will trigger recreation of resource. 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, str]
- Arbitrary map of values that, when changed, will trigger recreation of resource. 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.
- byte
Length Number - The number of random bytes to produce. The minimum value is 1, which produces eight bits of randomness.
- keepers Map<String>
- Arbitrary map of values that, when changed, will trigger recreation of resource. 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.
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.
- 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.
- 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.
Look up 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, str]] = 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)
public static RandomId get(String name, Output<String> id, RandomIdState state, CustomResourceOptions options)
Resource lookup is not supported in YAML
- 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.
- 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.
- 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-
. - Byte
Length 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, string>
- Arbitrary map of values that, when changed, will trigger recreation of resource. 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-
. - Byte
Length 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]string
- Arbitrary map of values that, when changed, will trigger recreation of resource. 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-
. - byte
Length Integer - 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,String>
- Arbitrary map of values that, when changed, will trigger recreation of resource. 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-
. - byte
Length 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]: string}
- Arbitrary map of values that, when changed, will trigger recreation of resource. 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, str]
- Arbitrary map of values that, when changed, will trigger recreation of resource. 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.
- 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-
. - byte
Length 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 Map<String>
- Arbitrary map of values that, when changed, will trigger recreation of resource. 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.
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
To learn more about importing existing cloud resources, see Importing resources.
Package Details
- Repository
- random pulumi/pulumi-random
- License
- Apache-2.0
- Notes
- This Pulumi package is based on the
random
Terraform Provider.