正在获得502响应,并且已对我的lambda函数运行简单的提取请求,并且已被CORS策略阻止

问题描述 投票:1回答:1

[使用无服务器框架在AWS上构建无服务器Web应用程序,我收到一个CORS错误,并带有针对AWS Cognito用户池进行身份验证的502响应代码

GET https://URL.amazonaws.com/dev/asset/ID-1178 502
index.html:1 Access to fetch at 'https://URL.amazonaws.com/dev/asset/PO-TIENDA1178' from origin 'http://localhost:3000' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled.
index.js:109 Uncaught (in promise) TypeError: Failed to fetch

几乎相同的请求适用于另一个功能。

这是从前端发送的两个ajax请求:


// working just fine

        async function getAllAssets() {
            const getAssetsUrl = _config.api.invokeUrl + "/assets"
            const response = await fetch(getAssetsUrl, {
                headers: { 
                    Authorization: authToken
                },
                type: "GET",
                dataType: 'json',
                crossDomain: true
            })
        }

// not working, throwing the error described above

        async function getOneAsset() {
            const getAssetsUrl = _config.api.invokeUrl + "/asset/" + "ID-1178"
            const response = await fetch(getAssetsUrl, {
                headers: { 
                    Authorization: authToken
                },
                type: "GET",
                dataType: 'json',
                crossDomain: true
            })
        }

我在同一窗口中的onDocReady上运行这两个函数。

这是serverless.yaml中的定义:

  # WORKS 👌🏽
  getAssets:
    name: ${self:service}-${self:provider.stage}-get-assets
    handler: handler.getAssets
    role: InventoryLambdaRole
    events:
      - http:
          path: /assets
          method: get
          cors: true
          authorizer:
            arn: arn:aws:cognito-idp:eu-west-1:HARDCODED:ARN

  # doesn't work

  getAsset:
    name: ${self:service}-${self:provider.stage}-get-asset
    handler: handler.getAsset
    role: InventoryLambdaRole
    events:
      - http:
          path: /asset/{assetId}
          method: get
          cors: true
          authorizer:
            arn: arn:aws:cognito-idp:eu-west-1:HARDCODED:ARN

[这是我在handler.js中的函数实现:


// get all assets works fine:

module.exports.getAssets = function(event, context, callback) {
  const params = {
    TableName : 'Assets',
    Select: 'ALL_ATTRIBUTES',
  }

  const request = documentClient.scan(params, function(err, data) {
    if (err) {
      console.log("Error", err)
    } else {
      const itemCount = data.Count
      const response = {
        statusCode: 200,
        headers: {
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Credentials': true,
        },
        body: JSON.stringify({
          itemCount: itemCount,
          assets: data
        }),
      }
      callback(null, response);
    }
  })
}

// get one asset doesn't work:
module.exports.getAsset = function(event, context, callback) {
  const params = {
    TableName : 'Assets',
    Key: {
      AssetId: event.pathParameters.assetId // also tried to just hardcode it like this: 'ID-1178' 
    }
  }

  const request = documentClient.get(params, function(err, data) {
    if (err) {
      console.log("Error", err)
    } else {
      const response = {
        statusCode: 200,
        headers: {
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Credentials': true,
        },
        body: JSON.stringify({
          asset: data
        }),
      }
      callback(null, response);
    }
  })

尽管这是一个CORS错误,但您可以看到提供了原始标头,但我发现与502状态结合使用时,它可能在CORS之前出现,例如功能或授权存在问题。但是,到目前为止,我看不到它们有任何问题。

无服务器功能本身在本地调用时也可以正常工作:npm run sls -- invoke local --function getAsset -p test.json

您有什么想法可能是问题或如何调试它?

aws-lambda authorization amazon-cognito serverless-framework serverless
1个回答
1
投票

您的问题可能像拥有dynamodb:GetItem一样简单。这与列出所有列表(即查询或扫描)的权限不同。

© www.soinside.com 2019 - 2024. All rights reserved.