All Products
Search
Document Center

Function Compute:Quickly create a function

更新時間:Mar 01, 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.

Background

You must activate Function Compute and create a service in Function Compute before you create a function. After you create a function, you can run the function.

A service is the basic unit that helps you manage resources in Function Compute. You can create functions in a service and configure the settings of the service. For example, you can grant permissions to and configure the logging feature for the service. A function is the basic unit for resource scheduling and running in Function Compute. A function specifies the logic of code. You must write code based on the function interfaces provided by Function Compute and deploy the code to Function Compute as a function. Services in Function Compute are similar to microservices in a software application architecture. When you build applications on the Function Compute platform, abstract the business logic into microservices based on your business requirements. The microservices are then implemented as services in Function Compute.

Prerequisites

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

Procedure

This topic describes how to create a function in the Function Compute console. In this topic, Use Built-in Runtime and Use Custom Runtime are used as examples.

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 the services as an RAM user.

    • If you have 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 Create the AliyunFcDefaultRole Role message appears. Click Create to go to the Create Role page and create the AliyunFCDefaultRole role.

    • Authorization succeeds

      If the authorization succeeds, Function Compute has the permissions to access specified Alibaba Cloud resources.

    • Authorization fails

      If authorization fails, check whether you have the permissions to perform authorization.

      • If you use a RAM user and do not have the permissions to create the default role for Function Compute, you must log on with your Alibaba Cloud account and go to the page for creating roles to grant permissions. After the authorization, the Create the AliyunFcDefaultRole Role message is not displayed the next time you log on to the Function Compute console by using your RAM user.

      • If you use an Alibaba Cloud account or a RAM user that has the permission to create the default role for Function Compute, the authorization may fail because some features cannot be authorized. In this case, Function Compute can still be used as expected. If you still need to grant the default role, join the DingTalk group 11721331 for technical support.

    For more information about permission management, see Grant Function Compute permissions to access other Alibaba Cloud services.

Step 2: Create a service

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

  2. In the top navigation bar, select a region. On the Services page, click Create Service.

  3. In the Create Service panel, enter the service name and description, configure other parameters, and then click OK. The following table describes the parameters.

    Parameter

    Description

    Example

    Logging

    Specifies 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

    Advanced options

    Managed Service for OpenTelemetry

    Specify whether to enable Managed Service for OpenTelemetry. Valid values:

    • Enable: enables Managed Service for OpenTelemetry. You can use Jaeger to upload trace information. This allows you to view the traces of functions. You can also analyze and diagnose performance bottlenecks in serverless architectures. For more information, see Overview of Managed Service for OpenTelemetry.

    • Disable: disables Managed Service for OpenTelemetry.

    Enable

    Server Role

    Configure the role for functions in the service. This way, functions can obtain the permissions of the role. For more information, see Grant Function Compute permissions to access other Alibaba Cloud services.

    AliyunFCDefaultRole

    Access to VPC

    Specifies whether to allow functions to access virtual private cloud (VPC) resources. For more information, see Configure networks.

    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****

    Access to Internet

    Specifies whether to allow functions in the service to access the Internet. If you set this parameter to No, functions in the service cannot access the Internet over the default network interface controller (NIC) in Function Compute.

    Important

    If you want to use a static public IP address to access the Internet, you must set the Access to Internet parameter to No. Otherwise, the configured static public IP address does not take effect. For more information, see Assign a static public IP address.

    Yes

    After the service is created, you can click Services & Functions to view the created service on the Services page.

Step 3: Create a function

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

  2. In the top navigation bar, select a region. On the Services page, click the desired service.

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

    Note

    In this example, Use Built-in Runtime and Use Custom Runtime are used. If the runtimes provided by Function Compute cannot meet your business requirements, you can select Use Container Image to create a function. For more information, see Create a custom container function.

    • Basic Settings: Configure the basic information of the function, including Function Name and Handler Type. Handler Type can be set to one of the following values:

      • Event Handler: triggers the function by using a timer, calling API operations, using SDKs, or using triggers integrated with other Alibaba Cloud services.

      • HTTP Handler: triggers function execution by sending an HTTP request or creating a WebSocket connection. If you use a web scenario, we recommend that you select Use Custom Runtime.

    • 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 about the runtimes that are supported by Function Compute, see Supported function runtime environments.

      Node.js14

      Code Upload Method

      Select a method to upload the function code to Function Compute.

      • Use Sample Code: Select the sample code provided by Function Compute to create a function based on your business requirements.

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

      • Upload JAR: Select and upload a .jar file that contains function code.

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

      • OSS: Specify the Bucket Name and Object Name parameters for function code that you want to upload.

      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 is run.

      • If you set the Runtime parameter to Java 8 or Java 11, you can select only Use Sample Code, Upload JAR, or OSS to upload function code. For other runtimes, you can select Use Sample Code, Upload ZIP, Upload Folder, or OSS.

      Use Sample Code

      Startup Command

      Note

      You must configure this parameter only if you select Use Custom Runtime to create a function.

      The startup command of the program. If you do not configure the startup command, you need to manually create a startup script named bootstrap in the root directory of function code. You can start your program by using this script.

      npm run start

      Listening Port

      Note

      You must configure this parameter only if you select Use Custom Runtime to create a function.

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

      9000

    • Advanced Settings: Configure the instance information and the function execution timeout period.

      Parameter

      Description

      Example

      Specifications

      Configure the vCPU Capacity and Memory Capacity parameters based on your business requirements. For more information about the billing of resources, see Billing overview.

      Note

      The ratio of vCPU to memory capacity (in GB) is 1: N. N must be a value that ranges from 1 to 4.

      0.35 vCPUs, 512 MB

      Size of Temporary Disk

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

      Valid values:

      • 512 MB (default): You are not charged for using a temporary disk less than 512 MB in size. Function Compute provides you with a free disk of 512 MB.

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

      Note

      Data can be written to all directories in the temporary hard disk. The directories share the space of the temporary hard disk.

      The lifecycle of the temporary hard disk is consistent with that of the underlying instance. After the instance is recycled by the system, the data on the hard 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

      Instance Concurrency

      Specify the concurrency to process requests for instances. For more information, see Configure instance concurrency.

      10

      Execution Timeout Period

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

      Note

      If the execution of the function is not complete before the specified timeout period expires, the function fails to be executed. To apply for a longer timeout period, join the DingTalk group (ID: 11721331) 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 Use Custom Runtime or Use Container Image, you do not need to specify 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 is run.

      index.handler

      Time zone

      Select the time zone of your 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

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

    • In the Trigger Configurations section, configure a trigger for the function based on your business requirements. You can use the trigger to trigger the function. For more information, see Manage triggers.

Step 4: Execute the function

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

  2. In the top navigation bar, select a region. On the Services page, click the desired service.

  3. On the Functions page, click the function that you want to manage and click the Code tab to view code of the function.

    The following code snippet provides an example of code of a function that is created by using Use Built-in Runtime.

    Note

    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.

    'use strict';
    
    exports.handler = (event, context, callback) => {
      console.log('hello world');
      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.

References

  • Function Compute provides various sample code. You can easily find the sample code that meets your business requirements when you create or configure a function. For more information about the sample code of Function Compute, see Sample code.

  • 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 Manage function resources by using Serverless Devs.

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

FAQ