edit-icon download-icon

Execution environment

Last Updated: Mar 28, 2018

When a function is called, the function codes are run in a restricted environment:

Environment configuration Description
Execution user A common user (without the root permission)
Code directory env["FC_FUNC_CODE_PATH"]
Writable directory /tmp (Other directories are read-only.)
Operating system Linux 4.4.24-2.al7.x86_64
Network It is allowed to access the Internet and the intranets of Alibaba Cloud services (such as OSS and Table Store) in the same regions.

You can use this Web Shell tool to experience the function execution environment.

Environments in different programming languages contain different commonly-used libraries. For more information, see the programming documentation in each language:

Use a code directory

If you pack some configuration files or data files with codes and then upload them together, and need to access these files using codes, the FC_FUNC_CODE_PATH environment variable must be used to obtain the absolute file path. The following illustrates how to obtain the path in Python, Node.js and Java respectively:

Python:

  1. import os
  2. def handler(event, context):
  3. cfg_file = os.environ['FC_FUNC_CODE_PATH'] + '/config.json'
  4. print cfg_file

Node.js:

  1. exports.handler = function(event, context, callback) {
  2. cfgFile = process.env['FC_FUNC_CODE_PATH'] + '/config.json';
  3. console.log(cfgFile);
  4. callback(null, 'done');
  5. }

Java:

  1. import com.aliyun.fc.runtime.Context;
  2. import com.aliyun.fc.runtime.StreamRequestHandler;
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.io.OutputStream;
  6. public class HelloFC implements StreamRequestHandler {
  7. @Override
  8. public void handleRequest(InputStream inputStream, OutputStream outputStream, Context context) throws IOException {
  9. String cfgFile = System.getenv("FC_FUNC_CODE_PATH") + "/config.json";
  10. outputStream.write(cfgFile.getBytes());
  11. }
  12. }

Use an intranet domain name

When a function needs to access other cloud services, you are recommended to use an intranet domain name. On one hand, an intranet domain name can produce better performance. On the other hand, no Internet traffic fee is charged in this way. The following illustrates how to use an intranet domain name to access OSS in Python and Node.js respectively:

Python:

  1. import oss2
  2. def my_handler(event, context):
  3. creds = context.credentials
  4. auth = oss2.StsAuth(creds.accessKeyId, creds.accessKeySecret, creds.securityToken)
  5. bucket = oss2.Bucket(auth, 'oss-cn-shanghai-internal.aliyuncs.com', 'my-bucket')
  6. bucket.put_object('my-object', 'hello world')

Node.js:

  1. var OSSClient = require('ali-oss').Wrapper;
  2. exports.handler = function(event, context, callback) {
  3. console.log(event.toString());
  4. var ossClient = new OSSClient({
  5. accessKeyId: context.credentials.accessKeyId,
  6. accessKeySecret: context.credentials.accessKeySecret,
  7. stsToken: context.credentials.securityToken,
  8. region: 'oss-cn-shanghai',
  9. internal: true,
  10. bucket: 'my-bucket',
  11. });
  12. ossClient.put('my-object', new Buffer('hello world'))
  13. .then(function(res) {
  14. callback(null, res);
  15. }).catch(function(err) {
  16. callback(err);
  17. });
  18. };
Thank you! We've received your feedback.