The random_id Terraform resource generates a value that can be used to create remote infrastructure that requires a unique identifier.
The primary attribute it exposes is ‘.id’ which contains upper+lower+number characters, but it also has ‘.dec’ and ‘.hex’ equivalent representations that can be used to support infrastructure requiring a limited character set.
As an example, a remote resource such as a Google cloud storage bucket does not support uppercase letters, so using the ‘.hex’ representation guarantees uniqueness as well as an identifier that conforms to the hyperscaler’s constraints.
But regardless of what representation you choose to build your infrastructure resource, you may need to manually convert back to ‘id’, especially if you need to manually “terraform import” the random_id resource.
Converting hex/dec representations back to .id
If you have used the hex or decimal representation in one of your remote resources, it would be essential to convert this to the ‘.id’ value in order to perform a “terraform import” on the random_id resource.
# random_id.hex to random_id.id echo -en $TF_RAND_ID_HEX | xxd -r -p | base64 # random_id.dec to random_id.id printf "%x" $TF_RAND_ID_DEC | xxd -r -p | base64
Armed with this .id representation, you could now import the resource.
terraform import <random_id_ADDR> <id>
Converting .id to hex/dec representations
If you have the ‘.id’ value and need to determine the hex or decimal representation, here are the conversions.
# random_id.id to random_id.hex echo -en "$TF_RAND_ID====" | fold -w4 | sed '$ d' | base64 -d | xxd -p # random_id.id to random_id.dec echo -en "$TF_RAND_ID====" | fold -w4 | sed '$ d' | base64 -d | xxd -p | xargs -I{} printf "%d" 0x{}
The additional equals signs and fold are to ensure correct base64 padding (input length must be a multiple of 4).
REFERENCES
random_id resource documentation