All Products
Search
Document Center

Function Compute:Quickly create a function

Last Updated:Apr 11, 2024

With Function Compute, you do not need to purchase and manage infrastructure such as servers. You need to only write and upload code and images to build elastic and reliable applications. This topic describes how to quickly create a function in the Function Compute console. In this topic, a simple serverless Hello World function is used as an example.

Prerequisites

You have registered an Alibaba Cloud account and completed real-name verification.

Procedure

This topic uses Event Function and Web Function as examples to describe how to create functions in the Function Compute console.

Step 1: Activate Function Compute

  1. Go to the Function Compute homepage.

  2. Click Console. The page for trial plans for new users of Function Compute is displayed. Then, click Buy Now to activate Function Compute and go to the Function Compute console.

    Note
    • We recommend that you use an Alibaba Cloud account to activate Function Compute and use services as a RAM user.

    • If you have already activated Function Compute, you are directed to the Function Compute console.

  3. (Optional). If this is the first time you log on to the Function Compute console, the Alibaba Cloud Service Authorization message appears. Click OK.

    After the authorization, Function Compute can access specific Alibaba Cloud services, such as Virtual Private Cloud (VPC), Elastic Compute Service (ECS), Simple Log Service, and Container Registry. For more information about service-linked roles, see Service-linked roles.

