|
| 1 | +# Parameters (alpha) |
| 2 | + |
| 3 | +> Parameters are an [alpha feature](../contributing/feature-stages.md#alpha-features). See the [Rich Parameters Milestone](https://github.com/coder/coder/milestone/11) for more details. |
| 4 | +
|
| 5 | +Templates can contain _parameters_, which allow prompting the user for additional information when creating workspaces in both the UI and CLI. |
| 6 | + |
| 7 | + |
| 8 | + |
| 9 | +```hcl |
| 10 | +data "coder_parameter" "docker_host" { |
| 11 | + name = "Region" |
| 12 | + description = "Which region would you like to deploy to?" |
| 13 | + icon = "/emojis/1f30f.png" |
| 14 | + type = "string" |
| 15 | + default = "tcp://100.94.74.63:2375" |
| 16 | +
|
| 17 | + option { |
| 18 | + name = "Pittsburgh, USA" |
| 19 | + value = "tcp://100.94.74.63:2375" |
| 20 | + icon = "/emojis/1f1fa-1f1f8.png" |
| 21 | + } |
| 22 | +
|
| 23 | + option { |
| 24 | + name = "Helsinki, Finland" |
| 25 | + value = "tcp://100.117.102.81:2375" |
| 26 | + icon = "/emojis/1f1eb-1f1ee.png" |
| 27 | + } |
| 28 | +
|
| 29 | + option { |
| 30 | + name = "Sydney, Australia" |
| 31 | + value = "tcp://100.127.2.1:2375" |
| 32 | + icon = "/emojis/1f1e6-1f1f9.png" |
| 33 | + } |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +From there, parameters can be referenced during build-time: |
| 38 | + |
| 39 | +```hcl |
| 40 | +provider "docker" { |
| 41 | + host = data.coder_parameter.docker_host.value |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +The following parameter types are supported: `string`, `bool`, and `number`. |
| 46 | + |
| 47 | +> For a complete list of supported parameter properties, see the |
| 48 | +> [coder_parameter Terraform reference](https://registry.terraform.io/providers/coder/coder/latest/docs/data-sources/parameter) |
| 49 | +
|
| 50 | +## Options |
| 51 | + |
| 52 | +A _string_ parameter can provide a set of options to limit the choice: |
| 53 | + |
| 54 | +```hcl |
| 55 | +data "coder_parameter" "docker_host" { |
| 56 | + name = "Region" |
| 57 | + description = "Which region would you like to deploy to?" |
| 58 | + type = "string" |
| 59 | + default = "tcp://100.94.74.63:2375" |
| 60 | +
|
| 61 | + option { |
| 62 | + name = "Pittsburgh, USA" |
| 63 | + value = "tcp://100.94.74.63:2375" |
| 64 | + icon = "/emojis/1f1fa-1f1f8.png" |
| 65 | + } |
| 66 | +
|
| 67 | + option { |
| 68 | + name = "Helsinki, Finland" |
| 69 | + value = "tcp://100.117.102.81:2375" |
| 70 | + icon = "/emojis/1f1eb-1f1ee.png" |
| 71 | + } |
| 72 | +
|
| 73 | + option { |
| 74 | + name = "Sydney, Australia" |
| 75 | + value = "tcp://100.127.2.1:2375" |
| 76 | + icon = "/emojis/1f1e6-1f1f9.png" |
| 77 | + } |
| 78 | +} |
| 79 | +``` |
| 80 | + |
| 81 | +## Required and optional parameters |
| 82 | + |
| 83 | +A parameter is considered to be _required_ if it doesn't have the `default` property. The user **must** provide a value to this parameter before creating a workspace. |
| 84 | + |
| 85 | +```hcl |
| 86 | +data "coder_parameter" "account_name" { |
| 87 | + name = "Account name" |
| 88 | + description = "Cloud account name" |
| 89 | + mutable = true |
| 90 | +} |
| 91 | +``` |
| 92 | + |
| 93 | +If a parameter contains the `default` property, Coder will use this value |
| 94 | +if the user does not specify any: |
| 95 | + |
| 96 | +```hcl |
| 97 | +data "coder_parameter" "base_image" { |
| 98 | + name = "Base image" |
| 99 | + description = "Base machine image to download" |
| 100 | + default = "ubuntu:latest" |
| 101 | +} |
| 102 | +``` |
| 103 | + |
| 104 | +Admins can also set the `default` property to an empty value so that the parameter field can remain empty: |
| 105 | + |
| 106 | +```hcl |
| 107 | +data "coder_parameter" "dotfiles_url" { |
| 108 | + name = "dotfiles URL" |
| 109 | + description = "Git repository with dotfiles" |
| 110 | + mutable = true |
| 111 | + default = "" |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +## Mutability |
| 116 | + |
| 117 | +Immutable parameters can be only set before workspace creation. The idea is to prevent users from modifying fragile or persistent workspace resources like volumes, regions, etc.: |
| 118 | + |
| 119 | +```hcl |
| 120 | +data "coder_parameter" "region" { |
| 121 | + name = "Region" |
| 122 | + description = "Region where the workspace is hosted" |
| 123 | + mutable = false |
| 124 | + default = "us-east-1" |
| 125 | +} |
| 126 | +``` |
| 127 | + |
| 128 | +It is allowed to modify the mutability state anytime. In case of emergency, template authors can temporarily allow for changing immutable parameters to fix an operational issue, but it is not |
| 129 | +advised to overuse this opportunity. |
| 130 | + |
| 131 | +## Validation |
| 132 | + |
| 133 | +Rich parameters support multiple validation modes - min, max, monotonic numbers, and regular expressions. |
| 134 | + |
| 135 | +### Number |
| 136 | + |
| 137 | +A _number_ parameter can be limited to boundaries - min, max. Additionally, the monotonicity (`increasing` or `decreasing`) between the current parameter value and the new one can be verified too. |
| 138 | +Monotonicity can be enabled for resources that can't be shrunk without implications, for instance - disk volume size. |
| 139 | + |
| 140 | +```hcl |
| 141 | +data "coder_parameter" "instances" { |
| 142 | + name = "Instances" |
| 143 | + type = "number" |
| 144 | + description = "Number of compute instances" |
| 145 | + validation { |
| 146 | + min = 1 |
| 147 | + max = 8 |
| 148 | + monotonic = "increasing" |
| 149 | + } |
| 150 | +} |
| 151 | +``` |
| 152 | + |
| 153 | +### String |
| 154 | + |
| 155 | +A _string_ parameter can have a regular expression defined to make sure that the parameter value matches the pattern. The `regex` property requires a corresponding `error` property. |
| 156 | + |
| 157 | +```hcl |
| 158 | +data "coder_parameter" "project_id" { |
| 159 | + name = "Project ID" |
| 160 | + description = "Alpha-numeric project ID" |
| 161 | + validation { |
| 162 | + regex = "^[a-z0-9]+$" |
| 163 | + error = "Unfortunately, it isn't a valid project ID" |
| 164 | + } |
| 165 | +} |
| 166 | +``` |
| 167 | + |
| 168 | +## Legacy |
| 169 | + |
| 170 | +Prior to Coder v0.16.0 (Jan 2023), parameters were defined via Terraform `variable` blocks. These "legacy parameters" can still be used in templates, but will be removed in April 2023. |
| 171 | + |
| 172 | +```hcl |
| 173 | +variable "use_kubeconfig" { |
| 174 | + sensitive = true # Template-level parameter (not editable when creating a workspace) |
| 175 | + type = bool |
| 176 | + description = <<-EOF |
| 177 | + Use host kubeconfig? (true/false) |
| 178 | + EOF |
| 179 | +} |
| 180 | +
|
| 181 | +variable "cpu" { |
| 182 | + sensitive = false # User (workspace-level) parameter |
| 183 | + description = "CPU (__ cores)" |
| 184 | + default = 2 |
| 185 | + validation { |
| 186 | + condition = contains([ |
| 187 | + "2", |
| 188 | + "4", |
| 189 | + "6", |
| 190 | + "8" |
| 191 | + ], var.cpu) |
| 192 | + error_message = "Invalid cpu!" |
| 193 | + } |
| 194 | +} |
| 195 | +``` |
| 196 | + |
| 197 | +> ⚠️ Legacy (`variable`) parameters and rich parameters can't be used in the same template. |
| 198 | +
|
| 199 | +## Managed Terraform variables |
| 200 | + |
| 201 | +As parameters are intended to be used only for workspace customization purposes, Terraform variables can be freely managed by the template author to build templates. Workspace users are not able to modify |
| 202 | +template variables. |
| 203 | + |
| 204 | +The template author can enable managed Terraform variables mode by specifying the following flag: |
| 205 | + |
| 206 | +```hcl |
| 207 | +provider "coder" { |
| 208 | + feature_use_managed_variables = "true" |
| 209 | +} |
| 210 | +``` |
| 211 | + |
| 212 | +Once it's defined, coder will allow for modifying variables by using CLI and UI forms, but it will not be possible to use legacy parameters. |
0 commit comments