Skip to content

Commit

Permalink
chore(sdk): switch function log format from text to JSON for AWS …
Browse files Browse the repository at this point in the history
…platforms (#5955)

A best practice when authoring Lambda functions is to use the Powertools for AWS Lambda [Logger](https://docs.powertools.aws.dev/lambda/typescript/latest/core/logger/), which allows to emit structured logs in JSON format. However, even if you choose not to use Powertools, you can still take advantage of the JSON format to make navigating through your functions' logs much easier. This is true especially for the system logs, over which you have no direct control.

*By submitting this pull request, I confirm that my contribution is made under the terms of the [Wing Cloud Contribution License](https://github.com/winglang/wing/blob/main/CONTRIBUTION_LICENSE.md)*.
  • Loading branch information
garysassano authored Jul 4, 2024
1 parent a7171c7 commit 3ad156b
Show file tree
Hide file tree
Showing 94 changed files with 590 additions and 3 deletions.
2 changes: 1 addition & 1 deletion apps/wing/src/commands/test/fixtures/main.w.tf-aws.snap.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"required_providers": {
"aws": {
"source": "aws",
"version": "5.31.0"
"version": "5.56.1"
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions libs/awscdk/src/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Code,
Runtime,
LayerVersion,
LogFormat,
} from "aws-cdk-lib/aws-lambda";
import { LogGroup, RetentionDays } from "aws-cdk-lib/aws-logs";
import { Asset } from "aws-cdk-lib/aws-s3-assets";
Expand Down Expand Up @@ -191,6 +192,7 @@ export class Function
memorySize: props.memory ?? 1024,
architecture: Architecture.ARM_64,
logGroup: logs,
logFormat: LogFormat.JSON,
});
}

Expand Down
22 changes: 22 additions & 0 deletions libs/awscdk/test/__snapshots__/api.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ exports[`api configured for cors 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apiget0LogGroupB0A43FFB",
},
Expand Down Expand Up @@ -621,6 +622,7 @@ exports[`api url can be used as environment variable 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "FnLogGroup27CC2AC8",
},
Expand Down Expand Up @@ -990,6 +992,7 @@ exports[`api with 'name' & 'age' parameter 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apigetnameage0LogGroup06FA8F29",
},
Expand Down Expand Up @@ -1351,6 +1354,7 @@ exports[`api with 'name' parameter 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apigetname0LogGroup8430423E",
},
Expand Down Expand Up @@ -1703,6 +1707,7 @@ exports[`api with CONNECT route 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apiconnect0LogGroup1650F1E1",
},
Expand Down Expand Up @@ -2055,6 +2060,7 @@ exports[`api with DELETE route 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apidelete0LogGroupD287E557",
},
Expand Down Expand Up @@ -2407,6 +2413,7 @@ exports[`api with GET route at root 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apiget0LogGroupB0A43FFB",
},
Expand Down Expand Up @@ -2835,6 +2842,7 @@ exports[`api with GET routes with common prefix 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apigethellobat0LogGroup462CC819",
},
Expand Down Expand Up @@ -2911,6 +2919,7 @@ exports[`api with GET routes with common prefix 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apigethellofoo0LogGroup02F88FE3",
},
Expand Down Expand Up @@ -3339,6 +3348,7 @@ exports[`api with GET routes with different prefix 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apigetfoobar0LogGroup83F006C8",
},
Expand Down Expand Up @@ -3415,6 +3425,7 @@ exports[`api with GET routes with different prefix 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apigethellofoo0LogGroup02F88FE3",
},
Expand Down Expand Up @@ -3767,6 +3778,7 @@ exports[`api with HEAD route 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apihead0LogGroup1D4A092E",
},
Expand Down Expand Up @@ -4119,6 +4131,7 @@ exports[`api with OPTIONS route 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apioptions0LogGroup9252C37D",
},
Expand Down Expand Up @@ -4471,6 +4484,7 @@ exports[`api with PATCH route 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apipatch0LogGroupB991FA81",
},
Expand Down Expand Up @@ -4823,6 +4837,7 @@ exports[`api with POST route 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apipost0LogGroup0EB50BDA",
},
Expand Down Expand Up @@ -5175,6 +5190,7 @@ exports[`api with PUT route 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apiput0LogGroupD231D4EA",
},
Expand Down Expand Up @@ -5603,6 +5619,7 @@ exports[`api with multiple GET route and one lambda 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apigethellofoo0LogGroup02F88FE3",
},
Expand Down Expand Up @@ -6031,6 +6048,7 @@ exports[`api with multiple methods and multiple lambda 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apigethellofoo0LogGroup02F88FE3",
},
Expand Down Expand Up @@ -6107,6 +6125,7 @@ exports[`api with multiple methods and multiple lambda 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apiposthellobat0LogGroup4E1A6722",
},
Expand Down Expand Up @@ -6535,6 +6554,7 @@ exports[`api with multiple methods and one lambda 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apigethellofoo0LogGroup02F88FE3",
},
Expand Down Expand Up @@ -6961,6 +6981,7 @@ exports[`api with multiple methods on same route 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apiget0LogGroupB0A43FFB",
},
Expand Down Expand Up @@ -7322,6 +7343,7 @@ exports[`api with path parameter 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "Apigethelloworld0LogGroupDD091C4B",
},
Expand Down
6 changes: 6 additions & 0 deletions libs/awscdk/test/__snapshots__/bucket.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,7 @@ def submit_response(event: dict, context, response_status: str, error_message: s
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "mybucketOnCreate0LogGroup8E3C4A72",
},
Expand Down Expand Up @@ -729,6 +730,7 @@ def submit_response(event: dict, context, response_status: str, error_message: s
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "mybucketOnDelete0LogGroup44992347",
},
Expand Down Expand Up @@ -1156,6 +1158,7 @@ def submit_response(event: dict, context, response_status: str, error_message: s
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "mybucketOnCreate0LogGroup8E3C4A72",
},
Expand Down Expand Up @@ -1232,6 +1235,7 @@ def submit_response(event: dict, context, response_status: str, error_message: s
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "mybucketOnDelete0LogGroup44992347",
},
Expand Down Expand Up @@ -1308,6 +1312,7 @@ def submit_response(event: dict, context, response_status: str, error_message: s
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "mybucketOnUpdate0LogGroupFDC75EA3",
},
Expand Down Expand Up @@ -1667,6 +1672,7 @@ def submit_response(event: dict, context, response_status: str, error_message: s
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "mybucketOnUpdate0LogGroupFDC75EA3",
},
Expand Down
5 changes: 5 additions & 0 deletions libs/awscdk/test/__snapshots__/counter.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ exports[`dec() policy statement 2`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "FunctionLogGroup55B80E27",
},
Expand Down Expand Up @@ -380,6 +381,7 @@ exports[`function with a counter binding 2`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "FunctionLogGroup55B80E27",
},
Expand Down Expand Up @@ -565,6 +567,7 @@ exports[`inc() policy statement 2`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "FunctionLogGroup55B80E27",
},
Expand Down Expand Up @@ -750,6 +753,7 @@ exports[`peek() policy statement 2`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "FunctionLogGroup55B80E27",
},
Expand Down Expand Up @@ -935,6 +939,7 @@ exports[`set() policy statement 2`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "FunctionLogGroup55B80E27",
},
Expand Down
8 changes: 8 additions & 0 deletions libs/awscdk/test/__snapshots__/function.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ exports[`basic function 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "FunctionLogGroup55B80E27",
},
Expand Down Expand Up @@ -148,6 +149,7 @@ exports[`basic function with custom log retention 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "FunctionLogGroup55B80E27",
},
Expand Down Expand Up @@ -267,6 +269,7 @@ exports[`basic function with environment variables 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "FunctionLogGroup55B80E27",
},
Expand Down Expand Up @@ -384,6 +387,7 @@ exports[`basic function with infinite log retention 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "FunctionLogGroup55B80E27",
},
Expand Down Expand Up @@ -498,6 +502,7 @@ exports[`basic function with memory size specified 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "FunctionLogGroup55B80E27",
},
Expand Down Expand Up @@ -615,6 +620,7 @@ exports[`basic function with standard log retention 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "FunctionLogGroup55B80E27",
},
Expand Down Expand Up @@ -732,6 +738,7 @@ exports[`basic function with timeout explicitly set 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "FunctionLogGroup55B80E27",
},
Expand Down Expand Up @@ -849,6 +856,7 @@ exports[`source map setting 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "FunctionLogGroup55B80E27",
},
Expand Down
3 changes: 3 additions & 0 deletions libs/awscdk/test/__snapshots__/on-deploy.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ exports[`create an OnDeploy 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "myondeployFunctionLogGroup6E16B2CD",
},
Expand Down Expand Up @@ -587,6 +588,7 @@ exports[`execute OnDeploy after other resources 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "myondeployFunctionLogGroup6E16B2CD",
},
Expand Down Expand Up @@ -954,6 +956,7 @@ exports[`execute OnDeploy before other resources 1`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "myondeployFunctionLogGroup6E16B2CD",
},
Expand Down
1 change: 1 addition & 0 deletions libs/awscdk/test/__snapshots__/queue.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ exports[`queue with a consumer function 2`] = `
},
"Handler": "index.handler",
"LoggingConfig": {
"LogFormat": "JSON",
"LogGroup": {
"Ref": "QueueSetConsumer0LogGroupC0386522",
},
Expand Down
Loading

0 comments on commit 3ad156b

Please sign in to comment.