doson1998 2018-05-23 21:25
浏览 62
已采纳

AWS Lambda GoLang处理程序API

Is it possible to implement Lambda function handler with custom API?

For aws-lambda-go the valid handler is one of following types:

//  func ()
//  func () error
//  func (TIn) error
//  func () (TOut, error)
//  func (TIn) (TOut, error)
//  func (context.Context) error
//  func (context.Context, TIn) error
//  func (context.Context) (TOut, error)
//  func (context.Context, TIn) (TOut, error)

In my case, TIn is KinesisEvent containing MySpecificType as a payload. I'm looking for the way (maybe to overwrite lambda.Start()) to be able to create the handler of type

func(MySpecificType) error

The goal is to avoid extracting the payload from KinesisEvent in every lambda and make the handler source independent

  • 写回答

1条回答 默认 最新

  • dtiopy6088 2018-05-24 06:56
    关注

    Seems like the solution is quite simple. Just overwrite the lambda.Start() method:

    func Start(handler func(t * MySpecificType, i int) error) {
    
        lambda.Start(func(kinesisEvent events.KinesisEvent) (err error) {
    
            for i, record := range kinesisEvent.Records {
    
                mySpecType := new(MySpecificType)
                if err = json.Unmarshal(record.Kinesis.Data, mySpecType); err != nil {
                    logrus.Errorf("records[%d]: %s", i, err)
                    continue
                }
    
                err = handler(mySpecType, i)
            }
            return
        })
    }
    

    and then:

    // main.go
    func Handler(m *MySpecificType, index int) error {
    // ...
    }
    
    func main() {
        <wherever_you_put_it_package>.Start(Handler)
        // instead of lambda.Start(Handler)
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 pg数据库导入数据序列重复
  • ¥15 三分类机器学习模型可视化分析
  • ¥15 本地测试网站127.0.0.1 已拒绝连接,如何解决?(标签-ubuntu)
  • ¥50 Qt在release捕获异常并跟踪堆栈(有Demo,跑一下环境再回答)
  • ¥30 python,LLM 文本提炼
  • ¥15 关于将inet引入的相关问题
  • ¥15 关于一个倒计时的操作和显示设计
  • ¥15 提问STK的问题,哪位航天领域的同学会啊
  • ¥15 苹果系统的mac m1芯片的笔记本使用ce修改器使用不了
  • ¥15 单相逆变的电压电流双闭环中进行低通滤波PID算法改进