tencent cloud

Feedback

Debug (local invoke) locally

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

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

    Dependent component

    Before running local debugging, make sure that Launch Docker is installed in the local environment.

    Debug command

    SCF CLI passed local invoke The subcommand completes the local trigger run. The SCF command line tool will mount the code Directory to the specified Directory of the container instance according to the specified function template configuration file and the Launch container instance, and run the code through the specified trigger event to run the local cloud function simulation.

    Parameter description

    scf local 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 container will specify the port in debug mode Launch and Open. 3366
    Debugger-path No The debugger path in this machine. After the path is specified, the debugger will be mounted to the container by the container runtime. / root/debugger/pydev
    Debug-args No The debugger Launch parameter in this machine. After the parameter is specified, the debugger Launch will pass the specified parameter. None
    Docker-volume-basedir No Specifies the path to mount to the container. / User/xxx/code/project
    Docker-network No Specifies the network used by the container, using bridge mode by default. Bridge
    Log-file No Specifies that the log is output to a file. / User/xxx/code/project/log.txt
    Skip-pull-image No Skip checking and pulling the new container Image. 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 local invoke The command supports receiving events from the command line pipeline.
    • Can be implemented by scf local generate-event Command generates an event and passes it, forming, for example, scf local generate-event cos post | scf local invoke Debug commands for.
    • You can also construct and output content in JSON format and pass it to scf local invoke Command, forming, for example echo '{"test":"value"}' | scf local invoke Debug commands for.
    • ** Pass by file: ** By using the scf local 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 local invoke --event event.json Debug commands for.

    Usage Examples

    Passing through scf init The sample project obtained by initialization comes with the prepared code file and template configuration file. Take this example project as an example, assuming that the environment is Python 2.7, /Users/xxx/code/scf Directory created a testproject 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.py Example:

    # -*- coding: utf8 -*-
    def main_handler(event, context):
        print(event)
        return "hello world"
    
    1. Execute scf local generate-event cos post | scf local invoke Command, the Launch function runs locally.
    $ scf local generate-event cos post | scf local invoke 
    read event from stdin
    pull image ccr.ccs.tencentyun.com/scfrepo/scfcli:python3.6......
    START RequestId: 766e10b0-fd41-42ed-acd4-c161833e3bd2
    {'Records': [{'cos': {'cosSchemaVersion': '1.0', 'cosObject': {'url': 'http://testpic-1253970026.cos.ap-guangzhou.myqcloud.com/testfile', 'meta': {'Content-Type': '', 'x-cos-request-id': 'NWMxOWY4MGFfMjViMjU4NjRfMTUyMV8yNzhhZjM='}, 'key': '/1253970026/testpic/testfile', 'vid': '', 'size': 1029}, 'cosBucket': {'region': 'gz', 'name': 'testpic', 'appid': '1253970026'}, 'cosNotificationId': 'unkown'}, 'event': {'eventVersion': '1.0', 'eventTime': 1545205770, 'requestParameters': {'requestSourceIP': '59.37.125.38', 'requestHeaders': {'Authorization': 'q-sign-algorithm=sha1&q-ak=AKIDQm6iUh2NJ6jL41tVUis9KpY5Rgv49zyC&q-sign-time=1545205709;1545215769&q-key-time=1545205709;1545215769&q-header-list=host;x-cos-storage-class&q-url-param-list=&q-signature=098ac7dfe9cf21116f946c4b4c29001c2b449b14'}}, 'eventName': 'cos:ObjectCreated:Post', 'reqid': 179398952, 'eventSource': 'qcs::cos', 'eventQueue': 'qcs:0:lambda:cd:appid/1253970026:default.printevent.$LATEST', 'reservedInfo': ''}}]}
    END RequestId: 766e10b0-fd41-42ed-acd4-c161833e3bd2
    REPORT RequestId: 766e10b0-fd41-42ed-acd4-c161833e3bd2 Duration: 0 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 15 MB
    "hello world"

    From the output, you can see that after the function has been run, it outputs the printed log of the function and the content returned by the function.
    2. Generate the following event.json test event file.

    {
    "key1":"value1",
    "key2":"value2"
    }
    1. Execute scf local invoke --event event.json Command, the Launch function runs locally and outputs test events through the file.
    $ scf local invoke --event event.json 
    pull image ccr.ccs.tencentyun.com/scfrepo/scfcli:python3.6......
    START RequestId: 4a06d73d-e716-4e58-bc5f-ecfc955d77bd
    {'key1': 'value1', 'key2': 'value2'}
    END RequestId: 4a06d73d-e716-4e58-bc5f-ecfc955d77bd
    REPORT RequestId: 4a06d73d-e716-4e58-bc5f-ecfc955d77bd Duration: 0 ms Billed Duration: 100 ms Memory Size: 128 MB Max Memory Used: 15 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