Note: To use CloudShare's Customize Terraform Variables feature, contact your Account Manager.
Note: This feature is available only on Classic.The following describes how to create and configure variables for use in Terraform scripts in CloudShare so that when a relevant environment or snapshot spins up, the Terraform script’s variable values are automatically populated.
The Terraform script variables and their values are determined while:
- Defining a Terraform Script and its Variables in CloudShare: This is where both the variables and their values are created.
- Customizing an Environment’s Terraform Variable Values: This is when the variable’s values can be configured for a specific environment.
- Customizing a Snapshot's Terraform Variable Values: This is when the variable’s values can be configured for a specific snapshot.
Defining a Terraform Script and its Variables in CloudShare
Adding Terraform Scripts to CloudShare
CloudShare enables the creation of Terraform scripts for use in CloudShare by manually entering or pasting the script code. This is done by selecting the Infrastructure button on the Home page, then External Resources in the left menu, and finally Creation Scripts, as described in Creating a New Terraform Script.
These Terraform Scripts are then available for selection when defining an environment, as described in Creating an Environment.
Guidelines for Adding Variables in Terraform Scripts
When writing a Terraform script to be used in CloudShare, make sure the script defines the same variable key names that are planned to be defined as the custom variables for an environment, as described in Adding Terraform Variables to an Environment.
Defining Default Values
When defining a custom variable in a Terraform script, it is mandatory to define a default value for each variable. For example, as shown below:
variable "backend" {
type = string
default = "production"
}
Terraform validates the script’s configuration before each execution and requires every variable to have a value. If no default is defined for a variable in the script, and no value is passed to it during runtime, then the validation fails. For this reason, it is highly recommended to always define a default for each custom variable in the script, and not just the variable’s name and type.
Overriding the Default Value at Runtime
The default values described above, act as a fallback and can be overridden at runtime.
When custom Terraform key-value pairs are provided during execution, they are automatically passed to Terraform via the command line. If a runtime variable matches a predefined variable name, the runtime value of this variable takes precedence and is used throughout the configuration.
For example:
variable "template" {
type = string
default = "gateway"
}
Runtime override:
-var="template=hub"
In the example above, the Terraform script uses the value hub instead of the default value (gateway).
Adding Terraform Variables to an Environment
The following describes how to add Terraform script variables and their values while creating an environment. In this way, when an environment is run, variables will be passed to the Terraform script that CloudShare runs for that environment or snapshot.
- In the Create Environment flow (as described in Creating an Environment), open the External Resources page, as shown below:
- The Customize Terraform Variables area enables you to define up to eight variables by defining the Variable key in the Variable field and the variable’s value in the Value field, as shown below:
The variable key names and values must be defined according to the following rules:
- Keys: Up to 16 characters; Values: Up to 32 characters
- Use only letters, numbers, or underscores (no spaces or symbols)
- Strings only, no JSON or multiline values
- Don't include secrets or passwords
- Reserved names (for example, CS_Env_Id) can't be edited
- To add each additional Variable, click the + Add Variable button, which creates a new entry row for defining another variable and its value. For example, as shown below:
Note: Editing snapshot or experience variables may cause Terraform errors if they don't match your script. To delete a variable, click the Trash icon or the Clear all button.
- Click the Save Variables button.
- After this environment is saved, it spins up and populates the defined variables and their values in the environment’s Terraform script.
Customizing a Terraform Variable Values while Creating a Training or Sales (POC) Experience
Overview
After a Terraform script’s variables have been configured for an environment (as described above), that environment can be used to create an experience, as described in Creating Training Experiences or Sales (POC) Experience.
The following describes how to configure the values of an environment’s Terraform variables during the process of creating a Training or Sales (POC) experience, as described in the subsections below:
- Configuring the Variable’s Values while Creating a Training Experience
- Configuring the Variable’s Values while Creating a Sales (POC) Experience
Configuring the Variable’s Values while Creating a Training Experience
If an Experience is based on an environment/snapshot for which Terraform Script Variables were defined, the General page of the Training experience creation wizard shows a Customize Terraform Variables option, as shown below:
- Click the Customize Terraform Variables option to display the following:
This window displays all the variables and values defined for the environment’s Terraform Script, as described in Defining a Terraform Script and its Variables in CloudShare.
- Change the variable values as needed. The same syntax rules apply as described above.
Note: Only the values of the variables can be edited, not the variable key names.Configuring the Variable’s Values while Creating a Sales (POC) Experience
If a Sales (POC) Experience is based on an environment/snapshot for which Terraform Script Variables were defined, the Choose Blueprint page of the Create New POC (Sales) experience creation wizard shows a Customize Terraform Variables option, as shown below:
-
Click the Customize Terraform Variables option to display the following:
This window displays all the variables and values defined for the environment’s Terraform Script, as described in Defining a Terraform Script and its Variables in CloudShare.
- Change the variable values as needed. The same syntax rules apply as described above.
Note: Only the values of the variables can be edited, not the variable key names.Customizing a Snapshot’s Terraform Variable Values
Overview
The following describes how to configure the values of a snapshot’s Terraform variables.
An environment can be created from a template, such as when selecting the My Projects’ Environment Template option during environment creation, as described in Creating an Environment.
This option creates an environment based on a CloudShare blueprint, which is composed of snapshots.
If one of the snapshots was taken of an environment that contains Terraform variables, the newly created environment also includes the Terraform script, those variables, and their values.
Configuring the Variable Values
When an environment is based on a Terraform script that includes custom variables, the External Resources page that is displayed while creating an environment includes the Customize Terraform Variables option, as shown below:
- Select the Customize Terraform Variables option. The following is displayed:
This new window lists all the variables and their values defined for the environment’s Terraform Script.
- Change the variable values as needed. The same syntax rules apply as described above.
Note: Only the values of the variables can be edited, not the variable key names.Checking which Custom Variables are Defined for an Environment
The Environment Details page for an environment based on a Terraform script with custom variables shows which custom variables are defined for the Terraform script used by that environment.
To display the list of these custom variables:
In the Environment Details page, click External Resources, as shown below:
The following is displayed, which includes the Terraform Variables section, which lists the Variable Key names and each variable’s current Value.