tencent cloud


Debug (native invoke) locally

Last updated: 2022-03-15 16:13:40
This document is currently invalid. Please refer to the documentation page of the product.

    Through the local debugging capability, SCF CLI can run the code in the local simulation environment, send simulation test events, and obtain the running log, time consumption, memory occupation and other information of the function code.

    Dependent component

    Local debugging of native does not need to rely on Docker, to ensure that the Node.js environment is installed on the system. The current native command only supports Node.js and Python runtime. To ensure that the results of the deployment cloud and the local operation are consistent, it is recommended that the locally installed version of runtime is the same as the cloud version. For example, if you use Node.js 6.10 in the cloud, it is recommended that you also install Node.js 6.x.

    Debug command

    SCF CLI passed native invoke The subcommand completes the local trigger run. The SCF command line tool will run the corresponding code in the specified Directory on the local machine according to the specified function template configuration file, and run the local cloud function simulation through the specified trigger event.

    Parameter description

    scf native invoke The parameters supported by the command are as follows:

    Parameters Required Description Example
    EVENT No The source of the file that simulates the test event, and the content of the file must be in JSON format. Event.json
    Template No The path or file name of the project description profile, which defaults to template.yaml. Template.yaml
    Env-vars No Function runtime environment variable configuration, you need to specify the environment variable configuration file, the content must be in JSON format. Env.json
    Debug-port No The port of Open when the function is running. After the port is specified, the local runtime will specify the port in debug mode Launch and Open. 3366
    Debug-args No The debugger Launch parameter in this machine. After the parameter is specified, the debugger Launch will pass the specified parameter. None

    The supporting options FUNCTION_IDENTIFIER are described as follows:

    Parameters Required Description Example
    FUNCTION_IDENTIFIER No Indicates the identity and name of the function; if there are multiple function descriptions in the project description configuration file, you can specify the function to be debugged through this parameter. Hello_world

    Test simulation event

    Simulated events used to trigger cloud functions locally can be passed through Linux's command pipeline or through files.

    • Pass through the command pipeline: scf native invoke The command supports receiving events from the command line pipeline.
    • Can be implemented by scf native generate-event Command generates an event and passes it, forming, for example, scf native generate-event cos post | scf native invoke Debug commands for.
    • You can also construct and output content in JSON format and pass it to scf native invoke Command, forming, for example echo '{"test":"value"}' | scf native invoke Debug commands for.
    • ** Pass by file: ** By using the scf native invoke Ordered --event Parameter that specifies the file that contains the contents of the test simulation event. The contents of the file must be JSON data structures, such as scf native invoke --event event.json Debug commands for.

    Usage Examples

    Passing through scf init The sample project obtained by initialization contains the prepared code file and template configuration file. Take the example project as an example, assuming that the environment is Node.js 8.9, /Users/xxx/code/scf Directory created a hello_world project.
    We pass the simulation event of the cos post file through the command pipeline, triggering the function to run. The function code simply prints event and returns "hello world". Function code /Users/xxx/code/scf/testproject/hello_world/main.js Example:

    'use strict';
    exports.main_handler = async (event, context, callback) => {
        console.log("%j", event);
        return "hello world"
    1. Execute scf native generate-event cos post | scf native invoke Command, the Launch function runs locally.
    Enter a event: [0m
    START RequestId: 3e3e71c9-dc56-1967-c0a3-3a454e2ce634
    END RequestId: 3e3e71c9-dc56-1967-c0a3-3a454e2ce634
    REPORT RequestId: 3e3e71c9-dc56-1967-c0a3-3a454e2ce634  Duration: 1.91 ms
    Billed Duration: 100 ms Memory Size: 128 MB     Max Memory Used: 20 MB
    "hello world"

    From the output, you can see that after the local operation of the function is completed, the printed log of the function and the content returned by the function are output.
    2. Generate the following event.json test event file:

    1. Execute scf native invoke --event event.json Command, the Launch function runs locally and outputs test events through the file.
    Enter a event: [0m
    START RequestId: 6d06b0cf-4cc9-1f76-5f92-1f5871ff110a
    END RequestId: 6d06b0cf-4cc9-1f76-5f92-1f5871ff110a
    REPORT RequestId: 6d06b0cf-4cc9-1f76-5f92-1f5871ff110a  Duration: 1.72 ms
    Billed Duration: 100 ms Memory Size: 128 MB     Max Memory Used: 20 MB
    "hello world"

    As you can see from the output, the function code prints the test event and returns the specified content.

    Contact Us

    Contact our sales team or business advisors to help your business.

    Technical Support

    Open a ticket if you're looking for further assistance. Our Ticket is 7x24 avaliable.

    7x24 Phone Support