我有一个聊天应用程序,当我在输入消息后单击一个按钮时,消息会被发布到我的DynamoDB表中。这个应用程序应该只发布一次消息,但是不知怎么的,它会发布两次。
管道如下:
客户机单击send按钮->点击我的Google >触发我的Google函数->调用我的AWS函数-> POSTS消息DynamoDB。
在这样的帮助下,我将问题隔离到我的Google函数,异步调用Lambda,这是对Lambda进行两次排队。
使用异步时,请求在实际执行之前先排队。因此,如果您调用它一次,AWS将检查是否已经有一个正在执行,如果没有,它将添加另一个。

(AWS博客)
理想情况下,我希望同步调用我的Lambda,但是根据这个Github 帖子,我会收到两次账单(?)。他提到增加我函数的超时,但它已经被设置为60秒--这是我的lambda发送回复的充足时间。还有其他机制可以让我的兰达排队两次吗?
供您参考,我的Google云功能如下:
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功能可能会被触发两次。作为参考,这是我的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发布于 2020-01-07 00:33:15
您对lambda.invoke的调用是不正确的。它既包括回调,也包括等待承诺。你应该使用其中一种或另一种,最好是后者:
const rc = await lambda.invoke(params).promise();https://stackoverflow.com/questions/59620270
复制相似问题