All Products
Search
Document Center

Function Compute:Quickly create a function

Last Updated:Sep 01, 2023

This topic describes how to create a Serverless Hello World function in the Function Compute console.

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 correspond to microservices in the 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.

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.

Prerequisites

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

Procedure

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

    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 logged on with an Alibaba Cloud account or a RAM user that has RAM permissions, 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 to contact the technical support.

      • If you logged on as a RAM user that does not have permissions on RAM, you must log on to the Function Compute console by using an Alibaba Cloud account. 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.

    For more information about permission management, see Permission Management.

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 a service name and description, configure the following parameters, and then click OK.
    ParameterDescriptionExample
    LoggingSpecify whether to enable the logging feature. Take note of the following items:
    • Enable: Function Compute sends function execution logs to Log Service for persistent storage. You can debug code, troubleshoot issues, and analyze data based on the logs.
    • Disable: You cannot use Log Service to store or query function execution logs.
    Enable
    Advanced settings
    Tracing AnalysisSpecify whether to enable Tracing Analysis. Take note of the following items:
    • Enable: enables Tracing Analysis. 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.
    • Disable: disables Tracing Analysis.
    Enable
    Server RoleConfigure the role for functions in the service so that 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 VPCSpecify whether to allow functions to access virtual private cloud (VPC) resources. For more information, see Configure network settings. Yes
    VPCThis parameter is required if you set Access to VPC 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****
    vSwitchThis parameter is required if you set Access to VPC 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 GroupThis parameter is required if you set Access to VPC 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 InternetSpecify 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 by using 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 Access to Internet to No. Otherwise, the configured static public IP address does not take effect. For more information, see Configure static public IP addresses.
    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: processes HTTP requests or WebSocket requests. If you use a web scenario, we recommend that you select Use Custom Runtime.

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

      Parameter

      Description

      Examples

      Runtime

      Select a programming language, such as Python, Java, PHP, or Node.js. For more information about the runtimes supported by Function Compute, see Manage functions.

      Node.js14

      Code Upload Method

      Select a method to upload the function 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.

      • Upload ZIP: Select and upload the ZIP package that contains your code.

      • Upload JAR: Select and upload the JAR package that contains your code.

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

      • OSS: Specify the Bucket Name and Object Name parameters for the code to be uploaded.

      Note
      • If you set Code Upload Method to Use Sample Code, you do not need to modify Handler. 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. 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 must manually create a startup script named bootstrap in the root directory of the code. You can use the bootstrap script to start your program.

      npm run start

      Listener 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

    • In the Advanced Settings section, 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 capacity to memory capacity (in GB) must be set from 1:1 to 1:4.

      • If original resource plans are used to offset the current resource usage, Function Compute supports offset of vCPU and memory usage at a ratio of 2:3 (1 GB-second = 0.66 vCPU-seconds + 1 GB-seconds). Starting from November 3, 2022, Function Compute supports flexible vCPU-to-memory ratios. However, the value of vCPU must be increased at increments of 0.05. The value of 0.66 vCPU-seconds in the original conversion ratio is no longer supported. You can select 0.60 vCPU-seconds or 0.65 vCPU-seconds.

      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: the default value. You are not charged for using a temporary disk of this 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, which share the space of the temporary hard disk.

      The temporary hard disk is consistent with the lifecycle of the underlying instance. After the instance is recycled by the system, the data on the hard disk is cleared. To persist the stored data, 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 value of a function instance. For more information, see Configure instance concurrency.

      10

      Execution Timeout Period

      The timeout period for the execution of the function. The default value of Execution 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 ends, the function fails to be executed. If you want to apply a longer timeout limit, join the DingTalk group 11721331 to contact the 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 Code Upload Method to Use Sample Code, you do not need to modify Handler. 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 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

    • In the Environment Variables section, configure the environment variables in the runtime environment 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, click the Code tab, and then click Test Function.

    You can also edit the code in the code editor to implement secondary development.

    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 that are provided by Function Compute, see Sample code.

  • 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