When you create a function, you need to specify a deployment package, including a ZIP or JAR package of the function code and dependencies. After developing the application locally or through the Function Compute console, Funcraft, or VSCode plug-in, you need to deploy the package to Function Compute. This topic takes installing the pymysql library of Python as an example to demonstrate how to deploy a package by Funcraft, and how to build a deployment package manually and deploy it by the console.

Background information

Whether the function you deploy has dependencies will determine how and what you package:
  • Deploy a function without dependencies

    If you only need to use standard libraries and built-in libraries of the Function Compute environment without installing additional dependencies, you can use the editor on the Function Compute console, Funcraft, or the VSCode plug-in to write your code. These tools automatically compress the function code and related configuration information into an executable deployment package.

  • Deploy a function with dependencies
    • If you have to develop service code on the Function Compute console and need a third-party dependency (that is, a custom module, such as using the graphics library for image processing and using a database driver), you need to manually upload the deployment package to Function Compute. For more information about how to manually package, see Deploy a package through the console.
    • If you use Funcraft to develop service code, Funcraft can help you install custom modules, package them into a deployment package, and upload the package to Function Compute.
Note If your deployment package is larger than 50 MB, you can upload the deployment package to OSS and provide the address of the deployment package to Function Compute. In this case, you can submit a ticket. You can also simplify the deployment package size, upload large dependencies to NAS, and load dependencies stored to NAS in the Initializer function.

Use Funcraft to deploy

  1. In the root directory of your function, initialize the Funfile file, and select python3.
    $ fun install init
    ? Select a runtime 
      nodejs8 
      nodejs10 
      python2.7 
    ❯ python3 
      java8 
      php7.2 
      dotnetcore2.1 
    (Move up and down to reveal more choices)
    A file named Funfile is generated under the root directory.
  2. Edit the Funfile file.
    RUNTIME python3
    RUN fun-install pip install PyMySQL
  3. Create a template.yml file, for example /tmp/code/template.yml, the content is as follows.
    ROSTemplateFormatVersion: '2015-09-01'
    Transform: 'Aliyun::Serverless-2018-04-03'
    Resources:
     FunDemo: 
       Type: 'Aliyun::Serverless::Service'
       pythondemo: 
         Type: 'Aliyun::Serverless::Function'
         Properties:
           Handler: index.handler
           Runtime: python3
           CodeUri: '. /'
    As for the template.yml file in this example:
    • Service declared: FunDemo service
    • Function declared: pythondemo function
    • Function entry configured: index.handler
    • Function runtime: python3
    • Directory specified by CodeUri: Funcraft packages the directory specified by CodeUri and uploads it to Function Compute.
  4. In the root directory of your function, run the fun install command to install dependencies.
    $ fun install
    using template: template.yml
    start installing function dependencies without docker
    
    building FunDemo/pythondemo
    Funfile exist, Fun will use container to build forcely
    Step 1/2 : FROM registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-python3.6:build-1.9.4
    ---> 702c91653452
    Step 2/2 : RUN fun-install pip install PyMySQL
    ---> Using cache
    ---> 8f59062f15bb
    sha256:8f59062f15bb7a86bd59b85e3b61bd0e4ed711c536fe0cd10fdefebc78eae152
    Successfully built 8f59062f15bb
    Successfully tagged fun-cache-a6f4221c-33c8-4050-93b8-015e42396475:latest
    copying function artifact to /Users/txd123/Desktop/express
    
    Install Success
    
    
    Tips for next step
    ======================
    * Invoke Event Function: fun local invoke
    * Invoke Http Function: fun local start
    * Build Http Function: fun build
    * Deploy Resources: fun deploy
  5. Run the following command to deploy the package.
    fun deploy -y
    If the package is deployed successfully, related logs are printed as follows.
    $ fun deploy -y
    using template: template.yml
    using region: cn-hangzhou
    using accountId: ***********3743
    using accessKeyId: ***********Ptgk
    using timeout: 60
    
    Collecting your services information, in order to caculate devlopment changes...
    
    Resources Changes(Beta version! Only FC resources changes will be displayed):
    
    ┌────────────┬──────────────────────────────┬────────┬──────────┐
    │ Resource   │ ResourceType                 │ Action │ Property │
    ├────────────┼──────────────────────────────┼────────┼──────────┤
    │            │                              │        │ Handler  │
    │            │                              │        ├──────────┤
    │ pythondemo │ Aliyun::Serverless::Function │ Add    │ Runtime  │
    │            │                              │        ├──────────┤
    │            │                              │        │ CodeUri  │
    └────────────┴──────────────────────────────┴────────┴──────────┘
    
    Waiting for service FunDemo to be deployed...
          Waiting for function pythondemo to be deployed...
                  Waiting for packaging function pythondemo code...
                  The function pythondemo has been packaged. A total of 51 files were compressed and the final size was 114.35 KB
          function pythondemo deploy success
    service FunDemo deploy success
  6. In the left-side navigation pane, click Services and Functions.
  7. On the function page, select Code > Invoke.
    The function invocation succeeds and returns the expected result.

Deploy a package through the console

  1. Create a folder to store the code and dependencies.
    mkdir /tmp/code
  2. Download dependencies to directory /tmp/code.
    cd /tmp/code
    pip install -t . PyMySQL
  3. Create a code file, for example /tmp/code/index.py, named pymysql in your code.
    import pymysql.cursors
    # Connect to the database
    connection = pymysql.connect(host='localhost',
                                 user='user',
                                 password='passwd',
                                 db='db',
                                 charset='utf8mb4',
                                 cursorclass=pymysql.cursors.DictCursor)
    def handler(event, context):
     with connection.cursor() as cursor:
         # Read a single record
         sql = "SELECT count(*) FROM `users`"
         cursor.execute(sql)
         result = cursor.fetchone()
         print(result)
         return result
  4. Package as follows.
    • Windows

      Go to the function code folder, select all the files, right-click, and choose Compress into zip package from the drop-down list to generate a code package.

    • Linux

      Specify the source file as all files in the code directory by running the zip command to generate a deployment code package, such as zip code.zip /home/code/*.

  5. In the top navigation bar, select a region.
  6. In the left-side navigation pane, click Services and Functions.
  7. On the function page, select Code > Upload Zip File > Select File. Upload your package, and then click Save and Invoke.
    The function invocation succeeds and returns the expected result.

References

For more information about how to build deployment packages in other languages, see the following documents:
For more information about Funcraft or VSCode, see the following documentation: