Skip to content

Commit a9f5aba

Browse files
authored
feat: added possibility to disable CloudWatch logs (#60)
1 parent 7df81ed commit a9f5aba

File tree

6 files changed

+27
-8
lines changed

6 files changed

+27
-8
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ repos:
77
args: ['--envs=AWS_REGION="eu-west-1"']
88
- id: terraform_tflint
99
- repo: https://github.com/pre-commit/pre-commit-hooks
10-
rev: v4.2.0
10+
rev: v4.3.0
1111
hooks:
1212
- id: check-merge-conflict
1313
- id: trailing-whitespace

README.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,26 +190,30 @@ module "lambda" {
190190

191191
The module will create a [CloudWatch Log Group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group)
192192
for your Lambda function. It's retention period and [CloudWatch Logs subscription filters](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_subscription_filter)
193-
to stream logs to other Lambda functions (e.g. to forward logs to Amazon Elasticsearch Service) can be declared inline.
193+
to stream logs to other Lambda functions (e.g. to forward logs to Amazon OpenSearch Service) can be declared inline.
194194

195195
The module will create the required [Lambda permissions](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) automatically.
196+
Sending logs to CloudWatch can be disabled with `cloudwatch_logs_enabled = false`
196197

197198
see [example](examples/with-cloudwatch-logs-subscription) for details
198199

199200
```hcl
200201
module "lambda" {
201202
// see above
202203
204+
// disable CloudWatch logs
205+
// cloudwatch_logs_enabled = false
206+
203207
cloudwatch_logs_retention_in_days = 14
204208
205209
cloudwatch_log_subscription_filters = {
206210
lambda_1 = {
207211
//see https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_subscription_filter for available arguments
208-
destination_arn = module.destination_1.arn // required
212+
destination_arn = module.destination_1.arn
209213
}
210214
211215
lambda_2 = {
212-
destination_arn = module.destination_2.arn // required
216+
destination_arn = module.destination_2.arn
213217
}
214218
}
215219
}
@@ -344,6 +348,7 @@ No modules.
344348
| <a name="input_cloudwatch_event_rules"></a> [cloudwatch\_event\_rules](#input\_cloudwatch\_event\_rules) | Creates EventBridge (CloudWatch Events) rules invoking your Lambda function. Required Lambda invocation permissions will be generated. | `map(any)` | `{}` | no |
345349
| <a name="input_cloudwatch_lambda_insights_enabled"></a> [cloudwatch\_lambda\_insights\_enabled](#input\_cloudwatch\_lambda\_insights\_enabled) | Enable CloudWatch Lambda Insights for your Lambda function. | `bool` | `false` | no |
346350
| <a name="input_cloudwatch_log_subscription_filters"></a> [cloudwatch\_log\_subscription\_filters](#input\_cloudwatch\_log\_subscription\_filters) | CloudWatch Logs subscription filter resources. Currently supports only Lambda functions as destinations. | `map(any)` | `{}` | no |
351+
| <a name="input_cloudwatch_logs_enabled"></a> [cloudwatch\_logs\_enabled](#input\_cloudwatch\_logs\_enabled) | Enables your Lambda function to send logs to CloudWatch. The IAM role of this Lambda function will be enhanced with required permissions. | `bool` | `true` | no |
347352
| <a name="input_cloudwatch_logs_kms_key_id"></a> [cloudwatch\_logs\_kms\_key\_id](#input\_cloudwatch\_logs\_kms\_key\_id) | The ARN of the KMS Key to use when encrypting log data. | `string` | `null` | no |
348353
| <a name="input_cloudwatch_logs_retention_in_days"></a> [cloudwatch\_logs\_retention\_in\_days](#input\_cloudwatch\_logs\_retention\_in\_days) | Specifies the number of days you want to retain log events in the specified log group. Possible values are: 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 3653, and 0. If you select 0, the events in the log group are always retained and never expire. | `number` | `null` | no |
349354
| <a name="input_description"></a> [description](#input\_description) | Description of what your Lambda Function does. | `string` | `"Instruction set architecture for your Lambda function. Valid values are [\"x86_64\"] and [\"arm64\"]."` | no |

docs/part1.md

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,26 +190,30 @@ module "lambda" {
190190

191191
The module will create a [CloudWatch Log Group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group)
192192
for your Lambda function. It's retention period and [CloudWatch Logs subscription filters](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_subscription_filter)
193-
to stream logs to other Lambda functions (e.g. to forward logs to Amazon Elasticsearch Service) can be declared inline.
193+
to stream logs to other Lambda functions (e.g. to forward logs to Amazon OpenSearch Service) can be declared inline.
194194

195195
The module will create the required [Lambda permissions](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) automatically.
196+
Sending logs to CloudWatch can be disabled with `cloudwatch_logs_enabled = false`
196197

197198
see [example](examples/with-cloudwatch-logs-subscription) for details
198199

199200
```hcl
200201
module "lambda" {
201202
// see above
202203
204+
// disable CloudWatch logs
205+
// cloudwatch_logs_enabled = false
206+
203207
cloudwatch_logs_retention_in_days = 14
204208
205209
cloudwatch_log_subscription_filters = {
206210
lambda_1 = {
207211
//see https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_subscription_filter for available arguments
208-
destination_arn = module.destination_1.arn // required
212+
destination_arn = module.destination_1.arn
209213
}
210214
211215
lambda_2 = {
212-
destination_arn = module.destination_2.arn // required
216+
destination_arn = module.destination_2.arn
213217
}
214218
}
215219
}

docs/part2.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ No modules.
5454
| <a name="input_cloudwatch_event_rules"></a> [cloudwatch\_event\_rules](#input\_cloudwatch\_event\_rules) | Creates EventBridge (CloudWatch Events) rules invoking your Lambda function. Required Lambda invocation permissions will be generated. | `map(any)` | `{}` | no |
5555
| <a name="input_cloudwatch_lambda_insights_enabled"></a> [cloudwatch\_lambda\_insights\_enabled](#input\_cloudwatch\_lambda\_insights\_enabled) | Enable CloudWatch Lambda Insights for your Lambda function. | `bool` | `false` | no |
5656
| <a name="input_cloudwatch_log_subscription_filters"></a> [cloudwatch\_log\_subscription\_filters](#input\_cloudwatch\_log\_subscription\_filters) | CloudWatch Logs subscription filter resources. Currently supports only Lambda functions as destinations. | `map(any)` | `{}` | no |
57+
| <a name="input_cloudwatch_logs_enabled"></a> [cloudwatch\_logs\_enabled](#input\_cloudwatch\_logs\_enabled) | Enables your Lambda function to send logs to CloudWatch. The IAM role of this Lambda function will be enhanced with required permissions. | `bool` | `true` | no |
5758
| <a name="input_cloudwatch_logs_kms_key_id"></a> [cloudwatch\_logs\_kms\_key\_id](#input\_cloudwatch\_logs\_kms\_key\_id) | The ARN of the KMS Key to use when encrypting log data. | `string` | `null` | no |
5859
| <a name="input_cloudwatch_logs_retention_in_days"></a> [cloudwatch\_logs\_retention\_in\_days](#input\_cloudwatch\_logs\_retention\_in\_days) | Specifies the number of days you want to retain log events in the specified log group. Possible values are: 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, 3653, and 0. If you select 0, the events in the log group are always retained and never expire. | `number` | `null` | no |
5960
| <a name="input_description"></a> [description](#input\_description) | Description of what your Lambda Function does. | `string` | `"Instruction set architecture for your Lambda function. Valid values are [\"x86_64\"] and [\"arm64\"]."` | no |

iam.tf

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,18 @@ resource "aws_iam_role" "lambda" {
1414
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
1515
}
1616

17+
1718
resource "aws_iam_role_policy_attachment" "cloudwatch_logs" {
19+
count = var.cloudwatch_logs_enabled ? 1 : 0
20+
1821
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
1922
role = aws_iam_role.lambda.name
2023
}
2124

2225
resource "aws_iam_role_policy_attachment" "vpc_attachment" {
2326
count = var.vpc_config == null ? 0 : 1
2427

25-
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
28+
policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSLambdaENIManagementAccess"
2629
role = aws_iam_role.lambda.name
2730
}
2831

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ variable "cloudwatch_lambda_insights_enabled" {
3131
type = bool
3232
}
3333

34+
variable "cloudwatch_logs_enabled" {
35+
description = "Enables your Lambda function to send logs to CloudWatch. The IAM role of this Lambda function will be enhanced with required permissions."
36+
type = bool
37+
default = true
38+
}
39+
3440
variable "cloudwatch_logs_kms_key_id" {
3541
description = "The ARN of the KMS Key to use when encrypting log data."
3642
type = string

0 commit comments

Comments
 (0)