簡體   English   中英

異步調用兩次觸發 aws lambda

[英]Asynchronous invoke triggering aws lambda twice

我有一個聊天應用程序,當我在輸入消息后單擊按鈕時,該消息會發布到我的 DynamoDB 表中。 該應用程序假設發布一次消息,但不知何故發布了兩次。

管道如下:

客戶端單擊send按鈕 --> 命中我的 Google Cloud Endpoint API --> 觸發我的 Google Cloud 函數 --> 調用我的 AWS lambda 函數 --> POSTS消息 DynamoDB。

在 SO 的幫助下,我已將問題與我的 Google Cloud 函數隔離,該函數異步調用 Lambda,該函數將 Lambda 排隊兩次。

使用異步,請求在實際執行之前排隊。 因此,如果您調用它一次,AWS 將檢查是否已經有一個正在執行,如果沒有,它將添加另一個。

在此處輸入圖片說明 AWS 博客

理想情況下,我想同步調用我的 Lambda,但根據這篇 Github帖子,我會被收取兩次費用(?)。 他提到增加我的函數的超時時間,但它已經設置為 60 秒 - 我的 lambda 有足夠的時間發回響應。 是否有其他機制將我的 lambda 排隊兩次?

供大家參考我的谷歌雲功能如下:

let AWS = require('aws-sdk');

AWS.config.update({
  accessKeyId: ''
  secretAccessKey: ''
  region: ''
})

let lambda = new AWS.Lambda();

exports.helloWorld = async (req,res) =>{
  let payload = {
    pathParameters: req.query.p,
    httpMethod: req.method,
    body: req.method == "POST" ? req.body.message || req.body.user : null,
    cognitoUsername: req.query.u
  }

  let params = {
    FunctionName: '',
    InvocationType: 'RequestResponse',
    Payload: JSON.stringify(payload)
  }

  res.status(200).send( await lambda.invoke(params, function(err,data){
    if (err){throw err}
    else {return data.Payload}
  }).promise())
}

解決方案

基於@jarmod 的解決方案,我的雲功能如下所示。 相關部分在最后。

let AWS = require('aws-sdk');

AWS.config.update({
  accessKeyId: ''
  secretAccessKey: ''
  region: ''
})

let lambda = new AWS.Lambda();

exports.helloWorld = async (req,res) =>{
  let payload = {
    pathParameters: req.query.p,
    httpMethod: req.method,
    body: req.method == "POST" ? req.body.message || req.body.user : null,
    cognitoUsername: req.query.u
  }

  let params = {
    FunctionName: '',
    InvocationType: 'RequestResponse',
    Payload: JSON.stringify(payload)
  }

  // code changed only here
  res.status(200).send( await lambda.invoke(params).promise())
}

編輯

@Ngenator 讓我注意到我的 Google Cloud 功能可能會被觸發兩次。 作為參考,這是我的 API yaml 配置:

swagger: '2.0'
info:
  title: Cloud Endpoints + GCF
  version: 1.0.0
host: service.run.app
x-google-endpoints:
  - name: "service.run.app"
    allowCors: "true"
schemes:
  - https
produces:
  - application/json
paths:
  /function-2:
    get:
      operationId: get 
      parameters:
        - name: p
          in: query
          required: true
          type: string
        - name: u
          in: query
          required: false
          type: string
      x-google-backend:
        address: https://to.my/function-2
      responses:
        '200':
          description: A successful response
          schema:
            type: string
    post:
      operationId: post 
      consumes:
        - application/json
      parameters:
        - name: p
          in: query
          required: true
          type: string
        - name: u
          in: query
          required: false
          type: string
        - in: body
          name: body
          schema:
            type: object
            properties:
              message:
                type: string
              user:
                type: array
                items:
                  type: string
      x-google-backend:
        address: https://to.my/function-2
      responses:
        '200':
          description: A successful response
          schema:
            type: string

您對lambda.invoke調用不正確。 包括一個回調並等待一個承諾。 您應該使用其中之一,最好是后者:

const rc = await lambda.invoke(params).promise();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM