'serverless invoke -f hello'给出KeyError

问题描述

我正在关注一个教程,以学习如何使用无服务器框架。目标是部署Django应用程序。本教程建议将必要的环境变量放在单独的yml文件中。不幸的是,按照教程进行操作会给我带来KeyError。 我有一个serverless.yml,variables.yml和handler.py。我将对所有下面的代码以及给定的错误进行认证。

serverless.yml:

service: serverless-django
custom: ${file(./variables.yml)}
provider:
  name: aws
  runtime: python3.8
functions:
  hello:
    environment: 
      - THE_ANSWER: ${self:custom.THE_ANSWER}
    handler: handler.hello

variables.yml:

THE_ANSWER: 42

handler.py:

import os

def hello(event,context):
    return {
        "statusCode": 200,"body": "The answer is: " + os.environ["THE_ANSWER"]
    }

我的终端中的错误

{
    "errorMessage": "'THE_ANSWER'","errorType": "KeyError","stackTrace": [
        "  File \"/var/task/handler.py\",line 7,in hello\n    \"body\": \"The answer is: \" + os.environ[\"THE_ANSWER\"]\n","  File \"/var/lang/lib/python3.8/os.py\",line 675,in __getitem__\n    raise KeyError(key) from None\n"
    ]
}
 
  Error --------------------------------------------------
 
  Error: Invoked function Failed
      at AwsInvoke.log (/snapshot/serverless/lib/plugins/aws/invoke/index.js:105:31)
      at AwsInvoke.tryCatcher (/snapshot/serverless/node_modules/bluebird/js/release/util.js:16:23)
      at Promise._settlePromiseFromHandler (/snapshot/serverless/node_modules/bluebird/js/release/promise.js:547:31)
      at Promise._settlePromise (/snapshot/serverless/node_modules/bluebird/js/release/promise.js:604:18)
      at Promise._settlePromise0 (/snapshot/serverless/node_modules/bluebird/js/release/promise.js:649:10)
      at Promise._settlePromises (/snapshot/serverless/node_modules/bluebird/js/release/promise.js:729:18)
      at _drainQueueStep (/snapshot/serverless/node_modules/bluebird/js/release/async.js:93:12)
      at _drainQueue (/snapshot/serverless/node_modules/bluebird/js/release/async.js:86:9)
      at Async._drainQueues (/snapshot/serverless/node_modules/bluebird/js/release/async.js:102:5)
      at Immediate._onImmediate (/snapshot/serverless/node_modules/bluebird/js/release/async.js:15:14)
      at processImmediate (internal/timers.js:456:21)
      at process.topLevelDomainCallback (domain.js:137:15)
 
     For debugging logs,run again after setting the "SLS_DEBUG=*" environment variable.
 
  Get Support --------------------------------------------
     Docs:          docs.serverless.com
     Bugs:          github.com/serverless/serverless/issues
     Issues:        forum.serverless.com
 
  Your Environment information ---------------------------
     Operating System:          linux
     Node Version:              12.18.1
     Framework Version:         2.0.0 (standalone)
     Plugin Version:            4.0.2
     SDK Version:               2.3.1
     Components Version:        3.1.2

我正在尝试的命令是'sls invoke -f hello'。命令“ sls deploy”已经成功执行。

我是无服务器新手,所以请让我知道如何解决此问题,或者是否需要更多信息。

解决方法

首先,yml脚本中存在错误:

无服务器错误---------------------------------------

环境变量0中的无效字符

The error is defining environment variables as an array instead of key-value pairs

然后在部署之后,一切都会顺利进行( sls deploy -v

sls调用--f你好

{ “状态代码”:200, “ body”:“答案是:42” }


serverless.yml

service: sls-example

custom: ${file(./variables.yml)}

provider:
  name: aws
  runtime: python3.8

functions:
  hello:
    environment:
        THE_ANSWER: ${self:custom.THE_ANSWER}
    handler: handler.hello

variables.yml

THE_ANSWER: 42