Manage configurations with variables

更新时间:
复制 MD 格式

Function AI lets you manage configurations and sensitive data across services by using variables. You can define project-level shared variables and service-level service variables, and reference them in your configurations. After a service is deployed, the system automatically generates output variables for it. You can then reference these variables in the configurations of other services to establish dependencies between them.

Variable types

Function AI supports three variable types: shared variable, service variable, and output variable. The following table details their differences.

Type

Shared variable

Service variable

Output variable

Scope

Within the configurations of all services in the current project.

Within the configuration of the current service.

Within the configurations of other services in the current project.

Data type

String

String

String

Reference syntax

${shared.variable_name}

${self.variable_name}

${output.service_name.variable_name}

Notes on variable references:

  • After you define a shared variable, you can reference it in the environment variables of any service within the same project by using the syntax ${shared.variable_name}.

  • After you define a service variable, you can reference it only in the environment variables of the current service by using the syntax ${self.variable_name}.

  • Custom output variables are not supported. After a service is successfully deployed, the system automatically generates output variables for the service and stores them in the backend. You can reference these output variables in the environment variables of other services by using the format ${output.<service_name>.}.

Any changes to variables, such as adding, updating, or deleting them, require a full deployment to take effect.

Note

Environment variables in the configurations of a function service or web service are distinct from the three variable types discussed above. The value of an environment variable can reference shared variables, service variables, and output variables. Your code can then retrieve these values by using environment variables in code.

Services Output Variables: Overview

The following lists the output variables for each service type. When you reference an output variable's value in the environment variable configuration of another service, you must replace <service_name> with the name of the service that provides the variable. For example, after you create and successfully deploy a database service named awesome-keller, you can reference the output variable for its instance access address, ${output.awesome-keller.host}, in the environment variables of a web service within the same project. After you reference this output variable, the code in the web service can retrieve the access address of the database instance, which allows the web service to connect to that database instance. For more information, see Use environment variables in code.

Service type

Description

Reference syntax

Web service

Function name

${output.<service_name>.functionName}

Function ARN

${output.<service_name>.functionArn}

Private endpoint

${output.<service_name>.url.system_intranet_url}

Public endpoint

${output.<service_name>.url.system_url}

Custom domain name

${output.<service_name>.url.custom_domain}

Asynchronous task service

Function name

${output.<service_name>.functionName}

Function ARN

${output.<service_name>.functionArn}

Function service

Function name

${output.<service_name>.functionName}

Function ARN

${output.<service_name>.functionArn}

Database service

Database instance ID

${output.<service_name>.instanceID}

Database instance endpoint

${output.<service_name>.host}

Model service

Model service name

${output.<service_name>.serviceName}

Public endpoint

${output.<service_name>.urlInternet}

Private endpoint

${output.<service_name>.urlIntranet}

Workflow service

Workflow service name

${output.<service_name>.name}

Encrypt variables

You can encrypt both shared variables and service variables to protect sensitive information, such as database credentials and access tokens. Go to the project details page and click Project Settings to view the shared variables, their corresponding values, and which services reference them. Click Edit next to a shared variable to add, delete, or modify it.

Similarly, go to the service details page and click Service Variables to manage variables for that service.

When you enable the Encrypt option, the variable is encrypted and managed by the platform. After you click Save and perform a full deployment, the variable appears as ciphertext in both the console and deployment logs, and its plaintext value is hidden.

Note

After a variable is encrypted and saved, you can no longer view its plaintext value. To modify an encrypted variable, click Edit, enter the new value, and then click Save. During deployment, Function AI automatically decrypts the variable within the deployment pipeline and prevents the plaintext from being exposed in logs or the console. No manual decryption is required.

Referencing variables

  • After you define a shared variable, you can reference it in the environment variables of any service within the same project by using the syntax ${shared.variable_name}.

  • After you define a service variable, you can reference it only in the environment variables of the current service by using the syntax ${self.variable_name}.

  • Custom output variables are not supported. After a service is successfully deployed, the system automatically generates output variables for the service and stores them in the backend. You can reference these output variables in the environment variables of other services by using the format ${output.<service_name>.}.

Variable reference example

For example, a function service's environment variables can reference shared, service, and output variables.

In the example, password is set to ${shared.password} (referencing a shared variable), name is set to ${self.name} (referencing a service variable for the current service), and MYSQL_HOST is set to ${output.awesome-keller.host} (referencing an output variable of the awesome-keller database service).

Advanced reference methods

Concatenating references

Function AI supports concatenated references of multiple variables or a combination of variables and strings. This allows the value of a variable to be a concatenation of multiple variable references, which Function AI can parse. For example, the @ character is used to concatenate two shared variables.

For example, if you set the shared variable userName to root and ipAddr to 127.0.01, you can set the url variable to ${shared.userName}@${shared.ipAddr} to generate the string root@127.0.01.

Nesting references

You can nest references within other variables. For example, a url variable can reference a domainName variable, which itself references another variable. Function AI automatically resolves these nested references.

For example, if you define the shared variable prefix with the value fc and the variable domainName with the value test.${shared.prefix}.com, the domainName variable has a nested reference to prefix. If you then define the variable url with the value root@${shared.domainName}, you create a multi-level nested reference chain of url→domainName→prefix.

Actual value

After configuring variables, click Save and then click full deployment to apply the changes. On the Shared Variables or Service Variables page, you can click the view icon image to see the resolved actual value.

After you click the view icon, the reference to the url variable is resolved to its actual value, root@test.fc.com. This is the final result obtained by resolving the reference through the path prefix→fc → domainName→test.fc.com → url→root@test.fc.com.