【问题标题】:How define lambda function in serverless with inlineCode?如何使用 inlineCode 在无服务器中定义 lambda 函数?
【发布时间】:2019-12-06 16:22:50
【问题描述】:

考虑一段无服务器代码:

functions:
  MyFunc:
    handler: index.handler
    name: "my_name"
    runtime: nodejs12.x
    memorySize: 512
    timeout: 30
    inlineCode: |
      exports.handler = function(event, context) {
          console.log("ok");
      };
    description: description 

这会导致打包源文件夹中的所有内容。我无法禁用它。如果我添加事件:

package:
  artifact: dummy.zip

部署失败,因为 dummy.zip 是空文件。但是为什么我在指定inlineCode 时需要一个 zip 文件?有没有办法只用inlineCode参数禁用打包和部署nodejs功能?

【问题讨论】:

    标签: javascript node.js aws-lambda serverless-framework


    【解决方案1】:

    解决方法是将 lambda 函数定义定义为普通的 cloudformation 资源,如下所示:

    resources:
      Resources:
        MyFunc:
          Type: AWS::Lambda::Function
          Properties:
            FunctionName: "my_name"
            Handler: index.handler
            Runtime: nodejs10.x
            Role: !GetAtt LambdaRole.Arn # do not forget to define role by hand :(
            Code:
              ZipFile: |
                exports.handler = function(event, context, callback) {
                console.log(event);
                    const response = {
                        statusCode: 200,
                        body: JSON.stringify('Hello Node')
                    };
                    callback(null, response);
                };
    

    【讨论】:

      【解决方案2】:

      AWS::Serverless::Function 支持inlineCode 参数的概念,但serverless-framework 不支持。您粘贴的 YAML 不是到 AWS::Serverless::Function 的 1:1 映射,它特定于 sls 本身。

      将您的代码存储在文件/目录中,直到 sls 团队添加对 inlineCode 的支持。我没有看到任何功能请求。我相信他们会很高兴从你这里得到一个。

      【讨论】:

      • `我相信他们会很高兴从你这里得到一个。`-如果我有时间... :-)
      猜你喜欢
      • 2021-07-09
      • 1970-01-01
      • 2019-02-24
      • 1970-01-01
      • 2020-12-21
      • 1970-01-01
      • 2018-01-21
      • 2021-04-25
      • 2021-10-07
      相关资源
      最近更新 更多