Skip to content

Commit f64e41f

Browse files
thisismanamoritzzimmer
authored andcommitted
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 885b1b1 commit f64e41f

File tree

5 files changed

+50
-3
lines changed

5 files changed

+50
-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: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ module "lambda" {
3737
}
3838
}
3939

40+
logging_config = {
41+
log_format = "JSON"
42+
application_log_level = "INFO"
43+
system_log_level = "WARN"
44+
}
45+
4046
// AWS Systems Manager (SSM) Parameter Store
4147
ssm = {
4248
parameter_names = ["/internal/params", "/external/params"]

examples/fixtures/context/index.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
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+
3+
console.debug({ event, context })
4+
console.info("Hello from Lambda!")
5+
console.warn("This is a warning message!")
6+
7+
return context.logStreamName
48
}

main.tf

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,17 @@ resource "aws_lambda_function" "lambda" {
7777
}
7878
}
7979

80+
dynamic "logging_config" {
81+
for_each = var.logging_config == null ? [] : [var.logging_config]
82+
content {
83+
application_log_level = logging_config.value.application_log_level
84+
log_format = logging_config.value.log_format
85+
log_group = logging_config.value.log_group
86+
system_log_level = logging_config.value.system_log_level
87+
}
88+
}
89+
90+
8091
// create the CloudWatch log group first so it's no create automatically
8192
// by AWS Lambda
8293
depends_on = [aws_cloudwatch_log_group.lambda]
@@ -158,6 +169,16 @@ resource "aws_lambda_function" "lambda_external_lifecycle" {
158169
}
159170
}
160171

172+
dynamic "logging_config" {
173+
for_each = var.logging_config == null ? [] : [var.logging_config]
174+
content {
175+
application_log_level = logging_config.value.application_log_level
176+
log_format = logging_config.value.log_format
177+
log_group = logging_config.value.log_group
178+
system_log_level = logging_config.value.system_log_level
179+
}
180+
}
181+
161182
// create the CloudWatch log group first so it's no create automatically
162183
// by AWS Lambda
163184
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)