-
-
Notifications
You must be signed in to change notification settings - Fork 28
/
deploy.go
379 lines (347 loc) · 11.1 KB
/
deploy.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
package lambroll
import (
"bytes"
"context"
"encoding/json"
"errors"
"fmt"
"log"
"os"
"github.com/aereal/jsondiff"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/aws/aws-sdk-go-v2/service/lambda/types"
"github.com/itchyny/gojq"
)
// DeployOption represents an option for Deploy()
type DeployOption struct {
Src string `help:"function zip archive or src dir" default:"."`
Publish bool `help:"publish function" default:"true"`
AliasName string `name:"alias" help:"alias name for publish" default:"current"`
AliasToLatest bool `help:"set alias to unpublished $LATEST version" default:"false"`
DryRun bool `help:"dry run" default:"false"`
SkipArchive bool `help:"skip to create zip archive. requires Code.S3Bucket and Code.S3Key in function definition" default:"false"`
KeepVersions int `help:"Number of latest versions to keep. Older versions will be deleted. (Optional value: default 0)." default:"0"`
Ignore string `help:"ignore fields by jq queries in function.json" default:""`
FunctionURL string `help:"path to function-url definition" default:"" env:"LAMBROLL_FUNCTION_URL"`
SkipFunction bool `help:"skip to deploy a function. deploy function-url only" default:"false"`
ZipOption
}
func (opt DeployOption) label() string {
if opt.DryRun {
return "**DRY RUN**"
}
return ""
}
type versionAlias struct {
Version string
Name string
}
// Expand expands ExcludeFile contents to Excludes
func expandExcludeFile(file string) ([]string, error) {
b, err := os.ReadFile(file)
if err != nil {
if os.IsNotExist(err) {
return nil, nil
}
return nil, err
}
lines := bytes.Split(b, []byte{'\n'})
excludes := make([]string, 0, len(lines))
for _, line := range lines {
line = bytes.TrimSpace(line)
if len(line) == 0 || bytes.HasPrefix(line, []byte{'#'}) {
// skip blank or comment line
continue
}
excludes = append(excludes, string(line))
}
return excludes, nil
}
func (opt *DeployOption) String() string {
b, _ := json.Marshal(opt)
return string(b)
}
// Deploy deploys a new lambda function code
func (app *App) Deploy(ctx context.Context, opt *DeployOption) error {
if err := opt.Expand(); err != nil {
return err
}
log.Printf("[debug] %s", opt.String())
fn, err := app.loadFunction(app.functionFilePath)
if err != nil {
return fmt.Errorf("failed to load function: %w", err)
}
deployFunctionURL := func(context.Context) error { return nil }
if opt.FunctionURL != "" {
deployFunctionURL = func(ctx context.Context) error {
fc, err := app.loadFunctionUrl(opt.FunctionURL, *fn.FunctionName)
if err != nil {
return fmt.Errorf("failed to load function url config: %w", err)
}
return app.deployFunctionURL(ctx, fc, opt)
}
}
if opt.SkipFunction {
// skip to deploy a function. deploy function-url only
return deployFunctionURL(ctx)
}
log.Printf("[info] starting deploy function %s", *fn.FunctionName)
if current, err := app.lambda.GetFunction(ctx, &lambda.GetFunctionInput{
FunctionName: fn.FunctionName,
}); err != nil {
var nfe *types.ResourceNotFoundException
if !errors.As(err, &nfe) {
return err
}
if err := app.create(ctx, opt, fn); err != nil {
return err
}
if err := deployFunctionURL(ctx); err != nil {
return err
}
return nil
} else if err := validateUpdateFunction(current.Configuration, current.Code, fn); err != nil {
return err
}
fillDefaultValues(fn)
if err := app.prepareFunctionCodeForDeploy(ctx, opt, fn); err != nil {
return fmt.Errorf("failed to prepare function code for deploy: %w", err)
}
if ignore := opt.Ignore; ignore != "" {
q, err := gojq.Parse(ignore)
if err != nil {
return fmt.Errorf("failed to parse ignore query: %w", err)
}
q = jsondiff.WithUpdate(q)
fnAny, _ := marshalAny(fn)
fnAny, err = jsondiff.ModifyValue(q, fnAny)
if err != nil {
return fmt.Errorf("failed to modify function: %w", err)
}
src, _ := json.Marshal(fnAny)
fn = &Function{}
unmarshalJSON(src, &fn, app.functionFilePath)
}
log.Println("[info] updating function configuration", opt.label())
confIn := &lambda.UpdateFunctionConfigurationInput{
DeadLetterConfig: fn.DeadLetterConfig,
Description: fn.Description,
Environment: fn.Environment,
EphemeralStorage: fn.EphemeralStorage,
FunctionName: fn.FunctionName,
FileSystemConfigs: fn.FileSystemConfigs,
Handler: fn.Handler,
KMSKeyArn: fn.KMSKeyArn,
Layers: fn.Layers,
LoggingConfig: fn.LoggingConfig,
MemorySize: fn.MemorySize,
Role: fn.Role,
Runtime: fn.Runtime,
Timeout: fn.Timeout,
TracingConfig: fn.TracingConfig,
VpcConfig: fn.VpcConfig,
ImageConfig: fn.ImageConfig,
SnapStart: fn.SnapStart,
}
log.Printf("[debug] %s", jsonStr(confIn))
var newerVersion string
if !opt.DryRun {
proc := func(ctx context.Context) error {
return app.updateFunctionConfiguration(ctx, confIn)
}
if err := app.ensureLastUpdateStatusSuccessful(ctx, *fn.FunctionName, "updating function configuration", proc, opt.label()); err != nil {
return fmt.Errorf("failed to update function configuration: %w", err)
}
}
if err := app.updateTags(ctx, fn, opt); err != nil {
return err
}
codeIn := &lambda.UpdateFunctionCodeInput{
Architectures: fn.Architectures,
FunctionName: fn.FunctionName,
ZipFile: fn.Code.ZipFile,
S3Bucket: fn.Code.S3Bucket,
S3Key: fn.Code.S3Key,
S3ObjectVersion: fn.Code.S3ObjectVersion,
ImageUri: fn.Code.ImageUri,
}
if opt.DryRun {
codeIn.DryRun = true
} else {
codeIn.Publish = opt.Publish
}
var res *lambda.UpdateFunctionCodeOutput
proc := func(ctx context.Context) error {
var err error
// set res outside of this function
res, err = app.updateFunctionCode(ctx, codeIn)
return err
}
if err := app.ensureLastUpdateStatusSuccessful(ctx, *fn.FunctionName, "updating function code", proc, opt.label()); err != nil {
return err
}
if res.Version != nil {
newerVersion = *res.Version
log.Printf("[info] deployed version %s %s", *res.Version, opt.label())
} else {
newerVersion = versionLatest
log.Printf("[info] deployed version %s %s", newerVersion, opt.label())
}
if opt.DryRun {
return nil
}
if opt.Publish || opt.AliasToLatest {
err := app.updateAliases(ctx, *fn.FunctionName, versionAlias{newerVersion, opt.AliasName})
if err != nil {
return err
}
}
if opt.KeepVersions > 0 { // Ignore zero-value.
return app.deleteVersions(ctx, *fn.FunctionName, opt.KeepVersions)
}
if err := deployFunctionURL(ctx); err != nil {
return err
}
return nil
}
func (app *App) updateFunctionConfiguration(ctx context.Context, in *lambda.UpdateFunctionConfigurationInput) error {
retryer := retryPolicy.Start(ctx)
for retryer.Continue() {
_, err := app.lambda.UpdateFunctionConfiguration(ctx, in)
if err != nil {
var rce *types.ResourceConflictException
if errors.As(err, &rce) {
log.Println("[debug] retrying", rce.Error())
continue
}
return fmt.Errorf("failed to update function configuration: %w", err)
}
return nil
}
return fmt.Errorf("failed to update function configuration (max retries reached)")
}
func (app *App) updateFunctionCode(ctx context.Context, in *lambda.UpdateFunctionCodeInput) (*lambda.UpdateFunctionCodeOutput, error) {
var res *lambda.UpdateFunctionCodeOutput
retryer := retryPolicy.Start(ctx)
for retryer.Continue() {
var err error
res, err = app.lambda.UpdateFunctionCode(ctx, in)
if err != nil {
var rce *types.ResourceConflictException
if errors.As(err, &rce) {
log.Println("[debug] retrying", err)
continue
}
return nil, fmt.Errorf("failed to update function code: %w", err)
}
break
}
return res, nil
}
func (app *App) ensureLastUpdateStatusSuccessful(ctx context.Context, name string, msg string, code func(ctx context.Context) error, label string) error {
log.Println("[info]", msg, "...", label)
if err := app.waitForLastUpdateStatusSuccessful(ctx, name); err != nil {
return err
}
if err := code(ctx); err != nil {
return err
}
log.Println("[info]", msg, "accepted. waiting for LastUpdateStatus to be successful.", label)
if err := app.waitForLastUpdateStatusSuccessful(ctx, name); err != nil {
return err
}
log.Println("[info]", msg, "successfully", label)
return nil
}
func (app *App) waitForLastUpdateStatusSuccessful(ctx context.Context, name string) error {
retryer := retryPolicy.Start(ctx)
for retryer.Continue() {
res, err := app.lambda.GetFunction(ctx, &lambda.GetFunctionInput{
FunctionName: aws.String(name),
})
if err != nil {
log.Println("[warn] failed to get function, retrying", err)
continue
} else {
state := res.Configuration.State
last := res.Configuration.LastUpdateStatus
log.Printf("[info] State:%s LastUpdateStatus:%s", state, last)
if last == types.LastUpdateStatusSuccessful {
return nil
}
log.Printf("[info] waiting for LastUpdateStatus %s", types.LastUpdateStatusSuccessful)
}
}
return fmt.Errorf("max retries reached")
}
func (app *App) updateAliases(ctx context.Context, functionName string, vs ...versionAlias) error {
for _, v := range vs {
log.Printf("[info] updating alias set %s to version %s", v.Name, v.Version)
_, err := app.lambda.UpdateAlias(ctx, &lambda.UpdateAliasInput{
FunctionName: aws.String(functionName),
FunctionVersion: aws.String(v.Version),
Name: aws.String(v.Name),
})
if err != nil {
var nfe *types.ResourceNotFoundException
if errors.As(err, &nfe) {
log.Printf("[info] alias %s is not found. creating alias", v.Name)
_, err := app.lambda.CreateAlias(ctx, &lambda.CreateAliasInput{
FunctionName: aws.String(functionName),
FunctionVersion: aws.String(v.Version),
Name: aws.String(v.Name),
})
if err != nil {
return fmt.Errorf("failed to create alias: %w", err)
}
} else {
return fmt.Errorf("failed to update alias: %w", err)
}
}
log.Println("[info] alias updated")
}
return nil
}
func (app *App) deleteVersions(ctx context.Context, functionName string, keepVersions int) error {
if keepVersions <= 0 {
log.Printf("[info] specify --keep-versions")
return nil
}
params := &lambda.ListVersionsByFunctionInput{
FunctionName: aws.String(functionName),
}
// versions will be set asc order, like 1 to N
versions := []types.FunctionConfiguration{}
for {
res, err := app.lambda.ListVersionsByFunction(ctx, params)
if err != nil {
return fmt.Errorf("failed to list versions: %w", err)
}
versions = append(versions, res.Versions...)
if res.NextMarker != nil {
params.Marker = res.NextMarker
continue
}
break
}
keep := len(versions) - keepVersions
for i, v := range versions {
if i == 0 {
continue
}
if i >= keep {
break
}
log.Printf("[info] deleting function version: %s", *v.Version)
_, err := app.lambda.DeleteFunction(ctx, &lambda.DeleteFunctionInput{
FunctionName: aws.String(functionName),
Qualifier: v.Version,
})
if err != nil {
return fmt.Errorf("failed to delete version: %w", err)
}
}
log.Printf("[info] except %d latest versions are deleted", keepVersions)
return nil
}