diff --git a/src/constructs/cloudwatch/__snapshots__/lambda-alarms.test.ts.snap b/src/constructs/cloudwatch/__snapshots__/lambda-alarms.test.ts.snap index f11189dba5..71a3feeade 100644 --- a/src/constructs/cloudwatch/__snapshots__/lambda-alarms.test.ts.snap +++ b/src/constructs/cloudwatch/__snapshots__/lambda-alarms.test.ts.snap @@ -39,6 +39,9 @@ exports[`GuLambdaThrottlingAlarm construct should match snapshot 1`] = ` }, }, "Handler": "handler.ts", + "LoggingConfig": { + "LogFormat": "JSON", + }, "MemorySize": 512, "Role": { "Fn::GetAtt": [ @@ -322,6 +325,9 @@ exports[`The GuLambdaErrorPercentageAlarm construct should create the correct al }, }, "Handler": "handler.ts", + "LoggingConfig": { + "LogFormat": "JSON", + }, "MemorySize": 512, "Role": { "Fn::GetAtt": [ diff --git a/src/constructs/lambda/lambda.test.ts b/src/constructs/lambda/lambda.test.ts index 4225fc106e..c5e8760abd 100644 --- a/src/constructs/lambda/lambda.test.ts +++ b/src/constructs/lambda/lambda.test.ts @@ -1,7 +1,8 @@ import { Duration } from "aws-cdk-lib"; import { Match, Template } from "aws-cdk-lib/assertions"; -import { Runtime } from "aws-cdk-lib/aws-lambda"; +import { LoggingFormat, Runtime } from "aws-cdk-lib/aws-lambda"; import { simpleGuStackForTesting } from "../../utils/test"; +import type { GuStack } from "../core"; import { GuLambdaFunction } from "./lambda"; describe("The GuLambdaFunction class", () => { @@ -193,6 +194,54 @@ describe("The GuLambdaFunction class", () => { }); }); + function hasLoggingFormat(stack: GuStack, logFormat: LoggingFormat) { + Template.fromStack(stack).hasResourceProperties("AWS::Lambda::Function", { + LoggingConfig: { + LogFormat: logFormat, + }, + }); + } + + it("should use JSON log formatting by default", () => { + const stack = simpleGuStackForTesting(); + + new GuLambdaFunction(stack, "lambda", { + fileName: "my-app.jar", + handler: "handler.ts", + runtime: Runtime.JAVA_17, + app: "testing", + }); + + hasLoggingFormat(stack, LoggingFormat.JSON); + }); + + it("should use JSON log formatting when JSON is specified", () => { + const stack = simpleGuStackForTesting(); + + new GuLambdaFunction(stack, "lambda", { + fileName: "my-app.jar", + handler: "handler.ts", + runtime: Runtime.JAVA_17, + app: "testing", + logFormat: "JSON", + }); + + hasLoggingFormat(stack, LoggingFormat.JSON); + }); + it("should use Text log formatting when it is defined", () => { + const stack = simpleGuStackForTesting(); + + new GuLambdaFunction(stack, "lambda", { + fileName: "my-app.jar", + handler: "handler.ts", + runtime: Runtime.JAVA_17, + app: "testing", + logFormat: "Text", + }); + + hasLoggingFormat(stack, LoggingFormat.TEXT); + }); + it("should not create an alias or version if the enableVersioning prop is unset", () => { const stack = simpleGuStackForTesting(); diff --git a/src/constructs/lambda/lambda.ts b/src/constructs/lambda/lambda.ts index 5762f45442..52c32b1814 100644 --- a/src/constructs/lambda/lambda.ts +++ b/src/constructs/lambda/lambda.ts @@ -2,7 +2,7 @@ import { Duration } from "aws-cdk-lib"; import type { PolicyStatement } from "aws-cdk-lib/aws-iam"; import type { FunctionProps, Runtime } from "aws-cdk-lib/aws-lambda"; -import { Alias, Code, Function, RuntimeFamily } from "aws-cdk-lib/aws-lambda"; +import { Alias, Code, Function, LoggingFormat, RuntimeFamily } from "aws-cdk-lib/aws-lambda"; import { Bucket } from "aws-cdk-lib/aws-s3"; import { StringParameter } from "aws-cdk-lib/aws-ssm"; import { GuDistributable } from "../../types"; @@ -124,6 +124,7 @@ export class GuLambdaFunction extends Function { bucketNamePath, withoutFilePrefix = false, withoutArtifactUpload = false, + logFormat = LoggingFormat.JSON, } = props; const bucketName = bucketNamePath @@ -141,6 +142,7 @@ export class GuLambdaFunction extends Function { const code = Code.fromBucket(bucket, objectKey); super(scope, id, { ...props, + logFormat, environment: { ...props.environment, ...defaultEnvironmentVariables, diff --git a/src/experimental/patterns/__snapshots__/kinesis-lambda.test.ts.snap b/src/experimental/patterns/__snapshots__/kinesis-lambda.test.ts.snap index 91fa179da5..b8dd50aec4 100644 --- a/src/experimental/patterns/__snapshots__/kinesis-lambda.test.ts.snap +++ b/src/experimental/patterns/__snapshots__/kinesis-lambda.test.ts.snap @@ -73,6 +73,9 @@ exports[`The GuKinesisLambda pattern should create the correct resources for a n }, "FunctionName": "my-lambda-function", "Handler": "my-lambda/handler", + "LoggingConfig": { + "LogFormat": "JSON", + }, "MemorySize": 512, "Role": { "Fn::GetAtt": [ diff --git a/src/experimental/patterns/__snapshots__/sns-lambda.test.ts.snap b/src/experimental/patterns/__snapshots__/sns-lambda.test.ts.snap index cff8c0d184..f65ae9f023 100644 --- a/src/experimental/patterns/__snapshots__/sns-lambda.test.ts.snap +++ b/src/experimental/patterns/__snapshots__/sns-lambda.test.ts.snap @@ -76,6 +76,9 @@ exports[`The GuSnsLambda pattern should create the correct resources for a new s }, "FunctionName": "my-lambda-function", "Handler": "my-lambda/handler", + "LoggingConfig": { + "LogFormat": "JSON", + }, "MemorySize": 512, "Role": { "Fn::GetAtt": [ diff --git a/src/patterns/__snapshots__/api-lambda.test.ts.snap b/src/patterns/__snapshots__/api-lambda.test.ts.snap index 761e967428..ac73acb4ab 100644 --- a/src/patterns/__snapshots__/api-lambda.test.ts.snap +++ b/src/patterns/__snapshots__/api-lambda.test.ts.snap @@ -126,6 +126,9 @@ exports[`The GuApiLambda pattern should allow us to link a domain name to a Lamb }, }, "Handler": "handler.ts", + "LoggingConfig": { + "LogFormat": "JSON", + }, "MemorySize": 512, "Role": { "Fn::GetAtt": [ @@ -838,6 +841,9 @@ exports[`The GuApiLambda pattern should create the correct resources with minima }, }, "Handler": "handler.ts", + "LoggingConfig": { + "LogFormat": "JSON", + }, "MemorySize": 512, "Role": { "Fn::GetAtt": [ diff --git a/src/patterns/__snapshots__/api-multiple-lambdas.test.ts.snap b/src/patterns/__snapshots__/api-multiple-lambdas.test.ts.snap index d74d1a0f54..e401cfbb7a 100644 --- a/src/patterns/__snapshots__/api-multiple-lambdas.test.ts.snap +++ b/src/patterns/__snapshots__/api-multiple-lambdas.test.ts.snap @@ -750,6 +750,9 @@ exports[`The GuApiGatewayWithLambdaByPath pattern should create the correct reso }, }, "Handler": "handler.ts", + "LoggingConfig": { + "LogFormat": "JSON", + }, "MemorySize": 512, "Role": { "Fn::GetAtt": [ @@ -957,6 +960,9 @@ exports[`The GuApiGatewayWithLambdaByPath pattern should create the correct reso }, }, "Handler": "handler.ts", + "LoggingConfig": { + "LogFormat": "JSON", + }, "MemorySize": 512, "Role": { "Fn::GetAtt": [ @@ -1164,6 +1170,9 @@ exports[`The GuApiGatewayWithLambdaByPath pattern should create the correct reso }, }, "Handler": "handler.ts", + "LoggingConfig": { + "LogFormat": "JSON", + }, "MemorySize": 512, "Role": { "Fn::GetAtt": [ @@ -1371,6 +1380,9 @@ exports[`The GuApiGatewayWithLambdaByPath pattern should create the correct reso }, }, "Handler": "handler.ts", + "LoggingConfig": { + "LogFormat": "JSON", + }, "MemorySize": 512, "Role": { "Fn::GetAtt": [ diff --git a/src/patterns/__snapshots__/scheduled-lambda.test.ts.snap b/src/patterns/__snapshots__/scheduled-lambda.test.ts.snap index b0c940c0f4..e617b7308d 100644 --- a/src/patterns/__snapshots__/scheduled-lambda.test.ts.snap +++ b/src/patterns/__snapshots__/scheduled-lambda.test.ts.snap @@ -39,6 +39,9 @@ exports[`The GuScheduledLambda pattern should create the correct resources with }, "FunctionName": "my-lambda-function", "Handler": "my-lambda/handler", + "LoggingConfig": { + "LogFormat": "JSON", + }, "MemorySize": 512, "Role": { "Fn::GetAtt": [