Esta página ainda não está disponível no idioma selecionado. Estamos trabalhando para adicionar mais opções de idiomas. Agradecemos sua compreensão.

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

Creating an HTTP Function

Updated on 2024-08-07 GMT+08:00

Overview

HTTP functions are designed to optimize web services. You can send HTTP requests to URLs to trigger function execution. HTTP functions support APIG triggers only.

NOTE:
  • HTTP functions do not distinguish between programming languages. The handler must be set in the bootstrap file. You can directly write the startup command, and allow access over port 8000. The bound IP address is 127.0.0.1.
  • The bootstrap file is the startup file of the HTTP function. The HTTP function can only read bootstrap as the startup file name. If the file name is not bootstrap, the service cannot be started. For more information, see the bootstrap file example.
  • HTTP functions support multiple programming languages.
  • Functions must return a valid HTTP response.
  • This section uses Node.js as an example. To use another runtime, simply change the runtime path. The code package path does not need to be changed. For the paths of other runtimes, see Table 1.
  • When a function initiates an HTTP request, the request IP address is dynamic for private network access and fixed for public network access. For more information, contact technical support.

Prerequisites

  1. Prepare a Node.js script. A code example is as follows:
    const http = require('http'); // Import Node.js core module
     
    var server = http.createServer(function (req, res) {   //create web server
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.write('<html><body><h2>This is http function.</h2></body></html>');
        res.end();
    });
     
    server.listen(8000, '127.0.0.1'); //6 - listen for any incoming requests
     
    console.log('Node.js web server at port 8000 is running..')
  2. You have prepared a bootstrap file as the startup file of the HTTP function.

    Example

    The content of the bootstrap file is as follows:

    /opt/function/runtime/nodejs12.13/rtsp/nodejs/bin/node $RUNTIME_CODE_ROOT/index.js
  3. Compress the preceding two files into a ZIP package.
    Figure 1 Compressing files into a ZIP package
    NOTE:

    For HTTP functions in Python, add the -u parameter in the bootstrap file to ensure that logs can be flushed to the disk. Example:

    /opt/function/runtime/python3.6/rtsp/python/bin/python3 -u $RUNTIME_CODE_ROOT/index.py

    To use another runtime, change the runtime path by referring to Table 1. The code package path does not need to be changed.

    Table 1 Paths for different runtimes

    Runtime

    Path

    Java 8

    /opt/function/runtime/java8/rtsp/jre/bin/java

    Java 11

    /opt/function/runtime/java11/rtsp/jre/bin/java

    Node.js 6

    /opt/function/runtime/nodejs6.10/rtsp/nodejs/bin/node

    Node.js 8

    /opt/function/runtime/nodejs8.10/rtsp/nodejs/bin/node

    Node.js 10

    /opt/function/runtime/nodejs10.16/rtsp/nodejs/bin/node

    Node.js 12

    /opt/function/runtime/nodejs12.13/rtsp/nodejs/bin/node

    Node.js 14

    /opt/function/runtime/nodejs14.18/rtsp/nodejs/bin/node

    Node.js 16

    /opt/function/runtime/nodejs16.17/rtsp/nodejs/bin/node

    Node.js 18

    /opt/function/runtime/nodejs18.15/rtsp/nodejs/bin/node

    Python 2.7

    /opt/function/runtime/python2.7/rtsp/python/bin/python

    Python 3.6

    /opt/function/runtime/python3.6/rtsp/python/bin/python3

    Python 3.9

    /opt/function/runtime/python3.9/rtsp/python/bin/python3

Procedure

  1. Create a function.
    1. Create an HTTP function. For details, see Creating an Event Function. Pay special attention to the following parameters:
      • Function Type: HTTP function
      • Region: Select a region where you will deploy your code.
    2. Choose Upload > Local ZIP, upload the ZIP package, and click Deploy.
      Figure 2 Uploading a ZIP file
  2. Create a trigger.
    NOTE:

    HTTP functions support APIG triggers only.

    1. On the function details page, choose Configuration > Triggers and click Create Trigger.
    2. Set the trigger information. This step uses an APIG (dedicated) trigger as an example. For more information, see Using an APIG (Dedicated) Trigger.

      NOTE:

      In this example, Security Authentication is set to None. You need to select an authentication mode based on site requirements.

      • App: AppKey and AppSecret authentication. This mode is of high security and is recommended.
      • IAM: IAM authentication. This mode grants access permissions to IAM users only and is of medium security.
      • None: No authentication. This mode grants access permissions to all users.
    3. When the configuration is complete, click OK. After the trigger is created, API_test_http will be generated on the APIG console.
  3. Publish the API.
    1. On the Triggers tab page, click an API name to go to the API overview page.
    2. Click Edit in the upper right corner. The Basic Information page is displayed.
      Figure 3 Editing an API
    3. Click Next. On the Define API Request page that is displayed, change Path to /user/get and click Finish.
      Figure 4 Defining an API request
    4. Click Publish API. On the displayed page, click Publish.
  4. Trigger a function.
    1. Go to the FunctionGraph console, choose Functions > Function List in the navigation pane, and click the created HTTP function to go to its details page.
    2. Choose Configuration > Triggers, copy the URL, and access it using a browser.
      Figure 5 Copying the URL
    3. View the request result.
      Figure 6 Viewing the request result

Common Function Request Headers

The following table lists the default request header fields of an HTTP function.

Table 2 Default request header fields

Field

Description

X-CFF-Request-Id

ID of the current request

X-CFF-Memory

Allocated memory

X-CFF-Timeout

Function timeout duration

X-CFF-Func-Version

Function version

X-CFF-Func-Name

Function name

X-CFF-Project-Id

Project ID

X-CFF-Package

App to which the function belongs

X-CFF-Region

Current region

Usamos cookies para aprimorar nosso site e sua experiência. Ao continuar a navegar em nosso site, você aceita nossa política de cookies. Saiba mais

Feedback

Feedback

Feedback

0/500

Selected Content

Submit selected content with the feedback