<p>Hi gophers,</p>
<p>Has anyone had any success making golang lambda function to listen to more than one source. For example API Gateway call and Cloudwatch event?</p>
<p><a href="https://aws.amazon.com/blogs/compute/announcing-go-support-for-aws-lambda/" rel="nofollow">AWS documentation</a> says:
// Handler is your Lambda function handler
// It uses Amazon API Gateway request/responses provided by the aws-lambda-go/events package,
// However you could use other event sources (S3, Kinesis etc), or JSON-decoded primitive types such as 'string'.</p>
<p>However, I had no luck receiving data that was send from 2 different sources.</p>
<hr/>**评论:**<br/><br/>nmarley: <pre><p>A Lambda function can be mapped to many event types, and yes, I've done it with different event types (I believe it was a DynamoDb stream and a s3 notification, but it's been a few months).</p>
<p>Why this isn't working for you depends on a number of factors. Without any other information, my guess is that you don't have the <a href="https://docs.aws.amazon.com/lambda/latest/dg/invoking-lambda-functions.html" rel="nofollow">event mapping</a> or <a href="https://docs.aws.amazon.com/lambda/latest/dg/lambda-auth-and-access-control.html" rel="nofollow">access policy</a> set up properly.</p>
<p>TBH, this question is probably better suited for <a href="/r/aws" rel="nofollow">/r/aws</a> than this sub.</p></pre>grannyno: <pre><p>Would you mind sharing the definition of your handler func that can decode 2 different events?</p>
<p>In my case I need to make it work with events.APIGatewayProxyRequest and events.CloudWatchEvent. But any example will work.</p></pre>nmarley: <pre><p>I can't remember <em>exactly</em> where it is (could probably look it up), but that's not relevant honestly. It's just JSON, you need to handle <code>event</code> and <code>context</code>. It's not Go either, it's Python. The concepts are still the same.</p></pre>NeedsMoreTests: <pre><p><a href="/u/grannyno" rel="nofollow">/u/grannyno</a> <a href="https://github.com/aws/aws-lambda-go/blob/master/lambda/entry.go#L25" rel="nofollow">if you look at the docs</a> you'll notice it does not declare an explicit type and the private signature <a href="https://github.com/aws/aws-lambda-go/blob/master/lambda/handler.go#L13" rel="nofollow">is just []byte</a>. You should be able to make a function signature like this:</p>
<pre><code>func(context.Context, interface{}) error
</code></pre>
<p>You'll just have to handle the encoding/decoding yourself.</p></pre>9gPgEpW82IUTRbCzC5qr: <pre><p>actually, if your function takes an "events.S3Event" struct (<a href="https://github.com/aws/aws-lambda-go/blob/master/events/s3.go#L9" rel="nofollow">https://github.com/aws/aws-lambda-go/blob/master/events/s3.go#L9</a>), you don't have to take care of any parsing.</p></pre>
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:692541889
- 请尽量让自己的回复能够对别人有帮助
- 支持 Markdown 格式, **粗体**、~~删除线~~、
`单行代码`
- 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
- 图片支持拖拽、截图粘贴等方式上传