为什么我通过 api 网关收到 502 到节点 js lambda 的申请?

时间:2021-08-02 00:02:12

标签: node.js amazon-web-services aws-lambda aws-api-gateway

我有一个来自 aws chime sdk 的演示代码,该代码位于 lambda 函数上,并由 /join 的后请求调用。如果不受影响,它可以完美运行,但是当我添加几行代码时,api 网关的响应是 502“格式错误的 Lambda 代理响应”。即使 var finalResult 为真,换句话说,我不需要更改将返回的响应,它返回格式错误的响应。

但是如果我注释掉我的代码,一切都会恢复正常。如果我使用相同的代码,响应如何格式错误?我只是在返回响应之前添加一个任务。这里发生了什么?

更新 - 我的全部代码:

exports.join = async(event, context) => {
  const query = event.queryStringParameters;

  if (!query.title || !query.name || !query.region) {
    return response(400, 'application/json', JSON.stringify({error: 'Need parameters: title, name, region'}));
  }

  
  let finalResult=false;
  let dataString = '';
  var re = /PHPSESSID=/gi;
  var str = event.headers.Cookie;
  var newstr = str.replace(re,'');

  
    const jsonResp = await new Promise((resolve,reject) => {
      const req = https.get("url/"+query.title+'@'+newstr, 
      function(res) {
        res.on('data', chunk => {
          dataString += chunk;
        });
        res.on('end', async () => {
          console.log('Passing here dataString => '+JSON.parse(dataString).status);
          if(dataString!='' && JSON.parse(dataString).status===1){
            
            console.log('Starting to create a meeting ');
            let meeting = await getMeeting(query.title);
            if (!meeting) {
              const request = {
                ClientRequestToken: uuidv4(),
                MediaRegion: query.region,
                ExternalMeetingId: query.title.substring(0, 64),
                Tags: [{ Key: 'Department', Value: 'RND'}]
              };
              console.info('Creating new meeting: ' + JSON.stringify(request));
              meeting = await chime.createMeeting(request).promise();

              // Store the meeting in the table using the meeting title as the key.
              await putMeeting(query.title, meeting);
            }

              console.info('Creating new meeting: ' + JSON.stringify(meeting));

            // Create new attendee for the meeting
            console.info('Adding new attendee');
            const attendee = await chime.createAttendee({
              // The meeting ID of the created meeting to add the attendee to
              MeetingId: meeting.Meeting.MeetingId,
              ExternalUserId: `${uuidv4().substring(0, 8)}#${query.name}`.substring(0, 64),
            }).promise();

            console.log("Creating Attendee => "+JSON.stringify(attendee));


resolve({"statusCode":200,"contentType":"application/json","body": JSON.stringify({JoinInfo: { Meeting: meeting, Attendee: attendee }}) });

          }
          else{
            reject({"statusCode":400, "contentType": "application/json","body": JSON.stringify({error: "access denied"})});
          }
        });
      });
      
      req.on('error', (e) => {
        console.log("request error => "+e.message);
        reject({"statusCode":400, "contentType": "application/json",  "body": JSON.stringify({error: "request error"})})
      });
    }).catch(function(erro){ console.log(erro.message); });
    
   
  return jsonResp;
};

API 网关响应:

...

