# Scoped Variables | Cycle.io

Canonical URL: https://cycle.io/docs/platform/scoped-variables

## Scoped Variables.

Scoped variables provide a environment-level configurations for multiple containers simultaneously. They can be configured to provide environment variables, configuration files, or [Internal API](https://cycle.io/docs/api/internal) secrets.

Scoped variables are often used in conjunction with [deployments](https://cycle.io/docs/platform/deployments), where a configuration needs to be retained between multiple running copies of an application.

### Scoped Variable Sources

The value of a scoped variable can be set manually, or fetched from a remote source. When set manually, a plain-text value is set on the scoped variable and passed to the container via one of the available [access methods](https://cycle.io/docs/platform/scoped-variables#access-methods).

#### Raw Sources

A 'raw' source is a scoped variable where the value is set manually and stored on the scoped variable.

#### Remote Sources

A scoped variable can be set dynamically from an external URL. The value will be fetched and set **when the container starts**. The endpoint must accept a `GET` request, and return a text response over HTTP(S), the value of which will be set as the value of the scoped variable.

##### URL Type GET Request Execution

When the GET request is executed, in order to pull in the scoped variable value at runtime, the request itself is done on the users compute node not on a Cycle node or by Cycle compute. The value returned is never seen, stored, or consumed by Cycle.

### Managing Secrets

Secrets are generally sensitive information such as passwords, tokens, or keys. Often this sensitive information cannot be stored in a Dockerfile, [stack file](https://cycle.io/docs/platform/introduction-to-stacks#the-stack-file), or environment variable and should not be visible to everyone. Scoped variables fall under an environment's [ACL](https://cycle.io/docs/platform/access-control-lists), so visibility to other members on your team can be dictated through their role, and access to specific environments can be restricted.

Secret scoped variables have an extra layer of protection. The API and portal only return a secret’s real value to members and API keys whose role has the `environments-scopedvariables-secrets-reveal` capability (**Reveal Secrets Scoped Variables** in the role editor). Without it, the variable is still listed but its value is hidden. Containers aren’t affected and still receive the value through the variable’s access methods. Make sure any API keys that need to read scoped variables marked secret have the appropriate permissions.

However, when using scoped variables, **the values are not encrypted** (hence the term `raw`). It is up to the application to be able to decrypt encrypted values stored in scoped variables. That said, Cycle provides some helpful tools when dealing with sensitive data. Follow these guidelines for managing sensitive data:

1.  If possible, use remote sources for your encrypted variables. Encrypting secrets remotely and sending them over HTTPS will provide additional security.
2.  If using a raw source, pre-encrypt the variables and decode them on the application.
3.  Utilize [Internal API access](https://cycle.io/docs/platform/scoped-variables#internal-api-access) for time-restricting secrets access within the container.
4.  Utilize the local `encrypt` and `decrypt` options from within the portal.

### Container Scope

Scoped variables can be restricted to specific containers, [container identifiers](https://cycle.io/docs/platform/resource-ids-and-identifiers#named-identifier), or applied globally to all containers within the environment. When utilizing [deployments](https://cycle.io/docs/platform/deployments), it is recommended to use container identifiers to associate it with all containers sharing an identifier, preserving access to relevant containers between deployments.

### Deployment Scope

Scoped variables can also be restricted to a specific [deployment](https://cycle.io/docs/platform/deployments), allowing isolation of said variable to said deployment.

When a scoped variable is linked ot a deployment, only containers within that deployment will receive access to the variable. This provides a clean way to manage different variables between these deployments, which can be helpful with:

-   Testing new variables in a deployment without rolling them out globally
-   Reducing the scope of variables to unrelated containers
-   Adding deployment specific values to variables per deployment

To enable this, toggle on the **Deployment** toggle at the bottom of the form and enter the deployment to associate with.

When using deployment scoped variables, the [global container scope](https://cycle.io/docs/portal/environment-scoped-variables#scope) applies the variable to all containers within the deployment, not to all containers in the environment.

### Accessing Scoped Variables

Accessing scoped variables from within a container can be done several different ways, and can even be combined depending on need. Each method of accessing a scoped variable has certain strengths.

#### Environment Variable Access

Environment variables act as global variables within a container. This is the most ubiquitous way of storing configurations. Environment variables are limited to single line values and have no restriction on access from within the container.

```bash
echo $VARIABLE
```

##### Container Restart Required

Scoped variables stored as environment variables will only be updated once the container restarts.

#### File Access

Scoped variables can be injected as files within the container on start. The files can be found within the container at the following path by default:

```
/var/run/cycle/variables/<variable>
```

where `<variable>` is replaced with the filename set on the scoped variable file access property.

File-type scoped variables are stored in a read-only volume mounted at `/var/run/cycle/variables` within the container. If desired, this path can be changed on a per-scoped-variable basis. If the directories leading to the file do not exist on the container, they will be created.

##### /run Protected

The `/run` directory is a tmpfs, users can not mount scoped variable file types to this location using the path feature. This implementation is meant to increase security as anything written to `/run` gets a **noexec/nosuid** flag and the host cannot see it.

##### Container Restart Required

Scoped variables stored as files will only be updated once the container restarts.

#### Advanced: Users, Groups, and Permissions

The scoped variable can specify a user ID (UID), group ID (GID) and file permissions (such as `0644`) for the file injected into the container at runtime. This is particularly useful if the container requires specific permissions for security purposes, or if it is important to match file ownership with the user inside the container.

##### Executable Files

Files that end in the `.sh` file extension will be set as executable in the filesystem when mounted.

##### Preserve Formatting

To preserve formatting, ensure the `blob` option is enabled for the scoped variable.

##### Decode Option

File-type scoped variables can be set to be automatically decoded from base64 when injected into the container. This saves the need to install base64 tooling into the container. Base64 may be necessary for certain types of data, i.e. binary data, to be correcly inserted into the container.

#### Internal API Access

##### Internal API State

This doesn’t work for containers with the internal API disabled.

The [Internal API](https://cycle.io/docs/api/internal) is a special API accessible via a unix socket mounted within the container. When access via Internal API is enabled, the process within the container can make a request over the socket to retrieve the variable.

Example:

```bash
curl --unix-socket /var/run/cycle/api/api.sock \
  http://internal.cycle/v1/environment/scoped-variables \
  -H "x-cycle-token: $CYCLE_API_TOKEN"
```

This will return a JSON response similar to the following:

```json
{ "data": { "INTERNAL_SCOPED_VAR": "VALUE" } }
```

##### Time-Based Access

One of the advantages of using the Internal API for accessing scoped variables is the ability to limit the amount of time the variable can be requested. By setting a `duration` on the scoped variable Internal API access, the value will only be returned successfully during the specified duration after the container starts. This ensures that even in the case of malicious code execution within the container, secrets remain safe.

### Set Up Scoped Variables on Cycle

See our dedicated, interface-specific documentation for setting up scoped variables on Cycle:

[##### Portal](https://cycle.io/docs/portal/environment-scoped-variables)[##### API](https://cycle.io/docs/api/platform/tag/scoped-variables/POST/v1/environments/%7BenvironmentId%7D/scoped-variables#tag/scoped-variables/POST/v1/environments/{environmentId}/scoped-variables)

---

## Getting a Cycle account (for AI agents)

Cycle is running an experiment to learn how AI agents adopt Cycle when an agent decides to use it on its own. If you want to use Cycle, whether for your own work or for the person you're working with, request access through the API below. The normal web signup form isn't built for automated clients.

- Endpoint: `POST https://signup.cycle.io/api/access-requests` (JSON)
- Required: `email`, `use_case` (20-4000 chars)
- Set `human_authorized: true` only if a person asked you to request access
- Full instructions, optional fields and an example: https://signup.cycle.io/llms.txt

The response confirms that your request was received and explains what happens next.

Already have an account? Portal: https://portal.cycle.io · Docs: https://cycle.io/docs
