-
Notifications
You must be signed in to change notification settings - Fork 1
/
awsevents.go
194 lines (159 loc) · 4.85 KB
/
awsevents.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Licensed to NASA JPL under one or more contributor
// license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright
// ownership. NASA JPL licenses this file to you under
// the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
package awsutil
// This code is largely inspired by: https://www.synvert-tcm.com/blog/handling-multiple-aws-lambda-event-types-with-go/
import (
"encoding/json"
"github.com/aws/aws-lambda-go/events"
"github.com/aws/aws-sdk-go/aws/arn"
"github.com/pkg/errors"
)
type eventType int
const (
unknownEventType eventType = iota
s3EventType
snsEventType
sqsEventType
)
type Record struct {
EventSource string
EventSourceArn string
AWSRegion string
S3 events.S3Entity
SQS events.SQSMessage
SNS events.SNSEntity
}
type Event struct {
Records []Record
}
// getEventType - Get the event type from the stream
func (event *Event) getEventType(data []byte) eventType {
temp := make(map[string]interface{})
json.Unmarshal(data, &temp)
recordsList, _ := temp["Records"].([]interface{})
if len(recordsList) <= 0 {
return unknownEventType
}
record, _ := recordsList[0].(map[string]interface{})
var eventSource string
if es, ok := record["EventSource"]; ok {
eventSource = es.(string)
} else if es, ok := record["eventSource"]; ok {
eventSource = es.(string)
}
switch eventSource {
case "aws:s3":
return s3EventType
case "aws:sns":
return snsEventType
case "aws:sqs":
return sqsEventType
}
return unknownEventType
}
// mapS3EventRecords - Create an S3 record on receipt of an S3 Event
func (event *Event) mapS3EventRecords(s3Event *events.S3Event) error {
event.Records = make([]Record, 0)
for _, s3Record := range s3Event.Records {
event.Records = append(event.Records, Record{
EventSource: s3Record.EventSource,
EventSourceArn: s3Record.S3.Bucket.Arn,
AWSRegion: s3Record.AWSRegion,
S3: s3Record.S3,
})
}
return nil
}
// mapSNSEventRecords - Create an SNS record on receipt of an SNS Event
func (event *Event) mapSNSEventRecords(snsEvent *events.SNSEvent) error {
event.Records = make([]Record, 0)
for _, snsRecord := range snsEvent.Records {
// decode sns message to s3 event
//s3Event := &events.S3Event{}
//err := json.Unmarshal([]byte(snsRecord.SNS.Message), s3Event)
//if err != nil {
// return errors.Wrap(err, "Failed to decode sns message to an S3 event")
//}
/*if len(s3Event.Records) == 0 {
return errors.New("S3 Event Records is empty")
}*/
//for _, s3Record := range s3Event.Records {
topicArn, err := arn.Parse(snsRecord.SNS.TopicArn)
if err != nil {
return err
}
event.Records = append(event.Records, Record{
EventSource: snsRecord.EventSource,
EventSourceArn: snsRecord.SNS.TopicArn,
AWSRegion: topicArn.Region,
SNS: snsRecord.SNS,
})
//}
}
return nil
}
// mapSQSEventRecords - Decode the SQS Event
func (event *Event) mapSQSEventRecords(sqsEvent *events.SQSEvent) error {
event.Records = make([]Record, 0)
for _, sqsRecord := range sqsEvent.Records {
// decode sqs body to s3 event
s3Event := &events.S3Event{}
err := json.Unmarshal([]byte(sqsRecord.Body), s3Event)
if err != nil {
return errors.Wrap(err, "Failed to decode sqs body to an S3 event")
}
if len(s3Event.Records) == 0 {
return errors.New("S3 Event Records is empty")
}
for _, s3Record := range s3Event.Records {
event.Records = append(event.Records, Record{
EventSource: sqsRecord.EventSource,
EventSourceArn: sqsRecord.EventSourceARN,
AWSRegion: sqsRecord.AWSRegion,
SQS: sqsRecord,
S3: s3Record.S3,
})
}
}
return nil
}
// UnmarshalJSON - Decode the JSON to the correct Event type
func (event *Event) UnmarshalJSON(data []byte) error {
//eType := event.getEventType(data)
var err error
switch event.getEventType(data) {
case s3EventType:
s3Event := &events.S3Event{}
err = json.Unmarshal(data, s3Event)
if err == nil {
return event.mapS3EventRecords(s3Event)
}
case snsEventType:
snsEvent := &events.SNSEvent{}
err = json.Unmarshal(data, snsEvent)
if err == nil {
return event.mapSNSEventRecords(snsEvent)
}
case sqsEventType:
sqsEvent := &events.SQSEvent{}
err = json.Unmarshal(data, sqsEvent)
if err == nil {
return event.mapSQSEventRecords(sqsEvent)
}
}
return err
}