diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 5e96145..a5c85d5 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -7,7 +7,7 @@ repos: args: ['--envs=AWS_REGION="eu-west-1"'] - id: terraform_tflint - repo: https://github.com/pre-commit/pre-commit-hooks - rev: v4.2.0 + rev: v4.3.0 hooks: - id: check-merge-conflict - id: trailing-whitespace diff --git a/README.md b/README.md index c92ab32..9c1dd05 100644 --- a/README.md +++ b/README.md @@ -190,9 +190,10 @@ module "lambda" { The module will create a [CloudWatch Log Group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) 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) -to stream logs to other Lambda functions (e.g. to forward logs to Amazon Elasticsearch Service) can be declared inline. +to stream logs to other Lambda functions (e.g. to forward logs to Amazon OpenSearch Service) can be declared inline. The module will create the required [Lambda permissions](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) automatically. +Sending logs to CloudWatch can be disabled with `cloudwatch_logs_enabled = false` see [example](examples/with-cloudwatch-logs-subscription) for details @@ -200,16 +201,19 @@ see [example](examples/with-cloudwatch-logs-subscription) for details module "lambda" { // see above + // disable CloudWatch logs + // cloudwatch_logs_enabled = false + cloudwatch_logs_retention_in_days = 14 cloudwatch_log_subscription_filters = { lambda_1 = { //see https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_subscription_filter for available arguments - destination_arn = module.destination_1.arn // required + destination_arn = module.destination_1.arn } lambda_2 = { - destination_arn = module.destination_2.arn // required + destination_arn = module.destination_2.arn } } } @@ -344,6 +348,7 @@ No modules. | [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 | | [cloudwatch\_lambda\_insights\_enabled](#input\_cloudwatch\_lambda\_insights\_enabled) | Enable CloudWatch Lambda Insights for your Lambda function. | `bool` | `false` | no | | [cloudwatch\_log\_subscription\_filters](#input\_cloudwatch\_log\_subscription\_filters) | CloudWatch Logs subscription filter resources. Currently supports only Lambda functions as destinations. | `map(any)` | `{}` | no | +| [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 | | [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 | | [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 | | [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 | diff --git a/docs/part1.md b/docs/part1.md index b802bfb..03eed84 100644 --- a/docs/part1.md +++ b/docs/part1.md @@ -190,9 +190,10 @@ module "lambda" { The module will create a [CloudWatch Log Group](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) 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) -to stream logs to other Lambda functions (e.g. to forward logs to Amazon Elasticsearch Service) can be declared inline. +to stream logs to other Lambda functions (e.g. to forward logs to Amazon OpenSearch Service) can be declared inline. The module will create the required [Lambda permissions](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) automatically. +Sending logs to CloudWatch can be disabled with `cloudwatch_logs_enabled = false` see [example](examples/with-cloudwatch-logs-subscription) for details @@ -200,16 +201,19 @@ see [example](examples/with-cloudwatch-logs-subscription) for details module "lambda" { // see above + // disable CloudWatch logs + // cloudwatch_logs_enabled = false + cloudwatch_logs_retention_in_days = 14 cloudwatch_log_subscription_filters = { lambda_1 = { //see https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_subscription_filter for available arguments - destination_arn = module.destination_1.arn // required + destination_arn = module.destination_1.arn } lambda_2 = { - destination_arn = module.destination_2.arn // required + destination_arn = module.destination_2.arn } } } diff --git a/docs/part2.md b/docs/part2.md index 6dfab3b..ee4eb8e 100644 --- a/docs/part2.md +++ b/docs/part2.md @@ -54,6 +54,7 @@ No modules. | [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 | | [cloudwatch\_lambda\_insights\_enabled](#input\_cloudwatch\_lambda\_insights\_enabled) | Enable CloudWatch Lambda Insights for your Lambda function. | `bool` | `false` | no | | [cloudwatch\_log\_subscription\_filters](#input\_cloudwatch\_log\_subscription\_filters) | CloudWatch Logs subscription filter resources. Currently supports only Lambda functions as destinations. | `map(any)` | `{}` | no | +| [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 | | [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 | | [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 | | [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 | diff --git a/iam.tf b/iam.tf index d5816b7..8747a93 100644 --- a/iam.tf +++ b/iam.tf @@ -14,7 +14,10 @@ resource "aws_iam_role" "lambda" { assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json } + resource "aws_iam_role_policy_attachment" "cloudwatch_logs" { + count = var.cloudwatch_logs_enabled ? 1 : 0 + policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" role = aws_iam_role.lambda.name } @@ -22,7 +25,7 @@ resource "aws_iam_role_policy_attachment" "cloudwatch_logs" { resource "aws_iam_role_policy_attachment" "vpc_attachment" { count = var.vpc_config == null ? 0 : 1 - policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole" + policy_arn = "arn:${data.aws_partition.current.partition}:iam::aws:policy/service-role/AWSLambdaENIManagementAccess" role = aws_iam_role.lambda.name } diff --git a/variables.tf b/variables.tf index c7c1a52..3626ad0 100644 --- a/variables.tf +++ b/variables.tf @@ -31,6 +31,12 @@ variable "cloudwatch_lambda_insights_enabled" { type = bool } +variable "cloudwatch_logs_enabled" { + description = "Enables your Lambda function to send logs to CloudWatch. The IAM role of this Lambda function will be enhanced with required permissions." + type = bool + default = true +} + variable "cloudwatch_logs_kms_key_id" { description = "The ARN of the KMS Key to use when encrypting log data." type = string