1. Packages
  2. Random
  3. API Docs
  4. RandomId
random v4.16.0 published on Wednesday, Feb 28, 2024 by Pulumi

random.RandomId

Explore with Pulumi AI

random logo
random v4.16.0 published on Wednesday, Feb 28, 2024 by Pulumi

    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

    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 {
    		// 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, 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

    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.

    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

    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, 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.
    ByteLength 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.
    byteLength 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.
    byteLength 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.
    byteLength 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.
    The following state arguments are supported:
    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, 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 -.
    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]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 -.
    byteLength 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 -.
    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]: 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 -.
    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 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.
    random logo
    random v4.16.0 published on Wednesday, Feb 28, 2024 by Pulumi