Skip to content

Commit f1f5d57

Browse files
committed
feat: added lambda logging config
Allow configuring aws lambda logging Sample: ``` logging_config = { log_format = "JSON" application_log_level = "INFO" system_log_level = "WARN" } ```
1 parent 6c29450 commit f1f5d57

File tree

5 files changed

+44
-3
lines changed

5 files changed

+44
-3
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -415,6 +415,7 @@ No modules.
415415
| <a name="input_kms_key_arn"></a> [kms\_key\_arn](#input\_kms\_key\_arn) | Amazon Resource Name (ARN) of the AWS Key Management Service (KMS) key that is used to encrypt environment variables. If this configuration is not provided when environment variables are in use, AWS Lambda uses a default service key. If this configuration is provided when environment variables are not in use, the AWS Lambda API does not save this configuration and Terraform will show a perpetual difference of adding the key. To fix the perpetual difference, remove this configuration. | `string` | `""` | no |
416416
| <a name="input_lambda_at_edge"></a> [lambda\_at\_edge](#input\_lambda\_at\_edge) | Enable Lambda@Edge for your Node.js or Python functions. Required trust relationship and publishing of function versions will be configured. | `bool` | `false` | no |
417417
| <a name="input_layers"></a> [layers](#input\_layers) | List of Lambda Layer Version ARNs (maximum of 5) to attach to your Lambda Function. | `list(string)` | `[]` | no |
418+
| <a name="input_logging_config"></a> [logging\_config](#input\_logging\_config) | The function's Amazon CloudWatch Logs configuration settings. | <pre>object({<br/> log_format = string<br/> application_log_level = optional(string, null)<br/> log_group = optional(string, null)<br/> system_log_level = optional(string, null)<br/> })</pre> | `null` | no |
418419
| <a name="input_memory_size"></a> [memory\_size](#input\_memory\_size) | Amount of memory in MB your Lambda Function can use at runtime. | `number` | `128` | no |
419420
| <a name="input_package_type"></a> [package\_type](#input\_package\_type) | The Lambda deployment package type. Valid values are Zip and Image. | `string` | `"Zip"` | no |
420421
| <a name="input_publish"></a> [publish](#input\_publish) | Whether to publish creation/change as new Lambda Function Version. | `bool` | `false` | no |

examples/complete/main.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,11 @@ module "lambda" {
3737
}
3838
}
3939

40+
logging_config = {
41+
log_format = "JSON"
42+
application_log_level = "INFO"
43+
}
44+
4045
// AWS Systems Manager (SSM) Parameter Store
4146
ssm = {
4247
parameter_names = ["/internal/params", "/external/params"]

examples/fixtures/context/index.js

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
exports.handler = async function(event, context) {
2-
console.log("EVENT: \n" + JSON.stringify(event, null, 2))
3-
return context.logStreamName
1+
exports.handler = async function (event, context) {
2+
const logMessage = {
3+
"message": "Hello from Lambda!",
4+
"level": "INFO",
5+
"timestamp": new Date().toISOString(),
6+
"context": {
7+
"functionName": context.functionName,
8+
"functionVersion": context.functionVersion,
9+
},
10+
"event": event
11+
}
12+
console.log(JSON.stringify(logMessage, null, 2))
13+
return context.logStreamName
414
}

main.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ resource "aws_lambda_function" "lambda_external_lifecycle" {
158158
}
159159
}
160160

161+
dynamic "logging_config" {
162+
for_each = var.logging_config == null ? [] : [var.logging_config]
163+
content {
164+
application_log_level = logging_config.value.application_log_level
165+
log_format = logging_config.value.log_format
166+
log_group = logging_config.value.log_group
167+
system_log_level = logging_config.value.system_log_level
168+
}
169+
}
170+
161171
// create the CloudWatch log group first so it's no create automatically
162172
// by AWS Lambda
163173
depends_on = [aws_cloudwatch_log_group.lambda]

variables.tf

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,21 @@ variable "vpc_config" {
249249
})
250250
}
251251

252+
variable "logging_config" {
253+
description = "The function's Amazon CloudWatch Logs configuration settings."
254+
default = null
255+
type = object({
256+
log_format = string
257+
application_log_level = optional(string, null)
258+
log_group = optional(string, null)
259+
system_log_level = optional(string, null)
260+
})
261+
validation {
262+
condition = var.logging_config == null || (var.logging_config.log_format == "JSON" || var.logging_config.log_format == "Text")
263+
error_message = "log_format must be either 'JSON' or 'Text'"
264+
}
265+
}
266+
252267
variable "iam_role_name" {
253268
description = "Override the name of the IAM role for the function. Otherwise the default will be your function name with the region as a suffix."
254269
default = null

0 commit comments

Comments
 (0)