Golang + AWS lambda listening from more than 1 source.

blov · · 141 次点击    
这是一个分享于 的资源,其中的信息可能已经有所发展或是发生改变。
<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 &#39;string&#39;.</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&#39;ve done it with different event types (I believe it was a DynamoDb stream and a s3 notification, but it&#39;s been a few months).</p> <p>Why this isn&#39;t working for you depends on a number of factors. Without any other information, my guess is that you don&#39;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&#39;t remember <em>exactly</em> where it is (could probably look it up), but that&#39;s not relevant honestly. It&#39;s just JSON, you need to handle <code>event</code> and <code>context</code>. It&#39;s not Go either, it&#39;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&#39;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&#39;ll just have to handle the encoding/decoding yourself.</p></pre>9gPgEpW82IUTRbCzC5qr: <pre><p>actually, if your function takes an &#34;events.S3Event&#34; 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&#39;t have to take care of any parsing.</p></pre>

入群交流(和以上内容无关):加入Go大咖交流群,或添加微信:liuxiaoyan-s 备注:入群;或加QQ群:701969077

141 次点击  
加入收藏 微博
添加一条新回复 (您需要 登录 后才能回复 没有账号 ?)
  • 请尽量让自己的回复能够对别人有帮助
  • 支持 Markdown 格式, **粗体**、~~删除线~~、`单行代码`
  • 支持 @ 本站用户;支持表情(输入 : 提示),见 Emoji cheat sheet
  • 图片支持拖拽、截图粘贴等方式上传