Mon Aug 02 03:48:03 UTC 2021 : Endpoint response headers: {Date=Mon, 02 Aug 2021 03:48:03 GMT, Content-Type=application/json, Content-Length=1249, Connection=keep-alive, x-amzn-RequestId=eb74a980-5855-45f0-ac8a-225b0491f844, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-61076aee-e4081186e3e6583b876db7ca;sampled=0}
Mon Aug 02 03:48:03 UTC 2021 : Endpoint response body before transformations: {"statusCode":200,"contentType":"application/json","body":"{\"JoinInfo\":{\"Meeting\":{\"Meeting\":{\"MeetingId\":\"d7da9893-470e-4cba-839c-9c6b4e230706\",\"ExternalMeetingId\":\"441cbe97-f344-11eb-957b-02adc802a03d\",\"MediaPlacement\":{\"AudioHostUrl\":\"2c9ff0da8a3961a1f10fc982bede14cf.k.m3.uw2.app.chime.aws:3478\",\"AudioFallbackUrl\":\"wss://haxrp.m3.uw2.app.chime.aws:443/calls/d7da9893-470e-4cba-839c-9c6b4e230706\",\"ScreenDataUrl\":\"wss://bitpw.m3.uw2.app.chime.aws:443/v2/screen/d7da9893-470e-4cba-839c-9c6b4e230706\",\"ScreenSharingUrl\":\"wss://bitpw.m3.uw2.app.chime.aws:443/v2/screen/d7da9893-470e-4cba-839c-9c6b4e230706\",\"ScreenViewingUrl\":\"wss://bitpw.m3.uw2.app.chime.aws:443/ws/connect?passcode=null&viewer_uuid=null&X-BitHub-Call-Id=d7da9893-470e-4cba-839c-9c6b4e230706\",\"SignalingUrl\":\"wss://signal.m3.uw2.app.chime.aws/control/d7da9893-470e-4cba-839c-9c6b4e230706\",\"TurnControlUrl\":\"https://ccp.cp.ue1.app.chime.aws/v2/turn_sessions\"},\"Med [TRUNCATED]
Mon Aug 02 03:48:03 UTC 2021 : Execution failed due to configuration error: Malformed Lambda proxy response
Mon Aug 02 03:48:03 UTC 2021 : Method completed with status: 502

CloudWatch 日志:

START RequestId: eb74a980-5855-45f0-ac8a-225b0491f844 Version: $LATEST
2021-08-02T03:48:01.602Z    eb74a980-5855-45f0-ac8a-225b0491f844    INFO    Entrou no end chunk dataString => 1
2021-08-02T03:48:01.603Z    eb74a980-5855-45f0-ac8a-225b0491f844    INFO    Ja ta criando a meeting 
2021-08-02T03:48:03.033Z    eb74a980-5855-45f0-ac8a-225b0491f844    INFO    Creating new meeting: 
{
    "Meeting": {
        "MeetingId": "d7da9893-470e-4cba-839c-9c6b4e230706",
        "ExternalMeetingId": "441cbe97-f344-11eb-957b-02adc802a03d",
        "MediaPlacement": {
            "AudioHostUrl": "2c9ff0da8a3961a1f10fc982bede14cf.k.m3.uw2.app.chime.aws:3478",
            "AudioFallbackUrl": "wss://haxrp.m3.uw2.app.chime.aws:443/calls/d7da9893-470e-4cba-839c-9c6b4e230706",
            "ScreenDataUrl": "wss://bitpw.m3.uw2.app.chime.aws:443/v2/screen/d7da9893-470e-4cba-839c-9c6b4e230706",
            "ScreenSharingUrl": "wss://bitpw.m3.uw2.app.chime.aws:443/v2/screen/d7da9893-470e-4cba-839c-9c6b4e230706",
            "ScreenViewingUrl": "wss://bitpw.m3.uw2.app.chime.aws:443/ws/connect?passcode=null&viewer_uuid=null&X-BitHub-Call-Id=d7da9893-470e-4cba-839c-9c6b4e230706",
            "SignalingUrl": "wss://signal.m3.uw2.app.chime.aws/control/d7da9893-470e-4cba-839c-9c6b4e230706",
            "TurnControlUrl": "https://ccp.cp.ue1.app.chime.aws/v2/turn_sessions"
        },
        "MediaRegion": "us-west-2"
    }
}

2021-08-02T03:48:03.033Z    eb74a980-5855-45f0-ac8a-225b0491f844    INFO    Adding new attendee
2021-08-02T03:48:03.361Z    eb74a980-5855-45f0-ac8a-225b0491f844    INFO    Createing Attendee => 
{
    "Attendee": {
        "ExternalUserId": "ab953cd5#Username",
        "AttendeeId": "63146cc4-fa1d-e3d5-e0c9-f1efa40cd64e",
        "JoinToken": "NjMxNDZjYzQtZmExZC1lM2Q1LWUwYzktZjFlZmE0MGNkNjRlOmYyZTg3M2QxLTliOWQtNDNkYi05NTBjLWU1YmFmOGFjNTIzZg"
    }
}

END RequestId: eb74a980-5855-45f0-ac8a-225b0491f844
REPORT RequestId: eb74a980-5855-45f0-ac8a-225b0491f844  Duration: 4496.03 ms    Billed Duration: 4497 ms    Memory Size: 128 MB Max Memory Used: 91 MB  Init Duration: 610.10 ms    

0 个答案:

没有答案