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.
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 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 serverRandomId = new Random.RandomId("serverRandomId", new()
{
Keepers =
{
{ "ami_id", @var.Ami_id },
},
ByteLength = 8,
});
var serverInstance = new Aws.Ec2.Instance("serverInstance", new()
{
Tags =
{
{ "Name", serverRandomId.Hex.Apply(hex => $"web-server {hex}") },
},
Ami = serverRandomId.Keepers.Apply(keepers => keepers?.AmiId),
});
// ... (other aws_instance arguments) ...
});
package main
import (
"fmt"
"github.com/pulumi/pulumi-aws/sdk/v5/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 {
serverRandomId, err := random.NewRandomId(ctx, "serverRandomId", &random.RandomIdArgs{
Keepers: pulumi.StringMap{
"ami_id": pulumi.Any(_var.Ami_id),
},
ByteLength: pulumi.Int(8),
})
if err != nil {
return err
}
_, err = ec2.NewInstance(ctx, "serverInstance", &ec2.InstanceArgs{
Tags: pulumi.StringMap{
"Name": serverRandomId.Hex.ApplyT(func(hex string) (string, error) {
return fmt.Sprintf("web-server %v", hex), nil
}).(pulumi.StringOutput),
},
Ami: serverRandomId.Keepers.ApplyT(func(keepers interface{}) (*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) {
var serverRandomId = new RandomId("serverRandomId", RandomIdArgs.builder()
.keepers(Map.of("ami_id", var_.ami_id()))
.byteLength(8)
.build());
var serverInstance = new Instance("serverInstance", InstanceArgs.builder()
.tags(Map.of("Name", serverRandomId.hex().applyValue(hex -> String.format("web-server %s", hex))))
.ami(serverRandomId.keepers().applyValue(keepers -> keepers.amiId()))
.build());
}
}
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_id = random.RandomId("serverRandomId",
keepers={
"ami_id": var["ami_id"],
},
byte_length=8)
server_instance = aws.ec2.Instance("serverInstance",
tags={
"Name": server_random_id.hex.apply(lambda hex: f"web-server {hex}"),
},
ami=server_random_id.keepers["amiId"])
# ... (other aws_instance arguments) ...
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 serverRandomId = new random.RandomId("serverRandomId", {
keepers: {
ami_id: _var.ami_id,
},
byteLength: 8,
});
const serverInstance = new aws.ec2.Instance("serverInstance", {
tags: {
Name: pulumi.interpolate`web-server ${serverRandomId.hex}`,
},
ami: serverRandomId.keepers.apply(keepers => keepers?.amiId),
});
// ... (other aws_instance arguments) ...
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.
serverRandomId:
type: random:RandomId
properties:
keepers:
ami_id: ${var.ami_id}
byteLength: 8
serverInstance:
type: aws:ec2:Instance
properties:
tags:
Name: web-server ${serverRandomId.hex}
# Read the AMI id "through" the random_id resource to ensure that
# # both will change together.
ami: ${serverRandomId.keepers.amiId}
Create RandomId Resource
new RandomId(name: string, args: RandomIdArgs, opts?: CustomResourceOptions);
@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)
@overload
def RandomId(resource_name: str,
args: RandomIdArgs,
opts: Optional[ResourceOptions] = 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.
- 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.
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
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
Package Details
- Repository
- random pulumi/pulumi-random
- License
- Apache-2.0
- Notes
This Pulumi package is based on the
random
Terraform Provider.