create_variable_in_variable_set
Add Terraform or environment variables to variable sets in Terraform Cloud, with options to mark them as sensitive or include HCL code.
Instructions
Create a new variable in a variable set.
Creates a new Terraform or environment variable within a variable set. Variables can be marked as sensitive to hide their values.
API endpoint: POST /varsets/{varset_id}/relationships/vars
Args: varset_id: The ID of the variable set (format: "varset-xxxxxxxx") key: The variable name/key category: Variable category ("terraform" or "env")
Returns: The created variable with its configuration and metadata
See: docs/tools/variables.md#create-variable-in-variable-set for reference documentation
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| varset_id | Yes | ||
| key | Yes | ||
| category | Yes | ||
| params | No |
Implementation Reference
- The core handler function that executes the tool logic: constructs payload from inputs and sends POST request to Terraform Cloud API endpoint /varsets/{varset_id}/relationships/vars to create a variable in the variable set.async def create_variable_in_variable_set( varset_id: str, key: str, category: str, params: Optional[VariableSetVariableParams] = None, ) -> APIResponse: """Create a new variable in a variable set. Creates a new Terraform or environment variable within a variable set. Variables can be marked as sensitive to hide their values. API endpoint: POST /varsets/{varset_id}/relationships/vars Args: varset_id: The ID of the variable set (format: "varset-xxxxxxxx") key: The variable name/key category: Variable category ("terraform" or "env") params: Additional variable parameters (optional): - value: Variable value - description: Description of the variable - hcl: Whether the value is HCL code (terraform variables only) - sensitive: Whether the variable value is sensitive Returns: The created variable with its configuration and metadata See: docs/tools/variables.md#create-variable-in-variable-set for reference documentation """ # Create a temporary request-like structure for the variable # Note: We don't have specific models for variable set variables yet var_data = { "key": key, "category": VariableCategory(category).value, } if params: param_dict = params.model_dump(exclude_none=True) var_data.update(param_dict) payload = {"data": {"type": "vars", "attributes": var_data}} return await api_request( f"varsets/{varset_id}/relationships/vars", method="POST", data=payload )
- Pydantic model defining optional input parameters (value, description, hcl, sensitive, etc.) for the create_variable_in_variable_set tool, used for validation and serialization.class VariableSetVariableParams(APIRequest): """Parameters for variable set variable operations without routing fields. This model provides all optional parameters for creating or updating variables within variable sets, separating configuration parameters from routing information like variable set ID and variable ID. Reference: https://developer.hashicorp.com/terraform/cloud-docs/api-docs/variable-sets See: docs/models/variables.md for reference """ key: Optional[str] = Field( None, description="Variable name/key", min_length=1, max_length=255, ) value: Optional[str] = Field( None, description="Variable value", max_length=256000, ) description: Optional[str] = Field( None, description="Description of the variable", max_length=512, ) category: Optional[VariableCategory] = Field( None, description="Variable category (terraform or env)", ) hcl: Optional[bool] = Field( None, description="Whether the value is HCL code (only valid for terraform variables)", ) sensitive: Optional[bool] = Field( None, description="Whether the variable value is sensitive", )
- terraform_cloud_mcp/server.py:143-143 (registration)Registration of the tool in the MCP server using FastMCP, with write permissions config.mcp.tool(**write_tool_config)(variables.create_variable_in_variable_set)