【问题标题】:How to pass parameters to scheduled .Net lambda functions using serverless framework?如何使用无服务器框架将参数传递给预定的 .Net lambda 函数?
【发布时间】:2021-07-26 17:50:34
【问题描述】:

我正在使用无服务器框架来部署计划的 AWS Lambda 函数,并使用 Cloudwatch 事件来触发该函数。该函数的调度按预期工作,但我也希望能够通过事件传递参数。无服务器配置类似于以下...

functions:
  myFunction:
    handler: index.handler
    events:
      - eventBridge:
          schedule: rate(10 minutes)
          input:
            key1: value1

我的问题是如何在函数处理程序中接收事件参数。该函数用C#实现,使用AWS ScheduledEvent类在函数处理程序中接收事件,如下...

public void FunctionHandler(ScheduledEvent scheduledEvent, ILambdaContext context)
{
}

但是scheduledEvent在任何地方都没有包含“key1”参数,那么它去了哪里,应该被函数处理程序接收吗? 任何帮助表示赞赏!

【问题讨论】:

  • 你能找到任何解决方案吗?

标签: .net lambda serverless-framework


【解决方案1】:

我想出了一个使用“schedule”而不是“eventBridge”事件的解决方案。

events:
  - schedule: 
      rate: cron(1 12 ? * MON *) # every monday @ 12:01
      enabled: true
      input:
        SomeParameter: SomeValue

然后定义一个类来接收参数...

public class EventParameters
{
    public string SomeParameter { get; set; }
}

然后handler函数接收参数类...

public async Task FunctionHandler(EventParameters eventParameters, ILambdaContext context)
{
}

【讨论】:

    【解决方案2】:

    我在使用event -> schedule 时遇到了同样的问题。输入属性包含在传递给处理函数的事件参数中:

    functions:
      hello:
        handler: handler.hello
        events:
          - schedule:
              rate: cron(* * * * * *)    // for every minute
              input:
                email: "true" 
    

    在这里,您可以通过事件对象简单地访问您的输入参数电子邮件,如下所示:

    module.exports.hello = async (event) {
      const { email } = event
      ... do some other stuff ...
    }
    

    【讨论】:

      猜你喜欢
      • 2019-06-18
      • 2021-12-20
      • 2021-09-16
      • 2021-10-07
      • 1970-01-01
      • 2016-04-10
      • 1970-01-01
      • 2017-12-25
      • 2021-08-01
      相关资源
      最近更新 更多