Skip to content

Commit 5b0e0bc

Browse files
authored
feat: Added module for Cloudwatch log group (#23)
1 parent 88f9f62 commit 5b0e0bc

File tree

9 files changed

+140
-6
lines changed

9 files changed

+140
-6
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ module "log_metric_filter" {
2323

2424
Read [Filter and Pattern Syntax](https://docs.aws.amazon.com/AmazonCloudWatch/latest/logs/FilterAndPatternSyntax.html) for explanation of `pattern`.
2525

26+
### Log group
27+
28+
```hcl
29+
module "log_group" {
30+
source = "terraform-aws-modules/cloudwatch/aws//modules/log-group"
31+
version = "~> 2.0"
32+
33+
name = "my-app"
34+
retention_in_days = 120
35+
}
36+
```
37+
2638
### Metric alarm
2739

2840
```hcl

examples/complete-log-metric-filter-and-alarm/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ No providers.
3232
|------|--------|---------|
3333
| <a name="module_alarm"></a> [alarm](#module\_alarm) | ../../modules/metric-alarm | |
3434
| <a name="module_aws_sns_topic"></a> [aws\_sns\_topic](#module\_aws\_sns\_topic) | ../fixtures/aws_sns_topic | |
35-
| <a name="module_log"></a> [log](#module\_log) | ../fixtures/aws_cloudwatch_log_group | |
35+
| <a name="module_log_group"></a> [log\_group](#module\_log\_group) | ../../modules/log-group | |
3636
| <a name="module_log_metric_filter"></a> [log\_metric\_filter](#module\_log\_metric\_filter) | ../../modules/log-metric-filter | |
3737

3838
## Resources
@@ -47,6 +47,8 @@ No inputs.
4747

4848
| Name | Description |
4949
|------|-------------|
50+
| <a name="output_cloudwatch_log_group_arn"></a> [cloudwatch\_log\_group\_arn](#output\_cloudwatch\_log\_group\_arn) | ARN of Cloudwatch log group |
51+
| <a name="output_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#output\_cloudwatch\_log\_group\_name) | Name of Cloudwatch log group |
5052
| <a name="output_cloudwatch_log_metric_filter_id"></a> [cloudwatch\_log\_metric\_filter\_id](#output\_cloudwatch\_log\_metric\_filter\_id) | The name of the metric filter |
5153
| <a name="output_cloudwatch_metric_alarm_arn"></a> [cloudwatch\_metric\_alarm\_arn](#output\_cloudwatch\_metric\_alarm\_arn) | The ARN of the Cloudwatch metric alarm |
5254
| <a name="output_cloudwatch_metric_alarm_id"></a> [cloudwatch\_metric\_alarm\_id](#output\_cloudwatch\_metric\_alarm\_id) | The ID of the Cloudwatch metric alarm |

examples/complete-log-metric-filter-and-alarm/main.tf

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,10 @@ module "aws_sns_topic" {
66
source = "../fixtures/aws_sns_topic"
77
}
88

9-
module "log" {
10-
source = "../fixtures/aws_cloudwatch_log_group"
9+
module "log_group" {
10+
source = "../../modules/log-group"
11+
12+
name_prefix = "my-app-"
1113
}
1214

1315
locals {
@@ -18,9 +20,9 @@ locals {
1820
module "log_metric_filter" {
1921
source = "../../modules/log-metric-filter"
2022

21-
log_group_name = module.log.cloudwatch_log_group_name
23+
log_group_name = module.log_group.cloudwatch_log_group_name
2224

23-
name = "metric-${module.log.random_id}"
25+
name = "metric-${module.log_group.cloudwatch_log_group_name}"
2426
pattern = "ERROR"
2527

2628
metric_transformation_namespace = local.metric_transformation_namespace
@@ -30,7 +32,7 @@ module "log_metric_filter" {
3032
module "alarm" {
3133
source = "../../modules/metric-alarm"
3234

33-
alarm_name = "log-errors-${module.log.random_id}"
35+
alarm_name = "log-errors-${module.log_group.cloudwatch_log_group_name}"
3436
alarm_description = "Log errors are too high"
3537
comparison_operator = "GreaterThanOrEqualToThreshold"
3638
evaluation_periods = 1

examples/complete-log-metric-filter-and-alarm/outputs.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
output "cloudwatch_log_group_name" {
2+
description = "Name of Cloudwatch log group"
3+
value = module.log_group.cloudwatch_log_group_name
4+
}
5+
6+
output "cloudwatch_log_group_arn" {
7+
description = "ARN of Cloudwatch log group"
8+
value = module.log_group.cloudwatch_log_group_arn
9+
}
10+
111
output "cloudwatch_log_metric_filter_id" {
212
description = "The name of the metric filter"
313
value = module.log_metric_filter.cloudwatch_log_metric_filter_id

modules/log-group/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# log-group
2+
3+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
4+
## Requirements
5+
6+
| Name | Version |
7+
|------|---------|
8+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 0.12.26 |
9+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 2.55 |
10+
11+
## Providers
12+
13+
| Name | Version |
14+
|------|---------|
15+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 2.55 |
16+
17+
## Modules
18+
19+
No modules.
20+
21+
## Resources
22+
23+
| Name | Type |
24+
|------|------|
25+
| [aws_cloudwatch_log_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
26+
27+
## Inputs
28+
29+
| Name | Description | Type | Default | Required |
30+
|------|-------------|------|---------|:--------:|
31+
| <a name="input_create"></a> [create](#input\_create) | Whether to create the Cloudwatch log group | `bool` | `true` | no |
32+
| <a name="input_kms_key_id"></a> [kms\_key\_id](#input\_kms\_key\_id) | The ARN of the KMS Key to use when encrypting logs | `string` | `null` | no |
33+
| <a name="input_name"></a> [name](#input\_name) | A name for the log group | `string` | `null` | no |
34+
| <a name="input_name_prefix"></a> [name\_prefix](#input\_name\_prefix) | A name prefix for the log group | `string` | `null` | no |
35+
| <a name="input_retention_in_days"></a> [retention\_in\_days](#input\_retention\_in\_days) | Specifies the number of days you want to retain log events in the specified log group | `number` | `null` | no |
36+
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to add to Cloudwatch log group | `map(string)` | `{}` | no |
37+
38+
## Outputs
39+
40+
| Name | Description |
41+
|------|-------------|
42+
| <a name="output_cloudwatch_log_group_arn"></a> [cloudwatch\_log\_group\_arn](#output\_cloudwatch\_log\_group\_arn) | ARN of Cloudwatch log group |
43+
| <a name="output_cloudwatch_log_group_name"></a> [cloudwatch\_log\_group\_name](#output\_cloudwatch\_log\_group\_name) | Name of Cloudwatch log group |
44+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

modules/log-group/main.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
resource "aws_cloudwatch_log_group" "this" {
2+
count = var.create ? 1 : 0
3+
4+
name = var.name
5+
name_prefix = var.name_prefix
6+
retention_in_days = var.retention_in_days
7+
kms_key_id = var.kms_key_id
8+
9+
tags = var.tags
10+
}

modules/log-group/outputs.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "cloudwatch_log_group_name" {
2+
description = "Name of Cloudwatch log group"
3+
value = element(concat(aws_cloudwatch_log_group.this.*.name, [""]), 0)
4+
}
5+
6+
output "cloudwatch_log_group_arn" {
7+
description = "ARN of Cloudwatch log group"
8+
value = element(concat(aws_cloudwatch_log_group.this.*.arn, [""]), 0)
9+
}

modules/log-group/variables.tf

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
variable "create" {
2+
description = "Whether to create the Cloudwatch log group"
3+
type = bool
4+
default = true
5+
}
6+
7+
variable "name" {
8+
description = "A name for the log group"
9+
type = string
10+
default = null
11+
}
12+
13+
variable "name_prefix" {
14+
description = "A name prefix for the log group"
15+
type = string
16+
default = null
17+
}
18+
19+
variable "retention_in_days" {
20+
description = "Specifies the number of days you want to retain log events in the specified log group"
21+
type = number
22+
default = null
23+
}
24+
25+
variable "kms_key_id" {
26+
description = "The ARN of the KMS Key to use when encrypting logs"
27+
type = string
28+
default = null
29+
}
30+
31+
variable "tags" {
32+
description = "A map of tags to add to Cloudwatch log group"
33+
type = map(string)
34+
default = {}
35+
}

modules/log-group/versions.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 0.12.26"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 2.55"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)