From a59439c9f91b7da556385b12dfecef7f6dfa3564 Mon Sep 17 00:00:00 2001 From: fujiwara Date: Tue, 28 Nov 2023 16:57:50 +0900 Subject: [PATCH] apply change for LoggingConfig from v0 refs #334 --- diff.go | 2 ++ function_test.go | 3 +++ lambroll.go | 9 ++++++++- logs.go | 2 +- test/function.json | 6 ++++++ test/function.jsonnet | 10 ++++++++-- utils.go | 7 +++++++ 7 files changed, 35 insertions(+), 4 deletions(-) diff --git a/diff.go b/diff.go index f99b7d4..ddf49e5 100644 --- a/diff.go +++ b/diff.go @@ -6,6 +6,7 @@ import ( "encoding/base64" "fmt" "io" + "log" "strings" "github.com/fatih/color" @@ -57,6 +58,7 @@ func (app *App) Diff(ctx context.Context, opt *DiffOption) error { latest = res.Configuration code = res.Code { + log.Println("[debug] list tags Resource", app.functionArn(ctx, name)) res, err := app.lambda.ListTags(ctx, &lambda.ListTagsInput{ // Tagging operations are permitted on Lambda functions only. // Tags on aliases and versions are not supported. diff --git a/function_test.go b/function_test.go index 3d614d2..808eadc 100644 --- a/function_test.go +++ b/function_test.go @@ -60,6 +60,9 @@ func TestLoadFunction(t *testing.T) { if len(arch) != 2 || arch[0] != "x86_64" || arch[1] != "arm64" { t.Errorf("unexpected Architectures %v", fn.Architectures) } + if *fn.LoggingConfig.LogGroup != "/aws/lambda/test/json" { + t.Errorf("unexpected LoggingConfig %v", fn.LoggingConfig) + } if *fn.EphemeralStorage.Size != 1024 { t.Errorf("unexpected EphemeralStorage %v", fn.EphemeralStorage) } diff --git a/lambroll.go b/lambroll.go index 1c8ac0a..930d5b5 100644 --- a/lambroll.go +++ b/lambroll.go @@ -195,7 +195,7 @@ func (app *App) AWSAccountID(ctx context.Context) string { svc := sts.NewFromConfig(app.awsConfig) r, err := svc.GetCallerIdentity(ctx, &sts.GetCallerIdentityInput{}) if err != nil { - log.Println("[warn] failed to get caller identity", err) + log.Println("[warn] failed to get caller identity.", err) return "" } app.accountID = *r.Account @@ -252,6 +252,7 @@ func newFunctionFrom(c *types.FunctionConfiguration, code *types.FunctionCodeLoc EphemeralStorage: c.EphemeralStorage, FunctionName: c.FunctionName, Handler: c.Handler, + LoggingConfig: c.LoggingConfig, MemorySize: c.MemorySize, Role: c.Role, Runtime: c.Runtime, @@ -313,6 +314,12 @@ func fillDefaultValues(fn *Function) { if fn.MemorySize == nil { fn.MemorySize = aws.Int32(128) } + if fn.LoggingConfig == nil { + fn.LoggingConfig = &types.LoggingConfig{ + LogFormat: types.LogFormatText, + LogGroup: aws.String(resolveLogGroup(fn)), + } + } if fn.TracingConfig == nil { fn.TracingConfig = &types.TracingConfig{ Mode: types.TracingModePassThrough, diff --git a/logs.go b/logs.go index f7e106d..b8f318e 100644 --- a/logs.go +++ b/logs.go @@ -23,7 +23,7 @@ func (app *App) Logs(ctx context.Context, opt *LogsOption) error { return fmt.Errorf("failed to load function: %w", err) } - logGroup := "/aws/lambda/" + *fn.FunctionName + logGroup := resolveLogGroup(fn) command := []string{ "aws", "--profile", app.profile, diff --git a/test/function.json b/test/function.json index 33e4c10..76e39df 100644 --- a/test/function.json +++ b/test/function.json @@ -22,6 +22,12 @@ } ], "Handler": "index.js", + "LoggingConfig": { + "ApplicationLogLevel": "DEBUG", + "LogFormat": "JSON", + "LogGroup": "/aws/lambda/{{ must_env `FUNCTION_NAME` }}/json", + "SystemLogLevel": "INFO" + }, "MemorySize": 128, "Role": "{{ tfstate `data.aws_iam_role.lambda.arn` }}", "Runtime": "nodejs12.x", diff --git a/test/function.jsonnet b/test/function.jsonnet index 8d27dd5..affda7b 100644 --- a/test/function.jsonnet +++ b/test/function.jsonnet @@ -5,13 +5,13 @@ ], Description: std.extVar('Description'), EphemeralStorage: { - Size: 1024 + Size: 1024, }, Environment: { Variables: { JSON: '{{ env `JSON` | json_escape }}', PREFIXED_TFSTATE_1: '{{ prefix1_tfstate `data.aws_iam_role.lambda.arn` }}', - PREFIXED_TFSTATE_2: '{{ prefix2_tfstate `data.aws_iam_role.lambda.arn` }}' + PREFIXED_TFSTATE_2: '{{ prefix2_tfstate `data.aws_iam_role.lambda.arn` }}', }, }, FunctionName: '{{ must_env `FUNCTION_NAME` }}', @@ -22,6 +22,12 @@ }, ], Handler: 'index.js', + LoggingConfig: { + ApplicationLogLevel: 'DEBUG', + LogFormat: 'JSON', + LogGroup: '/aws/lambda/{{ must_env `FUNCTION_NAME` }}/json', + SystemLogLevel: 'INFO', + }, MemorySize: std.extVar('MemorySize'), Role: '{{ tfstate `data.aws_iam_role.lambda.arn` }}', Runtime: 'nodejs12.x', diff --git a/utils.go b/utils.go index cc22406..d1d466b 100644 --- a/utils.go +++ b/utils.go @@ -77,3 +77,10 @@ func jsonToJsonnet(src []byte, filepath string) ([]byte, error) { } return []byte(s), nil } + +func resolveLogGroup(fn *Function) string { + if fn.LoggingConfig != nil && fn.LoggingConfig.LogGroup != nil { + return *fn.LoggingConfig.LogGroup + } + return fmt.Sprintf("/aws/lambda/%s", *fn.FunctionName) +}