-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathcmd_execute_awsbinary.go
277 lines (239 loc) · 8.99 KB
/
cmd_execute_awsbinary.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
//go:build lambdabinary
// +build lambdabinary
package sparta
import (
"context"
"encoding/json"
"fmt"
"os"
"reflect"
"sync"
awsLambdaGo "github.com/aws/aws-lambda-go/lambda"
awsLambdaContext "github.com/aws/aws-lambda-go/lambdacontext"
spartaAWS "github.com/mweagle/Sparta/aws"
cwCustomProvider "github.com/mweagle/Sparta/aws/cloudformation/provider"
cloudformationResources "github.com/mweagle/Sparta/aws/cloudformation/resources"
"github.com/rs/zerolog"
)
// StampedServiceName is the name stamp
// https://blog.cloudflare.com/setting-go-variables-at-compile-time/
// StampedServiceName is the serviceName stamped into this binary
var StampedServiceName string
// StampedBuildID is the buildID stamped into the binary
var StampedBuildID string
var discoveryInfo *DiscoveryInfo
var once sync.Once
func initDiscoveryInfo() {
info, _ := Discover()
discoveryInfo = info
}
func awsLambdaFunctionName(internalFunctionName string) string {
// TODO - move this to use SSM so that it's not human editable?
// But discover information is per-function, not per stack.
// Could we put the stack discovery info in there?
once.Do(initDiscoveryInfo)
sanitizedName := awsLambdaInternalName(internalFunctionName)
return fmt.Sprintf("%s%s%s",
discoveryInfo.StackName,
functionNameDelimiter,
sanitizedName)
}
func takesContext(handler reflect.Type) bool {
handlerTakesContext := false
if handler.NumIn() > 0 {
contextType := reflect.TypeOf((*context.Context)(nil)).Elem()
argumentType := handler.In(0)
handlerTakesContext = argumentType.Implements(contextType)
}
return handlerTakesContext
}
// tappedHandler is the handler that represents this binary's mode
func tappedHandler(handlerSymbol interface{},
interceptors *LambdaEventInterceptors,
logger *zerolog.Logger) interface{} {
// If there aren't any, make it a bit easier
// to call the applyInterceptors function
if interceptors == nil {
interceptors = &LambdaEventInterceptors{}
}
// Tap the call chain to inject the context params...
handler := reflect.ValueOf(handlerSymbol)
handlerType := reflect.TypeOf(handlerSymbol)
takesContext := takesContext(handlerType)
// Apply interceptors is a utility function to apply the
// specified interceptors as part of the lifecycle handler.
// We can push the specific behaviors into the interceptors
// and keep this function simple. 🎉
applyInterceptors := func(ctx context.Context,
msg json.RawMessage,
interceptors InterceptorList) context.Context {
for _, eachInterceptor := range interceptors {
ctx = eachInterceptor.Interceptor(ctx, msg)
}
return ctx
}
// How to determine if this handler has tracing enabled? That would be a property
// of the function template associated with this function.
// TODO - add Context.Timeout handler to ensure orderly exit
return func(ctx context.Context, msg json.RawMessage) (interface{}, error) {
awsConfig, awsConfigErr := spartaAWS.NewConfig(ctx, logger)
if awsConfigErr != nil {
return nil, awsConfigErr
}
ctx = applyInterceptors(ctx, msg, interceptors.Begin)
ctx = context.WithValue(ctx, ContextKeyLogger, logger)
ctx = context.WithValue(ctx, ContextKeyAWSConfig, awsConfig)
ctx = applyInterceptors(ctx, msg, interceptors.BeforeSetup)
// Create the entry logger that has some context information
var zerologRequestLogger zerolog.Logger
lambdaContext, lambdaContextOk := awsLambdaContext.FromContext(ctx)
if lambdaContextOk {
zerologRequestLogger = logger.With().
Str(LogFieldRequestID, lambdaContext.AwsRequestID).
Str(LogFieldARN, lambdaContext.InvokedFunctionArn).
Str(LogFieldBuildID, StampedBuildID).
Str(LogFieldInstanceID, InstanceID()).
Logger()
}
ctx = context.WithValue(ctx, ContextKeyRequestLogger, &zerologRequestLogger)
ctx = applyInterceptors(ctx, msg, interceptors.AfterSetup)
// construct arguments
var args []reflect.Value
if takesContext {
args = append(args, reflect.ValueOf(ctx))
}
if (handlerType.NumIn() == 1 && !takesContext) ||
handlerType.NumIn() == 2 {
eventType := handlerType.In(handlerType.NumIn() - 1)
event := reflect.New(eventType)
unmarshalErr := json.Unmarshal(msg, event.Interface())
if unmarshalErr != nil {
return nil, unmarshalErr
}
args = append(args, event.Elem())
}
ctx = applyInterceptors(ctx, msg, interceptors.BeforeDispatch)
response := handler.Call(args)
ctx = applyInterceptors(ctx, msg, interceptors.AfterDispatch)
// If the user function
// convert return values into (interface{}, error)
var err error
if len(response) > 0 {
if errVal, ok := response[len(response)-1].Interface().(error); ok {
err = errVal
}
}
ctx = context.WithValue(ctx, ContextKeyLambdaError, err)
var val interface{}
if len(response) > 1 {
val = response[0].Interface()
}
ctx = context.WithValue(ctx, ContextKeyLambdaResponse, val)
applyInterceptors(ctx, msg, interceptors.Complete)
return val, err
}
}
// Execute creates an HTTP listener to dispatch execution. Typically
// called via Main() via command line arguments.
func Execute(serviceName string,
lambdaAWSInfos []*LambdaAWSInfo,
logger *zerolog.Logger) error {
logger.Debug().Msg("Initializing discovery service")
// Initialize the discovery service
initializeDiscovery(logger)
// Find the function name based on the dispatch
// https://docs.aws.amazon.com/lambda/latest/dg/current-supported-versions.html
requestedLambdaFunctionName := os.Getenv("AWS_LAMBDA_FUNCTION_NAME")
logger.Debug().
Str("lambdaName", requestedLambdaFunctionName).
Msg("Invoking requested lambda")
// Log any info when we start up...
logger.Debug().
Msg("Querying for platform info")
platformLogSysInfo(requestedLambdaFunctionName, logger)
// So what if we have workflow hooks in here?
var interceptors *LambdaEventInterceptors
/*
There are three types of targets:
- User functions
- User custom resources
- Sparta custom resources
*/
// Based on the environment variable, setup the proper listener...
var lambdaFunctionName string
testAWSName := ""
var handlerSymbol interface{}
knownNames := []string{}
//////////////////////////////////////////////////////////////////////////////
// User registered commands?
//////////////////////////////////////////////////////////////////////////////
logger.Debug().Msg("Checking user-defined lambda functions")
for _, eachLambdaInfo := range lambdaAWSInfos {
lambdaFunctionName = awsLambdaFunctionName(eachLambdaInfo.lambdaFunctionName())
testAWSName = lambdaFunctionName
knownNames = append(knownNames, testAWSName)
if requestedLambdaFunctionName == testAWSName {
handlerSymbol = eachLambdaInfo.handlerSymbol
interceptors = eachLambdaInfo.Interceptors
}
// User defined custom resource handler?
for _, eachCustomResource := range eachLambdaInfo.customResources {
lambdaFunctionName = awsLambdaFunctionName(eachCustomResource.userFunctionName)
testAWSName = lambdaFunctionName
knownNames = append(knownNames, testAWSName)
if requestedLambdaFunctionName == testAWSName {
handlerSymbol = eachCustomResource.handlerSymbol
}
}
if handlerSymbol != nil {
break
}
}
//////////////////////////////////////////////////////////////////////////////
// Request to instantiate a CustomResourceHandler that implements
// the CustomResourceCommand interface?
//////////////////////////////////////////////////////////////////////////////
if handlerSymbol == nil {
logger.Debug().Msg("Checking CustomResourceHandler lambda functions")
requestCustomResourceType := os.Getenv(EnvVarCustomResourceTypeName)
if requestCustomResourceType != "" {
knownNames = append(knownNames, fmt.Sprintf("CloudFormation Custom Resource: %s", requestCustomResourceType))
logger.Debug().
Interface("customResourceTypeName", requestCustomResourceType).
Msg("Checking to see if there is a custom resource")
resource, resourceErr := cwCustomProvider.NewCloudFormationCustomResource(requestCustomResourceType, logger)
if resourceErr != nil {
return resourceErr
}
if resource != nil {
// Handler?
command, commandOk := resource.(cloudformationResources.CustomResourceCommand)
if !commandOk {
logger.Error().
Str("ResourceType", requestCustomResourceType).
Msg("CloudFormation type doesn't implement cloudformationResources.CustomResourceCommand")
} else {
customHandler := cloudformationResources.CloudFormationLambdaCustomResourceHandler(command, logger)
if customHandler != nil {
handlerSymbol = customHandler
}
}
} else {
logger.Error().
Str("ResourceType", requestCustomResourceType).
Msg("Failed to create CloudFormation custom resource of type")
}
}
}
if handlerSymbol == nil {
errorMessage := fmt.Errorf("No handler found for AWS Lambda function: %s. Registered function name: %#v",
requestedLambdaFunctionName,
knownNames)
logger.Error().Err(errorMessage).Msg("Handler error")
return errorMessage
}
// Startup our version...
tappedHandler := tappedHandler(handlerSymbol, interceptors, logger)
awsLambdaGo.Start(tappedHandler)
return nil
}