-
Notifications
You must be signed in to change notification settings - Fork 723
/
aws-lambda.go
141 lines (120 loc) · 4.68 KB
/
aws-lambda.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
/*
Copyright 2020 BlackRock, Inc.
Licensed 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 aws_lambda
import (
"context"
"encoding/json"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/pkg/errors"
"go.uber.org/zap"
"github.com/argoproj/argo-events/common/logging"
commonaws "github.com/argoproj/argo-events/eventsources/common/aws"
apicommon "github.com/argoproj/argo-events/pkg/apis/common"
"github.com/argoproj/argo-events/pkg/apis/sensor/v1alpha1"
"github.com/argoproj/argo-events/sensors/policy"
"github.com/argoproj/argo-events/sensors/triggers"
)
// AWSLambdaTrigger refers to trigger that invokes AWS Lambda functions
type AWSLambdaTrigger struct {
// LambdaClient is AWS Lambda client
LambdaClient *lambda.Lambda
// Sensor object
Sensor *v1alpha1.Sensor
// Trigger definition
Trigger *v1alpha1.Trigger
// logger to log stuff
Logger *zap.SugaredLogger
}
// NewAWSLambdaTrigger returns a new AWS Lambda context
func NewAWSLambdaTrigger(lambdaClients map[string]*lambda.Lambda, sensor *v1alpha1.Sensor, trigger *v1alpha1.Trigger, logger *zap.SugaredLogger) (*AWSLambdaTrigger, error) {
lambdatrigger := trigger.Template.AWSLambda
lambdaClient, ok := lambdaClients[trigger.Template.Name]
if !ok {
awsSession, err := commonaws.CreateAWSSessionWithCredsInVolume(lambdatrigger.Region, lambdatrigger.RoleARN, lambdatrigger.AccessKey, lambdatrigger.SecretKey)
if err != nil {
return nil, errors.Wrap(err, "failed to create a AWS session")
}
lambdaClient = lambda.New(awsSession, &aws.Config{Region: &lambdatrigger.Region})
lambdaClients[trigger.Template.Name] = lambdaClient
}
return &AWSLambdaTrigger{
LambdaClient: lambdaClient,
Sensor: sensor,
Trigger: trigger,
Logger: logger.With(logging.LabelTriggerType, apicommon.LambdaTrigger),
}, nil
}
// GetTriggerType returns the type of the trigger
func (t *AWSLambdaTrigger) GetTriggerType() apicommon.TriggerType {
return apicommon.LambdaTrigger
}
// FetchResource fetches the trigger resource
func (t *AWSLambdaTrigger) FetchResource(ctx context.Context) (interface{}, error) {
return t.Trigger.Template.AWSLambda, nil
}
// ApplyResourceParameters applies parameters to the trigger resource
func (t *AWSLambdaTrigger) ApplyResourceParameters(events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) {
resourceBytes, err := json.Marshal(resource)
if err != nil {
return nil, errors.Wrap(err, "failed to marshal the aws lamda trigger resource")
}
parameters := t.Trigger.Template.AWSLambda.Parameters
if parameters != nil {
updatedResourceBytes, err := triggers.ApplyParams(resourceBytes, t.Trigger.Template.AWSLambda.Parameters, events)
if err != nil {
return nil, err
}
var ht *v1alpha1.AWSLambdaTrigger
if err := json.Unmarshal(updatedResourceBytes, &ht); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal the updated aws lambda trigger resource after applying resource parameters")
}
return ht, nil
}
return resource, nil
}
// Execute executes the trigger
func (t *AWSLambdaTrigger) Execute(ctx context.Context, events map[string]*v1alpha1.Event, resource interface{}) (interface{}, error) {
trigger, ok := resource.(*v1alpha1.AWSLambdaTrigger)
if !ok {
return nil, errors.New("failed to interpret the trigger resource")
}
if trigger.Payload == nil {
return nil, errors.New("payload parameters are not specified")
}
payload, err := triggers.ConstructPayload(events, trigger.Payload)
if err != nil {
return nil, err
}
response, err := t.LambdaClient.Invoke(&lambda.InvokeInput{
FunctionName: &trigger.FunctionName,
Payload: payload,
InvocationType: trigger.InvocationType,
})
if err != nil {
return nil, err
}
return response, nil
}
// ApplyPolicy applies the policy on the trigger execution response
func (t *AWSLambdaTrigger) ApplyPolicy(ctx context.Context, resource interface{}) error {
if t.Trigger.Policy == nil || t.Trigger.Policy.Status == nil || t.Trigger.Policy.Status.Allow == nil {
return nil
}
obj, ok := resource.(*lambda.InvokeOutput)
if !ok {
return errors.New("failed to interpret the trigger resource")
}
p := policy.NewStatusPolicy(int(*obj.StatusCode), t.Trigger.Policy.Status.GetAllow())
return p.ApplyPolicy(ctx)
}