无服务器.yml环境变量问题

时间:2019-12-10 19:00:12

标签: aws-lambda environment-variables serverless

我在无服务器的this页面上,进行Lambdas和Dynamo DB练习。

我在“添加创建笔记API”部分中,但在测试API时遇到问题 命令:

serverless invoke local --function create --path mocks/create-event.json

产生此错误

Serverless: DOTENV: Loading environment variables from .env:
Serverless:      - tableName
Serverless: Bundling with Webpack...
error is : ResourceNotFoundException: Requested resource not found
    at Request.extractError (/Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/protocol/json.js:51:27)
    at Request.callListeners (/Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/sequential_executor.js:106:20)
    at Request.emit (/Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (/Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/request.js:683:14)
    at Request.transition (/Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/request.js:685:12)
    at Request.callListeners (/Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/sequential_executor.js:116:18)
    at Request.emit (/Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/sequential_executor.js:78:10)
    at Request.emit (/Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/request.js:683:14)
    at Request.transition (/Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/request.js:22:10)
    at AcceptorStateMachine.runTo (/Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/state_machine.js:14:12)
    at /Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/state_machine.js:26:10
    at Request.<anonymous> (/Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/request.js:38:9)
    at Request.<anonymous> (/Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/request.js:685:12)
    at Request.callListeners (/Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/sequential_executor.js:116:18)
    at callNextListener (/Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/sequential_executor.js:96:12)
    at IncomingMessage.onEnd (/Users/dannybyrne/notes-app-api/node_modules/aws-sdk/lib/event_listeners.js:307:13)
    at IncomingMessage.emit (events.js:215:7)
    at IncomingMessage.EventEmitter.emit (domain.js:476:20)
    at endReadableNT (_stream_readable.js:1183:12)
    at processTicksAndRejections (internal/process/task_queues.js:80:21) {
  message: 'Requested resource not found',
  code: 'ResourceNotFoundException',
  time: 2019-12-10T18:33:00.494Z,
  requestId: '4D2O3VOI437AE9MTH1M4UPGDUVVV4KQNSO5AEMVJF66Q9ASUAAJG',
  statusCode: 400,
  retryable: false,
  retryDelay: 28.999931507407407
}
{
    "statusCode": 500,
    "headers": {
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Credentials": true
    },
    "body": "{\"status\":false}"
}

我已将.env文件编辑为

tableName='notes'

.yml文件中的内容是

# These environment variables are made available to our functions
  # under process.env.
  environment:
    tableName: notes

我假设这意味着在create.js文件中的params对象下

TableName: process.env.tableName,

在说明中说 “我们使用process.env.tableName从环境变量中读取DynamoDB表的名称。我们将在下面的serverless.yml中进行设置。这样做是因为我们不必在每个函数中对其进行硬编码。”

这是否意味着我不必在包含的.env文件中进行设置?当我重命名该文件以使其不包含在构建中时,它还会引发错误提示。当我在.env中定义tableName并将其设置为'notes'时(如.yml文件中定义的那样)并且是dynamoDB表的名称时,它将引发以上错误。

我基本上是想弄清楚.env中是否有.yml拉动

1 个答案:

答案 0 :(得分:0)

是的,如果您在提供程序下定义了变量,那么它将对所有函数可用。