| --- |
| page_title: "Provider: Random" |
| description: |- |
| The Random provider is used to generate randomness. |
| --- |
| |
| |
| <!-- Please do not edit this file, it is generated. --> |
| # Random Provider |
| |
| The "random" provider allows the use of randomness within Terraform |
| configurations. This is a *logical provider*, which means that it works |
| entirely within Terraform's logic, and doesn't interact with any other |
| services. |
| |
| Unconstrained randomness within a Terraform configuration would not be very |
| useful, since Terraform's goal is to converge on a fixed configuration by |
| applying a diff. Because of this, the "random" provider provides an idea of |
| *managed randomness*: it provides resources that generate random values during |
| their creation and then hold those values steady until the inputs are changed. |
| |
| Even with these resources, it is advisable to keep the use of randomness within |
| Terraform configuration to a minimum, and retain it for special cases only; |
| Terraform works best when the configuration is well-defined, since its behavior |
| can then be more readily predicted. |
| |
| Unless otherwise stated within the documentation of a specific resource, this |
| provider's results are **not** sufficiently random for cryptographic use. |
| |
| For more information on the specific resources available, see the links in the |
| navigation bar. Read on for information on the general patterns that apply |
| to this provider's resources. |
| |
| ## Resource "Keepers" |
| |
| As noted above, the random resources generate randomness only when they are |
| created; the results produced are stored in the Terraform state and re-used |
| until the inputs change, prompting the resource to be recreated. |
| |
| The resources all provide a map argument called `keepers` that can be populated |
| with arbitrary key/value pairs that should be selected such that they remain |
| the same until new random values are desired. |
| |
| For example: |
| |
| ```python |
| # DO NOT EDIT. Code generated by 'cdktf convert' - Please report bugs at https://cdk.tf/bug |
| from constructs import Construct |
| from cdktf import Fn, Token, TerraformStack |
| # |
| # Provider bindings are generated by running `cdktf get`. |
| # See https://cdk.tf/provider-generation for more details. |
| # |
| from imports.aws.instance import Instance |
| from imports.random.id import Id |
| class MyConvertedCode(TerraformStack): |
| def __init__(self, scope, name): |
| super().__init__(scope, name) |
| server = Id(self, "server", |
| byte_length=8, |
| keepers={ |
| "ami_id": ami_id.string_value |
| } |
| ) |
| aws_instance_server = Instance(self, "server_1", |
| ami=Token.as_string(Fn.lookup_nested(server, ["keepers", "ami_id"])), |
| tags={ |
| "Name": "web-server ${" + server.hex + "}" |
| } |
| ) |
| # This allows the Terraform resource name to match the original name. You can remove the call if you don't need them to match. |
| aws_instance_server.override_logical_id("server") |
| ``` |
| |
| Resource "keepers" are optional. The other arguments to each resource must |
| *also* remain constant in order to retain a random result. |
| |
| `keepers` are *not* treated as sensitive attributes; a value used for `keepers` will be displayed in Terraform UI output as plaintext. |
| |
| To force a random result to be replaced, the `taint` command can be used to |
| produce a new result on the next run. |
| <!-- cache-key: cdktf-0.19.0 input-481e7e1d76d2dd9651b63f9879c24c591e04b1977a58673585b6ad657bd0e1fc 556251879b8ed0dc4c87a76b568667e0ab5e2c46efdd14a05c556daf05678783--> |