通过函数计算的 Custom Runtime,您可以在函数计算平台使用 PowerShell 来编写函数。本文介绍快速部署和调用 PowerShell 函数的详细步骤。

前提条件

您已安装 8.6.0 及以上版本的 Node.js。详情请参见官方下载地址
说明 如果您已安装 Funcraft 可直接跳转至步骤二:部署并调用函数

步骤一:准备环境

安装 Funcraft,最简单的方式就是直接下载可执行的二进制文件。

  1. 安装 Funcraft 到本机。详情请参见安装文档
  2. 执行 fun --version 检查安装是否成功。
  3. 执行 fun config 配置 Funcraft。然后按照提示依次配置 Account IDAccess Key IDAccess Key SecretDefault region name
    $ fun config
    Aliyun Account ID 1234xxx
    Aliyun Access Key ID xxxx
    Aliyun Access Key Secret xxxx
    Default region name cn-xxxx
    The timeout in seconds for each SDK client invoking 300
    The maximum number of retries for each SDK client 5
    Allow to anonynously report usage statistics to improve the tool over time? (Y/n)

步骤二:部署并调用函数

  1. 执行以下命令克隆示例工程到本地。
    git clone https://github.com/awesome-fc/fc-custom-demo          
    说明 如果您没有安装 Git,可以直接在浏览器地址栏输入 https://github.com/awesome-fc/fc-custom-demo,然后单击下载按钮直接下载。
  2. 执行以下命令进入克隆的示例项目中。
    cd fc-custom-demo
    cd powershell-demo       
  3. 执行以下命令安装依赖。
    $ fun install -v 
    start installing function dependencies without docker
    
    building powershell-demo/fc-powershell
    Funfile exist, Fun will use container to build forcely
    Step 1/7 : FROM registry.cn-beijing.aliyuncs.com/aliyunfc/runtime-custom:build-1.9.4
    ...
    Step 7/7 : RUN fun-install apt-get install powershell
    ...
    sha256:7c6476a3a4496bbf3da83bfb8966acc90fa94f4f8baccd248cd79c18c4fae409
    Successfully built 7c6476a3a449
    Successfully tagged fun-cache-965bbabb-ab6f-44e7-9910-7dd1df1d3c3e:latest
    copying function artifact to /Users/songluo/gitpro/fc-custom-demo/powershell-demo
    copy from container /mnt/auto/. to localNasDir
    
    Install Success     
  4. 执行以下命令将项目部署至函数计算。
    $ fun deploy -y
    ....
    ? Do you want to let fun to help you automate the configuration? Yes
    ? You have already configured 'NasConfig: Auto’. We want to use this configuration to store your function dependencies. Yes
    
    Fun automatically backups the original template.yml file to /Users/txd123/Desktop/jack/fc-custom-demo/powershell-demo/.template.yml.backup
    Fun add .fun/root to /Users/txd123/Desktop/jack/fc-custom-demo/powershell-demo/.nas.yml
    Fun add environment variables to 'powershellDemo/fc-powershell' in /Users/txd123/Desktop/jack/fc-custom-demo/powershell-demo/template.yml
    
    starting upload /Users/txd123/Desktop/jack/fc-custom-demo/powershell-demo/.fun/root to nas://powershellDemo/mnt/auto/root/
    
    start fun nas init...
    ....
    fun nas init Success
    
    zipping /Users/txd123/Desktop/jack/fc-custom-demo/powershell-demo/.fun/root
    ✔ upload done
    unzipping file
    ✔ unzip done
    ✔ upload completed!
    
    ....
    ? Region cn-qingdao only supports capacity NAS. Do you want to create it automatically? Yes
            ....
        Waiting for function fc-powershell to be deployed...
            Waiting for packaging function fc-powershell code...
            The function fc-powershell has been packaged. A total of 3 files were compressed and the final size was 1.31 KB
        function fc-powershell deploy success
        function fc-powershell deploy success
    service powershellDemo deploy success       
  5. 执行以下命令调用部署的函数。
    $ fun invoke -e "Hello World"
    ...
    ========= FC invoke Logs begin =========
    FC Invoke Start RequestId: cd30369e-7dfa-439c-a68d-7fe16d5a7e05
    Hello World
    FC Invoke End RequestId: cd30369e-7dfa-439c-a68d-7fe16d5a7e05
    
    Duration: 54.13 ms, Billed Duration: 100 ms, Memory Size: 512 MB, Max Memory Used: 133.70 MB
    ========= FC invoke Logs end =========
    
    FC Invoke Result:
    Hello World