Compute
Elastic Cloud Server
Huawei Cloud Flexus
Bare Metal Server
Auto Scaling
Image Management Service
Dedicated Host
FunctionGraph
Cloud Phone Host
Huawei Cloud EulerOS
Networking
Virtual Private Cloud
Elastic IP
Elastic Load Balance
NAT Gateway
Direct Connect
Virtual Private Network
VPC Endpoint
Cloud Connect
Enterprise Router
Enterprise Switch
Global Accelerator
Management & Governance
Cloud Eye
Identity and Access Management
Cloud Trace Service
Resource Formation Service
Tag Management Service
Log Tank Service
Config
OneAccess
Resource Access Manager
Simple Message Notification
Application Performance Management
Application Operations Management
Organizations
Optimization Advisor
IAM Identity Center
Cloud Operations Center
Resource Governance Center
Migration
Server Migration Service
Object Storage Migration Service
Cloud Data Migration
Migration Center
Cloud Ecosystem
KooGallery
Partner Center
User Support
My Account
Billing Center
Cost Center
Resource Center
Enterprise Management
Service Tickets
HUAWEI CLOUD (International) FAQs
ICP Filing
Support Plans
My Credentials
Customer Operation Capabilities
Partner Support Plans
Professional Services
Analytics
MapReduce Service
Data Lake Insight
CloudTable Service
Cloud Search Service
Data Lake Visualization
Data Ingestion Service
GaussDB(DWS)
DataArts Studio
Data Lake Factory
DataArts Lake Formation
IoT
IoT Device Access
Others
Product Pricing Details
System Permissions
Console Quick Start
Common FAQs
Instructions for Associating with a HUAWEI CLOUD Partner
Message Center
Security & Compliance
Security Technologies and Applications
Web Application Firewall
Host Security Service
Cloud Firewall
SecMaster
Anti-DDoS Service
Data Encryption Workshop
Database Security Service
Cloud Bastion Host
Data Security Center
Cloud Certificate Manager
Edge Security
Situation Awareness
Managed Threat Detection
Blockchain
Blockchain Service
Web3 Node Engine Service
Media Services
Media Processing Center
Video On Demand
Live
SparkRTC
MetaStudio
Storage
Object Storage Service
Elastic Volume Service
Cloud Backup and Recovery
Storage Disaster Recovery Service
Scalable File Service Turbo
Scalable File Service
Volume Backup Service
Cloud Server Backup Service
Data Express Service
Dedicated Distributed Storage Service
Containers
Cloud Container Engine
SoftWare Repository for Container
Application Service Mesh
Ubiquitous Cloud Native Service
Cloud Container Instance
Databases
Relational Database Service
Document Database Service
Data Admin Service
Data Replication Service
GeminiDB
GaussDB
Distributed Database Middleware
Database and Application Migration UGO
TaurusDB
Middleware
Distributed Cache Service
API Gateway
Distributed Message Service for Kafka
Distributed Message Service for RabbitMQ
Distributed Message Service for RocketMQ
Cloud Service Engine
Multi-Site High Availability Service
EventGrid
Dedicated Cloud
Dedicated Computing Cluster
Business Applications
Workspace
ROMA Connect
Message & SMS
Domain Name Service
Edge Data Center Management
Meeting
AI
Face Recognition Service
Graph Engine Service
Content Moderation
Image Recognition
Optical Character Recognition
ModelArts
ImageSearch
Conversational Bot Service
Speech Interaction Service
Huawei HiLens
Video Intelligent Analysis Service
Developer Tools
SDK Developer Guide
API Request Signing Guide
Terraform
Koo Command Line Interface
Content Delivery & Edge Computing
Content Delivery Network
Intelligent EdgeFabric
CloudPond
Intelligent EdgeCloud
Solutions
SAP Cloud
High Performance Computing
Developer Services
ServiceStage
CodeArts
CodeArts PerfTest
CodeArts Req
CodeArts Pipeline
CodeArts Build
CodeArts Deploy
CodeArts Artifact
CodeArts TestPlan
CodeArts Check
CodeArts Repo
Cloud Application Engine
MacroVerse aPaaS
KooMessage
KooPhone
KooDrive

Configuring Environment Variables

Updated on 2023-11-21 GMT+08:00

Overview

Environment variables allow you to pass dynamic parameters to a function without modifying code.

Scenario

  • Environment distinguishing: Configure different environment variables for the same function logic. For example, use environment variables to configure testing and development databases.
  • Configuration encryption: Configure encrypted environment variables to dynamically obtain authentication information (account, password, AK/SK) required to access other services.
  • Dynamic configuration: Configure environment variables for parameters that need to be dynamically adjusted, including query period and timeout, in function logic.

Procedure

You can configure encryption settings and environment variables to dynamically pass settings to your function code and libraries without changing your code.

Figure 1 Adding environment variables

For example, for Node.js, encryption settings and environment variable values can be obtained from getUserData(string key) in Context. For details, see Developing Functions in Node.js.

WARNING:
  • Environment variables and encryption settings are user-defined key-value pairs that store function settings. Keys can contain letters, digits, and underscores (_), and must start with a letter.
  • The total length of the key and value cannot exceed 4096 characters.
  • When you define environment variables, FunctionGraph displays all your input information in plain text. To ensure security, do not include sensitive information.
  • After encryption is enabled, key-value pairs are encrypted on the console and will remain encrypted during transmission.

Preset Parameters

The following lists preset parameters. Do not configure environment variables with the same names as any of these parameters.

Table 1 Preset parameters and description

Environment Variable

Description

Obtaining Method and Default Value

RUNTIME_PROJECT_ID

Project ID

Obtain the value from a Context interface or a system environment variable.

RUNTIME_FUNC_NAME

Function name

Obtain the value from a Context interface or a system environment variable.

RUNTIME_FUNC_VERSION

Function version

Obtain the value from a Context interface or a system environment variable.

RUNTIME_HANDLER

Handler

Obtain the value from a system environment variable.

RUNTIME_TIMEOUT

Execution timeout allowed for a function.

Obtain the value from a system environment variable.

RUNTIME_USERDATA

Value passed through an environment variable

Obtain the value from a Context interface or a system environment variable.

RUNTIME_CPU

CPU usage of a function. The value is in proportion to MemorySize.

Obtain the value from a Context interface or a system environment variable.

RUNTIME_MEMORY

Memory size configured for a function

Obtain the value from a Context interface or a system environment variable.

Unit: MB

RUNTIME_MAX_RESP_BODY_SIZE

Maximum size of a response body

Obtain the value from a system environment variable.

Default value: 6,291,456 bytes

RUNTIME_INITIALIZER_HANDLER

Initializer

Obtain the value from a system environment variable.

RUNTIME_INITIALIZER_TIMEOUT

Initialization timeout of a function

Obtain the value from a system environment variable.

RUNTIME_ROOT

Runtime package path

Obtain the value from a system environment variable.

Default value: /home/snuser/runtime

RUNTIME_CODE_ROOT

Path for storing code in a container

Obtain the value from a system environment variable.

Default value: /opt/function/code

RUNTIME_LOG_DIR

Path for storing system logs in a container

Obtain the value from a system environment variable.

Default value: /home/snuser/log

Example

You can use environment variables to configure which directory to install files in, where to store outputs, and how to store connection and logging settings. These settings are decoupled from the application logic, so you do not need to update your function code when you change the settings.

In the following code snippet, obs_output_bucket is the bucket used for storing processed images.

def handler(event, context):
    srcBucket, srcObjName = getObsObjInfo4OBSTrigger(event)
    obs_address = context.getUserData('obs_address')
    outputBucket = context.getUserData('obs_output_bucket')
    if obs_address is None:
        obs_address = '{obs_address_ip}'
    if outputBucket is None:
        outputBucket = 'casebucket-out'
            
    ak = context.getAccessKey()
    sk = context.getSecretKey()

    # download file uploaded by user from obs
    GetObject(obs_address, srcBucket, srcObjName, ak, sk)

    outFile = watermark_image(srcObjName)
    
    # Upload converted files to a new OBS bucket.
    PostObject (obs_address, outputBucket, outFile, ak, sk)

    return 'OK'

Using environment variable obs_output_bucket, you can flexibly set the OBS bucket used for storing output images.

Figure 2 Environment variables

We use cookies to improve our site and your experience. By continuing to browse our site you accept our cookie policy. Find out more

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback