Last updated: 2019-08-08 15:47:44PDF
Function code is the most important part of an SCF function. You upload the application code to the SCF platform in the form of SCF function, and the function executes the code on your behalf and performs all related server management tasks.
The lifecycle of an SCF-based application typically involves writing code, creating the function, deploying the function to the SCF platform, testing, monitoring, and troubleshooting. This section describes everything related to function code. For details on monitoring and troubleshooting, see the section for function monitoring and log.
Writing Code for SCF
You need to write function code in a language supported by the SCF platform. When writing code, you can choose any code writing tool such as SCF console, local editor or local IDE. It should be noted that if your code references other dependent libraries that have not been referenced by the platform, you must upload these dependent libraries. For more information about the dependent libraries provided by the platform, see Runtime environment. For more information about how to upload the code, see Creating a Deployment Package.
Currently, supported programming languages include Python, Node.js, PHP, and JAVA. For the code writing methods and features in each language, see Notes on Programming Languages.
In addition, the SCF platform provides a set of basic paradigms for function writing about how to determine the initial calling method of the function, how to get information from the parameters, how to output the log, and how to interact with the current runtime environment, among others. For the specific function paradigms, see Writing and Handling Methods.
Creating a Deployment Package
You need to provide code or deployment package:
If all the libraries used in your code are standard Python libraries or libraries provided by Tencent Cloud (such as SDKs for various cloud products), you only need to provide the code in the console, and the SCF platform will automatically package the code file and upload it.
If you need to reference an external library, please organize your code and dependencies in the way specified in Creating a Deployment Package, package them and upload to the SCF platform.
When creating a function by uploading a zip package, you should also pay attention to the packaging method and "execution method":
The execution method is in
a.bformat, where a is the name of the .py file, and b is the name of the method in the code. If a file named
a.pycannot be found in the root directory of the unzipped folder, the system will prompt that "Failed to create the function. Please retry" or "The function code cannot be displayed as the file specified by the execution method cannot be found in the zip package of the code".
For example, a folder structure is as follows:
When you create the zip package, if SecondFolder is zipped, the error above will occur; instead, you should select
Creating and Deploying an SCF Function
You can create a function through the SCF console, API, SDK, or TCCLI. First, you need to provide the configuration information for the function, including computing resources and runtime environment. For details, see Creating an SCF Function.
Testing and Triggering an SCF Function
You can test a function in the following ways:
- Click Test in the console to test the function.
- Test the function using the API, SDK or InvokeFunction method of TCCLI.
You need to provide the call data when testing. You can test whether the function responds to an event generated by a specific cloud product (such as COS) as you expect by passing in the call data of the product.
For more information about trigger configuring methods and event data generated by different cloud products, see Managing a Function Trigger.
Monitoring and Troubleshooting
When a function enters the production environment, SCF will automatically monitor its execution conditions. Function metrics will be reported to the Cloud Monitor platform, so that you can check the execution status of the function by yourself.
To help you debug and troubleshoot, the SCF platform will record all the calls and handling results of the function, and store the outputs generated by your code in the form of logs. For more information, see function logs.
Sample SCF-based Applications
Please make sure to read and try the samples in the following sections before using SCF:
- Getting Started: If you are using SCF for the first time, please read and try all the steps in the getting started section.
- Code Practices: If you need to reference an external library, you must create your code package in your local environment and upload it to the SCF platform. Please read and try the corresponding steps in the samples based on the selected programming language and the events to be handled.