sam*_*cos 14 lambda amazon-web-services amazon-cloudwatch aws-lambda serverless-framework
以从CloudWatch计时器处理ping的方式包装函数的最佳方法是什么?例如,采用下面的lambda函数:
export const fn = (event, context, callback) => {
const { year, make, model, } = event.queryStringParameters
return otherFn({ year, make, model, })
.then(res => response(callback, res))
.catch(err => console.log(err))
}
Run Code Online (Sandbox Code Playgroud)
如果我ping函数,它将会出错,因为CloudWatch请求中没有queryStringParameters.从技术上讲,这仍然可以保持Lambda函数的温暖(这是我的目标),但我不想有一个不必要的错误列表.
我注意到CloudWatch允许您包含(可能)传递给Lambda函数的输入:
包装上面的函数以便它可以接受ping的最聪明的方法是什么?理想情况下它看起来像这样:
export const fn = (event, context, callback) => {
if (event.ping) return ping(callback) // the ping function is an import to stay DRY
const { year, make, model, } = event.queryStringParameters
return otherFn({ year, make, model, })
.then(res => response(callback, res))
.catch(err => console.log(err))
}
Run Code Online (Sandbox Code Playgroud)
我会传递一些允许我改变事件的JSON,例如:
{ "ping": true }
Run Code Online (Sandbox Code Playgroud)
我已经阅读了文档中的输入,但我并不清楚各种输入类型的含义或如何使用它们......
use*_*510 20
如果选择输入选项Constant(JSON文本),则输入框应显示在下方.你需要输入json {"ping": true}
.您的函数将获取json作为event
对象,您可以event.ping
像访问代码一样访问它.
如果您使用的是无服务器框架,而不是在AWS控制台中执行此操作,则可以为您的函数添加计划事件.这将是您现有的http事件的补充.您可以ping: true
在预定事件的输入部分下添加参数,如下所示:
scheduledFunction:
handler: index.handler
events:
- schedule:
rate: rate(1 minute)
enabled: true
input:
ping: true
Run Code Online (Sandbox Code Playgroud)
这将创建并启用具有指定计划的cloudwatch日志事件,并ping
在event
对象中发送输入参数.
归档时间: |
|
查看次数: |
8291 次 |
最近记录: |