Skip to content

Commit

Permalink
apply change for LoggingConfig from v0
Browse files Browse the repository at this point in the history
refs #334
  • Loading branch information
fujiwara committed Nov 28, 2023
1 parent 9a2c19d commit a59439c
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 4 deletions.
2 changes: 2 additions & 0 deletions diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"encoding/base64"
"fmt"
"io"
"log"
"strings"

"github.com/fatih/color"
Expand Down Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions function_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
9 changes: 8 additions & 1 deletion lambroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion logs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions test/function.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
10 changes: 8 additions & 2 deletions test/function.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -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` }}',
Expand All @@ -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',
Expand Down
7 changes: 7 additions & 0 deletions utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit a59439c

Please sign in to comment.