Step 2: Create a function

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

  2. In the top navigation bar, select a region. On the Functions page, click Create Function.

  3. On the Create Function page, select a method to create a function, configure the following parameters, and then click Create.

    Note

    In this topic, Event Function and Web Function are used as examples. If runtimes provided by Function Compute cannot meet your business requirements, you can select Container Images to create a function. For more information, see Create a Custom Container function.

    • Basic Settings: Configure Function Name.

    • Code: Configure the runtime and code-related information of the function.

      Parameter

      Description

      Example

      Runtime

      Select a language, such as Python, Java, PHP, or Node.js. For more information, see Runtimes supported by Function Compute.

      Node.js 16

      Code Upload Method

      Specify how to upload code to Function Compute.

      • Use Sample Code: You can select the sample code provided by Function Compute to create a function based on your business requirements. This is the default method.

      • Upload ZIP: Select and upload a .zip file that contains your function code.

      • Upload Code in Folder: Select and upload the folder that contains your function code.

      • OSS: Upload code from an Object Storage Service (OSS) bucket. In this case, you must specify the Bucket Name and Object Name parameters.

      Use Sample Code

      Startup Command

      Note

      This parameter is required if you select Web Function or Container Images to create a function.

      Configure the startup command of the program. If you do not configure the startup command, you must manually create a startup script named bootstrap in the root directory of the code. The bootstrap script is used to start your program.

      npm run start

      Listening Port

      Note

      This parameter is required if you select Web Function or Container Images to create a function.

      Specify the port on which the HTTP server in your code listens.

      9000

    • In the Advanced Settings section, configure instance settings and other items such as the function execution timeout period.

      Parameter

      Description

      Example

      Specifications

      Configure instance specifications, such as vCPU Capacity and Memory Capacity based on your business requirements. For more information about billing of resources, see Billing overview.

      Note

      The ratio of vCPU capacity to memory capacity (in GB) must be set from 1:1 to 1:4.

      0.35 vCPUs, 512 MB

      Size of Temporary Disk

      Specify the size of the disk used to temporarily store files based on your business requirements.

      Valid values:

      • 512 MB: the default value. You are not charged for using a temporary disk of this size. Function Compute provides you with a free disk space of 512 MB.

      • 10 GB: You are charged based on a disk size of 9.5 GB.

      Note

      Data shares the space of the temporary disk and can be written to all directories in the disk.

      The size of the temporary disk is consistent with lifecycles of underlying instances. After an instance is recycled by the system, the corresponding data on the disk is cleared. To persist files, you can use Apsara File Storage NAS (NAS) or Object Storage Service (OSS). For more information, see Configure a NAS file system and Configure an OSS file system.

      512 MB

      Execution Timeout Period

      Specify the timeout period of function execution. The default timeout period is 60 seconds, and the maximum value is 86,400 seconds.

      Note

      If the execution of the function times out, the function fails to be executed. If you require a longer timeout limit, join the DingTalk group 11721331 to for technical support.

      60

      Handler

      Specify the handler of the function. The Function Compute runtime loads and invokes the handler to process requests. If you select Web Function or Container Images to create a function, you do not need to set this parameter.

      Note

      If you set the Code Upload Method parameter to Use Sample Code, retain the value of the Handler parameter. If you select another code upload method, you must modify Handler based on your business requirements. Otherwise, an error is reported when the function runs.

      index.handler

      Time Zone

      Select the time zone of the function. After you configure the time zone of the function, the environment variable TZ is automatically added to the function. The value is the time zone that you configure.

      UTC

      Function Role

      Specify the RAM role of the function. Function Compute uses this role to generate a temporary AccessKey pair for accessing your Alibaba Cloud resources and passes the AccessKey pair to your code. For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.

      mytestrole

      Access to VPC

      Specify whether to allow the function to access virtual private cloud (VPC) resources. For more information, see Configure network settings.

      Yes

      VPC

      This parameter is required if you set the Access to VPC parameter to Yes. Create a VPC or select the ID of an existing VPC that you want to access from the drop-down list.

      fc.auto.create.vpc.1632317****

      vSwitch

      This parameter is required if you set the Access to VPC parameter to Yes. Create a vSwitch or select the ID of an existing vSwitch from the drop-down list.

      fc.auto.create.vswitch.vpc-bp1p8248****

      Security group

      This parameter is required if you set the Access to VPC parameter to Yes. Create a security group or select an existing security group from the drop-down list.

      fc.auto.create.SecurityGroup.vsw-bp15ftbbbbd****

      Allow Default NIC to Access Internet

      Specify whether to allow the function to access the Internet through the default network interface controller (NIC). If you select No, the function cannot access the Internet through the default NIC of Function Compute.

      Important

      If you use a static public IP address, you must set Allow Default NIC to Access Internet to No. Otherwise, the configured static public IP address does not take effect. For more information, see Configure static public IP addresses.

      Yes

      Logging

      Specify whether to enable the logging feature. Valid values:

      • Enable: Function Compute sends function execution logs to Simple Log Service for persistent storage. You can debug code, troubleshoot issues, and analyze data based on the logs.

      • Disable: You cannot use Simple Log Service to store or query function execution logs.

      Enable

    • In the Environment Variables section, configure environment variables of the runtime of the function. For more information, see Environment variables.

Step 3: Execute the function

  1. Log on to the Function Compute console. In the left-side navigation pane, click Functions.

  2. In the top navigation bar, select a region. On the Functions page, click the desired function.

  3. On the function details page, click the Code tab to view code of the function.

    You can edit the code in the code editor to implement secondary development. Then, click Deploy. The code takes effect only after the code is deployed. In this example, an event function is created. The following sample code shows an example:

    Note

    The sample code supports one-click deployment. You can deploy the sample code in Function Compute with one click. start-fc3-nodejs

    'use strict';
    
    exports.handler = function (event, context, callback) {
      callback(null, 'hello world');
    };
    
  4. Click Test Function.

    After the function is executed, you can view the result and detailed logs on the Code tab.

More information

  • If the "Unable to import module 'index'" error occurs when your function is being executed, check whether the handler is correctly configured and whether the required dependencies are installed. For more information, see Install a third-party dependency for a function.

  • Function Compute allows you to use Serverless Devs to build, debug, and deploy applications. For more information, see Use Serverless Devs to manage functions.

  • Functions in Function Compute can be directly triggered or triggered by using events. For more information about event-triggered functions, see Trigger overview.

References