forked from imjerrybao/apex
/
function.go
568 lines (462 loc) · 12.8 KB
/
function.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
// Package function implements function-level operations.
package function
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/apex/log"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/aws/aws-sdk-go/service/lambda/lambdaiface"
"github.com/dustin/go-humanize"
"github.com/jpillora/archive"
"gopkg.in/validator.v2"
"github.com/apex/apex/hooks"
"github.com/apex/apex/utils"
)
// defaultPlugins are the default plugins which are required by Apex. Note that
// the order here is important for some plugins such as inference before the
// runtimes.
var defaultPlugins = []string{
"inference",
"golang",
"python",
"nodejs",
"hooks",
"env",
"shim",
}
// InvocationType determines how an invocation request is made.
type InvocationType string
// Invocation types.
const (
RequestResponse InvocationType = "RequestResponse"
Event = "Event"
DryRun = "DryRun"
)
// CurrentAlias name.
const CurrentAlias = "current"
// InvokeError records an error from an invocation.
type InvokeError struct {
Message string `json:"errorMessage"`
Type string `json:"errorType"`
Stack []string `json:"stackTrace"`
Handled bool
}
// Error message.
func (e *InvokeError) Error() string {
return e.Message
}
// Config for a Lambda function.
type Config struct {
Description string `json:"description"`
Runtime string `json:"runtime" validate:"nonzero"`
Memory int64 `json:"memory" validate:"nonzero"`
Timeout int64 `json:"timeout" validate:"nonzero"`
Role string `json:"role" validate:"nonzero"`
Handler string `json:"handler" validate:"nonzero"`
Shim bool `json:"shim"`
Environment map[string]string `json:"environment"`
Hooks hooks.Hooks `json:"hooks"`
}
// Function represents a Lambda function, with configuration loaded
// from the "function.json" file on disk.
type Function struct {
Config
Name string
FunctionName string
Path string
Service lambdaiface.LambdaAPI
Log log.Interface
IgnoredPatterns []string
Plugins []string
}
// Open the function.json file and prime the config.
func (f *Function) Open() error {
f.Log = f.Log.WithField("function", f.Name)
if f.Plugins == nil {
f.Plugins = defaultPlugins
}
if f.Environment == nil {
f.Environment = make(map[string]string)
}
p, err := os.Open(filepath.Join(f.Path, "function.json"))
if err == nil {
if err := json.NewDecoder(p).Decode(&f.Config); err != nil {
return err
}
}
if err := f.hookOpen(); err != nil {
return err
}
if err := validator.Validate(&f.Config); err != nil {
return fmt.Errorf("error opening function %s: %s", f.Name, err.Error())
}
patterns, err := utils.ReadIgnoreFile(f.Path)
if err != nil {
return err
}
f.IgnoredPatterns = append(f.IgnoredPatterns, patterns...)
return nil
}
// Setenv sets environment variable `name` to `value`.
func (f *Function) Setenv(name, value string) {
f.Environment[name] = value
}
// Deploy generates a zip and creates or deploy the function.
// If the configuration hasn't been changed it will deploy only code,
// otherwise it will deploy both configuration and code.
func (f *Function) Deploy() error {
f.Log.Info("deploying")
zip, err := f.BuildBytes()
if err != nil {
return err
}
if err := f.hookDeploy(); err != nil {
return err
}
config, err := f.GetConfig()
if e, ok := err.(awserr.Error); ok {
if e.Code() == "ResourceNotFoundException" {
return f.Create(zip)
}
}
if err != nil {
return err
}
if f.configChanged(config) {
f.Log.Info("config changed")
return f.DeployConfigAndCode(zip)
}
f.Log.Info("config unchanged")
return f.DeployCode(zip, config)
}
// DeployCode deploys function code when changed.
func (f *Function) DeployCode(zip []byte, config *lambda.GetFunctionOutput) error {
remoteHash := *config.Configuration.CodeSha256
localHash := utils.Sha256(zip)
if localHash == remoteHash {
f.Log.Info("code unchanged")
return nil
}
f.Log.WithFields(log.Fields{
"local": localHash,
"remote": remoteHash,
}).Debug("code changed")
return f.Update(zip)
}
// DeployConfigAndCode updates config and updates function code.
func (f *Function) DeployConfigAndCode(zip []byte) error {
f.Log.Info("updating config")
_, err := f.Service.UpdateFunctionConfiguration(&lambda.UpdateFunctionConfigurationInput{
FunctionName: &f.FunctionName,
MemorySize: &f.Memory,
Timeout: &f.Timeout,
Description: &f.Description,
Role: &f.Role,
Handler: &f.Handler,
})
if err != nil {
return err
}
return f.Update(zip)
}
// Delete the function including all its versions
func (f *Function) Delete() error {
f.Log.Info("deleting")
_, err := f.Service.DeleteFunction(&lambda.DeleteFunctionInput{
FunctionName: &f.FunctionName,
})
return err
}
// GetConfig returns the function configuration.
func (f *Function) GetConfig() (*lambda.GetFunctionOutput, error) {
f.Log.Debug("fetching config")
return f.Service.GetFunction(&lambda.GetFunctionInput{
FunctionName: &f.FunctionName,
})
}
// GetConfigQualifier returns the function configuration for the given qualifier.
func (f *Function) GetConfigQualifier(s string) (*lambda.GetFunctionOutput, error) {
f.Log.Debug("fetching config")
return f.Service.GetFunction(&lambda.GetFunctionInput{
FunctionName: &f.FunctionName,
Qualifier: &s,
})
}
// GetConfigCurrent returns the function configuration for the current version.
func (f *Function) GetConfigCurrent() (*lambda.GetFunctionOutput, error) {
return f.GetConfigQualifier(CurrentAlias)
}
// Update the function with the given `zip`.
func (f *Function) Update(zip []byte) error {
f.Log.Info("updating function")
updated, err := f.Service.UpdateFunctionCode(&lambda.UpdateFunctionCodeInput{
FunctionName: &f.FunctionName,
Publish: aws.Bool(true),
ZipFile: zip,
})
if err != nil {
return err
}
f.Log.Info("updating alias")
_, err = f.Service.UpdateAlias(&lambda.UpdateAliasInput{
FunctionName: &f.FunctionName,
Name: aws.String(CurrentAlias),
FunctionVersion: updated.Version,
})
if err != nil {
return nil
}
f.Log.WithFields(log.Fields{
"version": *updated.Version,
"name": f.FunctionName,
}).Info("function updated")
return nil
}
// Create the function with the given `zip`.
func (f *Function) Create(zip []byte) error {
f.Log.Info("creating function")
created, err := f.Service.CreateFunction(&lambda.CreateFunctionInput{
FunctionName: &f.FunctionName,
Description: &f.Description,
MemorySize: &f.Memory,
Timeout: &f.Timeout,
Runtime: &f.Runtime,
Handler: &f.Handler,
Role: &f.Role,
Publish: aws.Bool(true),
Code: &lambda.FunctionCode{
ZipFile: zip,
},
})
if err != nil {
return err
}
f.Log.Info("creating alias")
_, err = f.Service.CreateAlias(&lambda.CreateAliasInput{
FunctionName: &f.FunctionName,
FunctionVersion: created.Version,
Name: aws.String(CurrentAlias),
})
if err != nil {
return nil
}
f.Log.WithFields(log.Fields{
"version": *created.Version,
"name": f.FunctionName,
}).Info("function created")
return nil
}
// Invoke the remote Lambda function, returning the response and logs, if any.
func (f *Function) Invoke(event, context interface{}) (reply, logs io.Reader, err error) {
eventBytes, err := json.Marshal(event)
if err != nil {
return nil, nil, err
}
contextBytes, err := json.Marshal(context)
if err != nil {
return nil, nil, err
}
res, err := f.Service.Invoke(&lambda.InvokeInput{
ClientContext: aws.String(base64.StdEncoding.EncodeToString(contextBytes)),
FunctionName: &f.FunctionName,
InvocationType: aws.String(string(RequestResponse)),
LogType: aws.String("Tail"),
Qualifier: aws.String(CurrentAlias),
Payload: eventBytes,
})
if err != nil {
return nil, nil, err
}
logs = base64.NewDecoder(base64.StdEncoding, strings.NewReader(*res.LogResult))
if res.FunctionError != nil {
e := &InvokeError{
Handled: *res.FunctionError == "Handled",
}
if err := json.Unmarshal(res.Payload, e); err != nil {
return nil, logs, err
}
return nil, logs, e
}
reply = bytes.NewReader(res.Payload)
return reply, logs, nil
}
// Rollback the function to the previous.
func (f *Function) Rollback() error {
f.Log.Info("rolling back")
alias, err := f.Service.GetAlias(&lambda.GetAliasInput{
FunctionName: &f.FunctionName,
Name: aws.String(CurrentAlias),
})
if err != nil {
return err
}
f.Log.Infof("current version: %s", *alias.FunctionVersion)
list, err := f.Service.ListVersionsByFunction(&lambda.ListVersionsByFunctionInput{
FunctionName: &f.FunctionName,
})
if err != nil {
return err
}
versions := list.Versions[1:] // remove $LATEST
if len(versions) < 2 {
return errors.New("Can't rollback. Only one version deployed.")
}
latest := *versions[len(versions)-1].Version
prev := *versions[len(versions)-2].Version
rollback := latest
if *alias.FunctionVersion == latest {
rollback = prev
}
f.Log.Infof("rollback to version: %s", rollback)
_, err = f.Service.UpdateAlias(&lambda.UpdateAliasInput{
FunctionName: &f.FunctionName,
Name: aws.String(CurrentAlias),
FunctionVersion: &rollback,
})
return err
}
// RollbackVersion the function to the specified version.
func (f *Function) RollbackVersion(version string) error {
f.Log.Info("rolling back")
alias, err := f.Service.GetAlias(&lambda.GetAliasInput{
FunctionName: &f.FunctionName,
Name: aws.String(CurrentAlias),
})
if err != nil {
return err
}
f.Log.Infof("current version: %s", *alias.FunctionVersion)
if version == *alias.FunctionVersion {
return errors.New("Specified version currently deployed.")
}
_, err = f.Service.UpdateAlias(&lambda.UpdateAliasInput{
FunctionName: &f.FunctionName,
Name: aws.String(CurrentAlias),
FunctionVersion: &version,
})
return err
}
// BuildBytes returns the generated zip as bytes.
func (f *Function) BuildBytes() ([]byte, error) {
r, err := f.Build()
if err != nil {
return nil, err
}
b, err := ioutil.ReadAll(r)
if err != nil {
return nil, err
}
f.Log.Infof("created build (%s)", humanize.Bytes(uint64(len(b))))
return b, nil
}
// Build returns the zipped contents of the function.
func (f *Function) Build() (io.Reader, error) {
f.Log.Debugf("creating build")
buf := new(bytes.Buffer)
zip := archive.NewZipWriter(buf)
if err := f.hookBuild(zip); err != nil {
return nil, err
}
files, err := utils.LoadFiles(f.Path, f.IgnoredPatterns)
if err != nil {
return nil, err
}
for _, path := range files {
f.Log.WithField("file", path).Debug("add file to zip")
file, err := os.Open(filepath.Join(f.Path, path))
if err != nil {
return nil, err
}
if err := zip.AddFile(path, file); err != nil {
return nil, err
}
if err := file.Close(); err != nil {
return nil, err
}
}
if err := zip.Close(); err != nil {
return nil, err
}
return buf, nil
}
// Clean invokes the CleanHook, useful for removing build artifacts and so on.
func (f *Function) Clean() error {
return f.hookClean()
}
// GroupName returns the CloudWatchLogs group name.
func (f *Function) GroupName() string {
return fmt.Sprintf("/aws/lambda/%s", f.FunctionName)
}
// configChanged checks if function configuration differs from configuration stored in AWS Lambda
func (f *Function) configChanged(config *lambda.GetFunctionOutput) bool {
if f.Description != *config.Configuration.Description {
return true
}
if f.Memory != *config.Configuration.MemorySize {
return true
}
if f.Timeout != *config.Configuration.Timeout {
return true
}
if f.Role != *config.Configuration.Role {
return true
}
if f.Handler != *config.Configuration.Handler {
return true
}
return false
}
// hookOpen calls Openers.
func (f *Function) hookOpen() error {
for _, name := range f.Plugins {
if p, ok := plugins[name].(Opener); ok {
if err := p.Open(f); err != nil {
return err
}
}
}
return nil
}
// hookBuild calls Builders.
func (f *Function) hookBuild(zip *archive.Archive) error {
for _, name := range f.Plugins {
if p, ok := plugins[name].(Builder); ok {
if err := p.Build(f, zip); err != nil {
return err
}
}
}
return nil
}
// hookClean calls Cleaners.
func (f *Function) hookClean() error {
for _, name := range f.Plugins {
if p, ok := plugins[name].(Cleaner); ok {
if err := p.Clean(f); err != nil {
return err
}
}
}
return nil
}
// hookDeploy calls Deployers.
func (f *Function) hookDeploy() error {
for _, name := range f.Plugins {
if p, ok := plugins[name].(Deployer); ok {
if err := p.Deploy(f); err != nil {
return err
}
}
}
return nil
}