-
-
Notifications
You must be signed in to change notification settings - Fork 29
/
lambroll.go
442 lines (394 loc) · 10.9 KB
/
lambroll.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
package lambroll
import (
"context"
"fmt"
"log"
"os"
"path/filepath"
"text/template"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
awsconfig "github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/lambda"
"github.com/aws/aws-sdk-go-v2/service/lambda/types"
"github.com/aws/aws-sdk-go-v2/service/s3"
"github.com/aws/aws-sdk-go-v2/service/sts"
"github.com/fujiwara/ssm-lookup/ssm"
"github.com/fujiwara/tfstate-lookup/tfstate"
"github.com/google/go-jsonnet"
"github.com/hashicorp/go-envparse"
"github.com/kayac/go-config"
"github.com/shogo82148/go-retry"
)
var Version string
const (
versionLatest = "$LATEST"
packageTypeImage = "Image"
)
var retryPolicy = retry.Policy{
MinDelay: time.Second,
MaxDelay: 10 * time.Second,
MaxCount: 30,
}
// Function represents configuration of Lambda function
// type Function = lambda.CreateFunctionInput
type Function lambda.CreateFunctionInput
// Tags represents tags of function
type Tags map[string]string
func (app *App) functionArn(ctx context.Context, name string) string {
return fmt.Sprintf(
"arn:aws:lambda:%s:%s:function:%s",
app.awsConfig.Region,
app.AWSAccountID(ctx),
name,
)
}
var (
// IgnoreFilename defines file name includes ignore patterns at creating zip archive.
IgnoreFilename = ".lambdaignore"
// DefaultFunctionFilename defines file name for function definition.
DefaultFunctionFilenames = []string{
"function.json",
"function.jsonnet",
}
DefaultFunctionURLFilenames = []string{
"function_url.json",
"function_url.jsonnet",
}
// FunctionZipFilename defines file name for zip archive downloaded at init.
FunctionZipFilename = "function.zip"
// DefaultExcludes is a preset excludes file list
DefaultExcludes = []string{
IgnoreFilename,
DefaultFunctionFilenames[0],
DefaultFunctionFilenames[1],
DefaultFunctionURLFilenames[0],
DefaultFunctionURLFilenames[1],
FunctionZipFilename,
".git/*",
".terraform/*",
"terraform.tfstate",
}
// CurrentAliasName is alias name for current deployed function
CurrentAliasName = "current"
)
// App represents lambroll application
type App struct {
callerIdentity *CallerIdentity
profile string
loader *config.Loader
awsConfig aws.Config
lambda *lambda.Client
extStr map[string]string
extCode map[string]string
nativeFuncs []*jsonnet.NativeFunction
functionFilePath string
}
func newAwsConfig(ctx context.Context, opt *Option) (aws.Config, error) {
var region string
if opt.Region != nil && *opt.Region != "" {
region = aws.ToString(opt.Region)
}
optFuncs := []func(*awsconfig.LoadOptions) error{
awsconfig.WithRegion(region),
}
if opt.Endpoint != nil && *opt.Endpoint != "" {
customResolver := aws.EndpointResolverWithOptionsFunc(func(service, region string, options ...interface{}) (aws.Endpoint, error) {
if service == lambda.ServiceID || service == sts.ServiceID || service == s3.ServiceID {
return aws.Endpoint{
PartitionID: "aws",
URL: *opt.Endpoint,
SigningRegion: region,
}, nil
}
// returning EndpointNotFoundError will allow the service to fallback to it's default resolution
return aws.Endpoint{}, &aws.EndpointNotFoundError{}
})
optFuncs = append(optFuncs, awsconfig.WithEndpointResolverWithOptions(customResolver))
}
if opt.Profile != nil && *opt.Profile != "" {
optFuncs = append(optFuncs, awsconfig.WithSharedConfigProfile(*opt.Profile))
}
return awsconfig.LoadDefaultConfig(ctx, optFuncs...)
}
// New creates an application
func New(ctx context.Context, opt *Option) (*App, error) {
for _, envfile := range opt.Envfile {
if err := exportEnvFile(envfile); err != nil {
return nil, err
}
}
v2cfg, err := newAwsConfig(ctx, opt)
if err != nil {
return nil, err
}
var profile string
if opt.Profile != nil && *opt.Profile != "" {
profile = *opt.Profile
}
loader := config.New()
nativeFuncs := DefaultJsonnetNativeFuncs()
// load ssm functions
if ssmFuncs, err := ssm.FuncMap(ctx, v2cfg); err != nil {
return nil, err
} else {
loader.Funcs(ssmFuncs)
}
if ssmNativeFuncs, err := ssm.JsonnetNativeFuncs(ctx, v2cfg); err != nil {
return nil, err
} else {
nativeFuncs = append(nativeFuncs, ssmNativeFuncs...)
}
// load tfstate functions
if opt.TFState != nil && *opt.TFState != "" {
lookup, err := tfstate.ReadURL(ctx, *opt.TFState)
if err != nil {
return nil, err
}
loader.Funcs(lookup.FuncMap(ctx))
nativeFuncs = append(nativeFuncs, lookup.JsonnetNativeFuncs(ctx)...)
}
if len(opt.PrefixedTFState) > 0 {
prefixedFuncs := make(template.FuncMap)
for prefix, path := range opt.PrefixedTFState {
if prefix == "" {
return nil, fmt.Errorf("--prefixed-tfstate option cannot have empty key")
}
loader, err := tfstate.ReadURL(ctx, path)
if err != nil {
return nil, err
}
for name, f := range loader.FuncMap(ctx) {
prefixedFuncs[prefix+name] = f
}
nativeFuncs = append(nativeFuncs, loader.JsonnetNativeFuncsWithPrefix(ctx, prefix)...)
}
loader.Funcs(prefixedFuncs)
}
callerIdentity := newCallerIdentity(v2cfg)
nativeFuncs = append(nativeFuncs, callerIdentity.JsonnetNativeFuncs(ctx)...)
loader.Funcs(callerIdentity.FuncMap(ctx))
app := &App{
callerIdentity: callerIdentity,
profile: profile,
loader: loader,
awsConfig: v2cfg,
lambda: lambda.NewFromConfig(v2cfg),
functionFilePath: opt.Function,
nativeFuncs: nativeFuncs,
extStr: opt.ExtStr,
extCode: opt.ExtCode,
}
return app, nil
}
// AWSAccountID returns AWS account ID in current session
func (app *App) AWSAccountID(ctx context.Context) string {
return app.callerIdentity.Account(ctx)
}
func loadDefinitionFile[T any](app *App, path string, defaults []string) (*T, error) {
if path == "" {
p, err := findDefinitionFile("", defaults)
if err != nil {
return nil, err
}
path = p
}
var (
src []byte
err error
)
switch filepath.Ext(path) {
case ".jsonnet":
vm := jsonnet.MakeVM()
for _, f := range app.nativeFuncs {
vm.NativeFunction(f)
}
for k, v := range app.extStr {
vm.ExtVar(k, v)
}
for k, v := range app.extCode {
vm.ExtCode(k, v)
}
jsonStr, err := vm.EvaluateFile(path)
if err != nil {
return nil, err
}
src, err = app.loader.ReadWithEnvBytes([]byte(jsonStr))
if err != nil {
return nil, err
}
default:
src, err = app.loader.ReadWithEnv(path)
if err != nil {
return nil, err
}
}
var v T
if err := unmarshalJSON(src, &v, path); err != nil {
return nil, fmt.Errorf("failed to load %s: %w", path, err)
}
return &v, nil
}
func (app *App) loadFunction(path string) (*Function, error) {
return loadDefinitionFile[Function](app, path, DefaultFunctionFilenames)
}
func newFunctionFrom(c *types.FunctionConfiguration, code *types.FunctionCodeLocation, tags Tags) *Function {
if c == nil {
return nil
}
fn := &Function{
Architectures: c.Architectures,
Description: c.Description,
EphemeralStorage: c.EphemeralStorage,
FunctionName: c.FunctionName,
Handler: c.Handler,
LoggingConfig: c.LoggingConfig,
MemorySize: c.MemorySize,
Role: c.Role,
Runtime: c.Runtime,
Timeout: c.Timeout,
DeadLetterConfig: c.DeadLetterConfig,
FileSystemConfigs: c.FileSystemConfigs,
KMSKeyArn: c.KMSKeyArn,
SnapStart: newSnapStart(c.SnapStart),
}
if e := c.Environment; e != nil {
fn.Environment = &types.Environment{
Variables: e.Variables,
}
}
if i := c.ImageConfigResponse; i != nil && i.ImageConfig != nil {
fn.ImageConfig = &types.ImageConfig{
Command: i.ImageConfig.Command,
EntryPoint: i.ImageConfig.EntryPoint,
WorkingDirectory: i.ImageConfig.WorkingDirectory,
}
}
for _, layer := range c.Layers {
fn.Layers = append(fn.Layers, *layer.Arn)
}
if t := c.TracingConfig; t != nil {
fn.TracingConfig = &types.TracingConfig{
Mode: t.Mode,
}
}
if v := c.VpcConfig; v != nil && *v.VpcId != "" {
fn.VpcConfig = &types.VpcConfig{
SubnetIds: v.SubnetIds,
SecurityGroupIds: v.SecurityGroupIds,
Ipv6AllowedForDualStack: v.Ipv6AllowedForDualStack,
}
}
if (code != nil && aws.ToString(code.RepositoryType) == "ECR") || fn.PackageType == types.PackageTypeImage {
log.Printf("[debug] Image URL=%s", *code.ImageUri)
fn.PackageType = types.PackageTypeImage
fn.Code = &types.FunctionCode{
ImageUri: code.ImageUri,
}
}
fn.Tags = tags
return fn
}
func fillDefaultValues(fn *Function) {
if fn == nil {
return
}
if len(fn.Architectures) == 0 {
fn.Architectures = []types.Architecture{types.ArchitectureX8664}
}
if fn.Description == nil {
fn.Description = aws.String("")
}
if fn.MemorySize == nil {
fn.MemorySize = aws.Int32(128)
}
if fn.Layers == nil {
fn.Layers = []string{}
}
if lc := fn.LoggingConfig; lc == nil {
fn.LoggingConfig = &types.LoggingConfig{
LogFormat: types.LogFormatText,
LogGroup: aws.String(resolveLogGroup(fn)),
}
} else {
if lc.LogFormat == "" {
lc.LogFormat = types.LogFormatText
}
if lc.LogGroup == nil {
lc.LogGroup = aws.String(resolveLogGroup(fn))
}
if lc.ApplicationLogLevel == "" && lc.LogFormat == types.LogFormatJson {
lc.ApplicationLogLevel = types.ApplicationLogLevelInfo
}
if lc.SystemLogLevel == "" && lc.LogFormat == types.LogFormatJson {
lc.SystemLogLevel = types.SystemLogLevelInfo
}
}
if fn.TracingConfig == nil {
fn.TracingConfig = &types.TracingConfig{
Mode: types.TracingModePassThrough,
}
}
if fn.EphemeralStorage == nil {
fn.EphemeralStorage = &types.EphemeralStorage{
Size: aws.Int32(512),
}
}
if fn.Timeout == nil {
fn.Timeout = aws.Int32(3)
}
if fn.SnapStart == nil {
fn.SnapStart = &types.SnapStart{
ApplyOn: types.SnapStartApplyOnNone,
}
}
}
func newSnapStart(s *types.SnapStartResponse) *types.SnapStart {
if s == nil {
return nil
}
return &types.SnapStart{
ApplyOn: s.ApplyOn,
}
}
func exportEnvFile(file string) error {
if file == "" {
return nil
}
f, err := os.Open(file)
if err != nil {
return err
}
defer f.Close()
envs, err := envparse.Parse(f)
if err != nil {
return err
}
for key, value := range envs {
os.Setenv(key, value)
}
return nil
}
var errCannotUpdateImageAndZip = fmt.Errorf("cannot update function code between Image and Zip")
func validateUpdateFunction(currentConf *types.FunctionConfiguration, currentCode *types.FunctionCodeLocation, newFn *Function) error {
if currentConf == nil {
// create new function
return nil
}
newCode := newFn.Code
// new=Image
if newCode != nil && newCode.ImageUri != nil || newFn.PackageType == packageTypeImage {
// current=Zip
if currentCode == nil || currentCode.ImageUri == nil {
return errCannotUpdateImageAndZip
}
}
// current=Image
if currentCode != nil && currentCode.ImageUri != nil || currentConf != nil && currentConf.PackageType == types.PackageTypeImage {
// new=Zip
if newCode == nil || newCode.ImageUri == nil {
return errCannotUpdateImageAndZip
}
}
return nil
}