与 CloudFront 分配关联的 Lambda 函数无效或没有所需的权限

The Lambda function associated with the CloudFront distribution is invalid or doesn't have the required permissions

因此,作为借口,我几乎不知道该怎么办。我研究了大约两个小时,通常我会继续研究,但 none 我发现的信息很有用。我怀疑这与 YAML (serverless.yml) 文件有关,但我不确定。我对该文件进行了多次更新,因此我将 post 初始代码和当前代码,但没有任何区别。该代码在开发中完美运行,但在生产中抛出错误。你可以看到https://www.evote.space复制这个。

当前

myNextApplication:
  service: myService
  component: "@sls-next/serverless-component@1.18.0"
  provider:
    name: aws
    runtime: nodejs12.x
    stage: dev
    profile: evote
    iam:
    role: rolenamegoesherebutnotonWhosebug
  inputs:
    domain: "evote.space"
  functions:
    createuser:
      handler: data.createuser
    readTable:
      handler: data.readTable
  resources:
    Resources:
      usersTable:
        Type: AWS::DynamoDB::Table
        Properties:
          TableName: Users
          AttributeDefinitions:
          - AttributeName: userHash
            AttributeType: N
          KeySchema:
          - AttributeName: userHash
            KeyType: HASH
      votersTable:
        Type: AWS::DynamoDB::Table
        Properties:
          TableName: Voters
          AttributeDefinitions:
          - AttributeName: voterHash
            AttributeType: N
          KeySchema:
          - AttributeName: voterHash
            KeyType: HASH
      electionsTable:
        Type: AWS::DynamoDB::Table
        Properties:
          TableName: Elections
          AttributeDefinitions:
          - AttributeName: electionHash
            AttributeType: N
          KeySchema:
          - AttributeName: electionHash
            KeyType: HASH
      ballotsTable:
        Type: AWS::DynamoDB::Table
        Properties:
          TableName: Ballots
          AttributeDefinitions:
          - AttributeName: ballotHash
            AttributeType: N
          KeySchema:
          - AttributeName: ballotHash
            KeyType: HASH

初始(第一次部署时出错)

myNextApplication:
  service: myService
  component: "@sls-next/serverless-component@1.18.0"
  provider:
    name: aws
    runtime: nodejs12.x
    stage: dev
    profile: evote
  inputs:
    domain: "evote.space"

我的代码库很大,由许多页面和组件组成。到目前为止,我所做的只是一个登录功能,但在注册页面上它调用 api 到 return 用户(用于重复电子邮件验证),它 return 是错误我们都非常熟悉“Unexpected token < in JSON at position 0”,如果您返回然后再次加载页面,您可以让控制台显示该错误的来源,内容如下:

503 ERROR The request could not be satisfied. The Lambda function associated with the CloudFront distribution is invalid or doesn't have the required permissions. We can't connect to the server for this app or website at this time. There might be too much traffic or a configuration error. Try again later, or contact the app or website owner. If you provide content to customers through CloudFront, you can find steps to troubleshoot and help prevent this error by reviewing the CloudFront documentation. Generated by cloudfront (CloudFront) Request ID: No0_qVJ3gcOpg48rMXqvgyipx4wKWmV-hRewQblZ-loyaaiVJLqGIA==

所以是的,如果你能帮忙,请帮忙。

编辑: 导致问题的代码是以下块

NewUser.getInitialProps = async ({ req }) => {
  if (req) {
    // this is server side
    return {
      users: await data.readTable("Users")
    };
  } else {
    // we are client side
    const response = await fetch("/api/users");
    return { users: await response.json() };
  }
};

而处理此问题的 api 最初看起来像这样:

import data from "../../../data"

export default async (req, res) => {
  console.log("/api/users HIT!");
  res.status(200).json(await data.readTable("Users"));
};

但我更改了它以便将其标记为 lambda,所以现在它看起来像这样(尽管没有区别):

import data from "../../../data";

module.exports.read = async (event, context, callback) => {
  console.log("/api/users HIT!");
  callback(null, {statusCode: 200}).json(await data.readTable("Users"));
}

因此,在仔细研究之后,我做了以下操作,这显然是一个很常见的问题,所以我建议其他遇到此问题的人完全按照以下操作操作。请记住,这是与 serverless-nextjs 组件一起使用的,而不仅仅是无服务器框架,尽管同样适用于那里:

  1. 我仔细研究了存储库 README 并遵循了所有说明。
  2. 我登录 AWS 控制台,发现我认为是主要问题,但实际上是第三问题; Lambda@Edge 仅配置了基本的 CloudWatch 日志记录权限。我用所有必要的权限更新了它,同时确保最佳实践
  3. 更新您的 Lambda 后,确保将其部署到 Lambda@Edge(在操作菜单下)
  4. 然后我导航到 CloudWatch 并检查了正确区域中的日志,并在调用 API 后发现错误。
  5. 不知何故,我的 DynamoDB 表被删除或未正确复制,所以我使用 npm 运行 infra 和之后从我的 SDK 重新部署它们。

这种体验总是很好的。对于代码中真正愚蠢的错误,您绝不是无敌的。在 99% 的情况下,如果您遇到困难并认为它很复杂并且修复它是不现实的,请退后一步问,“它可能是最愚蠢的事情是什么?”

而且一直如此。

对我来说,使用无服务器的 Alpha 版本,解决了这个错误消息 '与 CloudFront 分配关联的 Lambda 函数无效或没有所需的权限'

serverless.yml 文件:

# component: "@sls-next/serverless-component@1.19.0"
component: "@sls-next/serverless-component@3.4.0-alpha.12"

我们可以在这里找到最新版本:https://www.npmjs.com/package/@sls-next/serverless-component