Parameters
Parameters (alpha)
Parameters are an alpha feature. See the Rich Parameters Milestone for more details.
Templates can contain parameters, which allow prompting the user for additional information when creating workspaces in both the UI and CLI.
data "coder_parameter" "docker_host" {
name = "Region"
description = "Which region would you like to deploy to?"
icon = "/emojis/1f30f.png"
type = "string"
default = "tcp://100.94.74.63:2375"
option {
name = "Pittsburgh, USA"
value = "tcp://100.94.74.63:2375"
icon = "/emojis/1f1fa-1f1f8.png"
}
option {
name = "Helsinki, Finland"
value = "tcp://100.117.102.81:2375"
icon = "/emojis/1f1eb-1f1ee.png"
}
option {
name = "Sydney, Australia"
value = "tcp://100.127.2.1:2375"
icon = "/emojis/1f1e6-1f1f9.png"
}
}
From there, parameters can be referenced during build-time:
provider "docker" {
host = data.coder_parameter.docker_host.value
}
For a complete list of supported parameter properties, see the coder_parameter Terraform reference
Types
The following parameter types are supported: string
, list(string)
, bool
, and number
.
List of strings
List of strings is a specific parameter type, that can't be easily mapped to the default value, which is string type.
Parameters with the list(string)
type must be converted to JSON arrays using jsonencode
function.
data "coder_parameter" "security_groups" {
name = "Security groups"
icon = "/icon/aws.png"
type = "list(string)"
description = "Select appropriate security groups."
mutable = true
default = jsonencode([
"Web Server Security Group",
"Database Security Group",
"Backend Security Group"
])
}
Options
A string parameter can provide a set of options to limit the choice:
data "coder_parameter" "docker_host" {
name = "Region"
description = "Which region would you like to deploy to?"
type = "string"
default = "tcp://100.94.74.63:2375"
option {
name = "Pittsburgh, USA"
value = "tcp://100.94.74.63:2375"
icon = "/emojis/1f1fa-1f1f8.png"
}
option {
name = "Helsinki, Finland"
value = "tcp://100.117.102.81:2375"
icon = "/emojis/1f1eb-1f1ee.png"
}
option {
name = "Sydney, Australia"
value = "tcp://100.127.2.1:2375"
icon = "/emojis/1f1e6-1f1f9.png"
}
}
Required and optional parameters
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.
data "coder_parameter" "account_name" {
name = "Account name"
description = "Cloud account name"
mutable = true
}
If a parameter contains the default
property, Coder will use this value
if the user does not specify any:
data "coder_parameter" "base_image" {
name = "Base image"
description = "Base machine image to download"
default = "ubuntu:latest"
}
Admins can also set the default
property to an empty value so that the parameter field can remain empty:
data "coder_parameter" "dotfiles_url" {
name = "dotfiles URL"
description = "Git repository with dotfiles"
mutable = true
default = ""
}
Mutability
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.:
data "coder_parameter" "region" {
name = "Region"
description = "Region where the workspace is hosted"
mutable = false
default = "us-east-1"
}
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 advised to overuse this opportunity.
Validation
Rich parameters support multiple validation modes - min, max, monotonic numbers, and regular expressions.
Number
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.
Monotonicity can be enabled for resources that can't be shrunk without implications, for instance - disk volume size.
data "coder_parameter" "instances" {
name = "Instances"
type = "number"
description = "Number of compute instances"
validation {
min = 1
max = 8
monotonic = "increasing"
}
}
String
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.
data "coder_parameter" "project_id" {
name = "Project ID"
description = "Alpha-numeric project ID"
validation {
regex = "^[a-z0-9]+$"
error = "Unfortunately, it isn't a valid project ID"
}
}
Legacy
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.
variable "use_kubeconfig" {
sensitive = true # Template-level parameter (not editable when creating a workspace)
type = bool
description = <<-EOF
Use host kubeconfig? (true/false)
EOF
}
variable "cpu" {
sensitive = false # User (workspace-level) parameter
description = "CPU (__ cores)"
default = 2
validation {
condition = contains([
"2",
"4",
"6",
"8"
], var.cpu)
error_message = "Invalid cpu!"
}
}
⚠️ Legacy (
variable
) parameters and rich parameters can't be used in the same template.
Migration
Terraform variables shouldn't be used for workspace scoped parameters anymore, and it's recommended to convert variables to coder_parameter
resources. To make the migration smoother, there is a special property introduced -
legacy_variable
and legacy_variable_name
, which can link coder_parameter
with a legacy variable.
variable "legacy_cpu" {
sensitive = false
description = "CPU cores"
default = 2
}
data "coder_parameter" "cpu" {
name = "CPU cores"
type = "number"
description = "Number of CPU cores"
mutable = true
legacy_variable_name = "legacy_cpu"
legacy_variable = var.legacy_cpu
}
Steps
-
Prepare and update a new template version:
- Add
coder_parameter
resource matching the legacy variable to migrate. - Use
legacy_variable_name
andlegacy_variable
to link thecoder_parameter
to the legacy variable. - Mark the new parameter as
mutable
, so that Coder will not block updating existing workspaces.
- Add
-
Update all workspaces to the updated template version. Coder will populate the added
coder_parameter
s with values from legacy variables. -
Prepare another template version:
- Remove the migrated variables.
- Remove properties
legacy_variable
andlegacy_variable_name
fromcoder_parameter
s.
-
Update all workspaces to the updated template version (2nd).
-
Prepare a third template version:
- Enable the
feature_use_managed_variables
provider flag to use managed Terraform variables for template customization. Once the flag is enabled, legacy variables won't be used.
- Enable the
-
Update all workspaces to the updated template version (3rd).
-
Delete legacy parameters.
As a template improvement, the template author can consider making some of the new coder_parameter
resources mutable
.
Managed Terraform variables
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 template variables.
The template author can enable managed Terraform variables mode by specifying the following flag:
provider "coder" {
feature_use_managed_variables = "true"
}
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.