Skip to content

Commit ce150e3

Browse files
committed
Add cloudwatch-log-group module
1 parent 5bf7af3 commit ce150e3

File tree

6 files changed

+254
-0
lines changed

6 files changed

+254
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# cloudwatch-log-group
2+
3+
This module creates following resources.
4+
5+
- `aws_cloudwatch_log_group`
6+
- `aws_cloudwatch_log_stream` (optional)
7+
8+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
9+
## Requirements
10+
11+
| Name | Version |
12+
|------|---------|
13+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.2 |
14+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.22 |
15+
16+
## Providers
17+
18+
| Name | Version |
19+
|------|---------|
20+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 4.22.0 |
21+
22+
## Modules
23+
24+
No modules.
25+
26+
## Resources
27+
28+
| Name | Type |
29+
|------|------|
30+
| [aws_cloudwatch_log_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
31+
| [aws_cloudwatch_log_stream.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_stream) | resource |
32+
| [aws_resourcegroups_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/resourcegroups_group) | resource |
33+
34+
## Inputs
35+
36+
| Name | Description | Type | Default | Required |
37+
|------|-------------|------|---------|:--------:|
38+
| <a name="input_name"></a> [name](#input\_name) | (Required) The name of the CloudWatch log group. | `string` | n/a | yes |
39+
| <a name="input_encryption_kms_key"></a> [encryption\_kms\_key](#input\_encryption\_kms\_key) | (Optional) The ARN of the KMS Key to use when encrypting log data. Please note, after the AWS KMS CMK is disassociated from the log group, AWS CloudWatch Logs stops encrypting newly ingested data for the log group. All previously ingested data remains encrypted, and AWS CloudWatch Logs requires permissions for the CMK whenever the encrypted data is requested. | `string` | `null` | no |
40+
| <a name="input_module_tags_enabled"></a> [module\_tags\_enabled](#input\_module\_tags\_enabled) | (Optional) Whether to create AWS Resource Tags for the module informations. | `bool` | `true` | no |
41+
| <a name="input_resource_group_description"></a> [resource\_group\_description](#input\_resource\_group\_description) | (Optional) The description of Resource Group. | `string` | `"Managed by Terraform."` | no |
42+
| <a name="input_resource_group_enabled"></a> [resource\_group\_enabled](#input\_resource\_group\_enabled) | (Optional) Whether to create Resource Group to find and group AWS resources which are created by this module. | `bool` | `true` | no |
43+
| <a name="input_resource_group_name"></a> [resource\_group\_name](#input\_resource\_group\_name) | (Optional) The name of Resource Group. A Resource Group name can have a maximum of 127 characters, including letters, numbers, hyphens, dots, and underscores. The name cannot start with `AWS` or `aws`. | `string` | `""` | no |
44+
| <a name="input_retention_in_days"></a> [retention\_in\_days](#input\_retention\_in\_days) | (Optional) Specify the number of days to retain log events in the 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. Default to `90` days. | `number` | `90` | no |
45+
| <a name="input_streams"></a> [streams](#input\_streams) | (Optional) A list of log streams for the CloudWatch log group. | `set(string)` | `[]` | no |
46+
| <a name="input_tags"></a> [tags](#input\_tags) | (Optional) A map of tags to add to all resources. | `map(string)` | `{}` | no |
47+
48+
## Outputs
49+
50+
| Name | Description |
51+
|------|-------------|
52+
| <a name="output_arn"></a> [arn](#output\_arn) | The ARN of the CloudWatch log group. |
53+
| <a name="output_encryption_kms_key"></a> [encryption\_kms\_key](#output\_encryption\_kms\_key) | The ARN of the KMS Key for log data encryption. |
54+
| <a name="output_id"></a> [id](#output\_id) | The ID of the CloudWatch log group. |
55+
| <a name="output_name"></a> [name](#output\_name) | The name of CloudWatch log group. |
56+
| <a name="output_retention_in_days"></a> [retention\_in\_days](#output\_retention\_in\_days) | The number of days to retain log events in the log group. |
57+
| <a name="output_streams"></a> [streams](#output\_streams) | The list of log streams for the log group. |
58+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

modules/cloudwatch-log-group/main.tf

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
locals {
2+
metadata = {
3+
package = "terraform-aws-observability"
4+
version = trimspace(file("${path.module}/../../VERSION"))
5+
module = basename(path.module)
6+
name = var.name
7+
}
8+
module_tags = var.module_tags_enabled ? {
9+
"module.terraform.io/package" = local.metadata.package
10+
"module.terraform.io/version" = local.metadata.version
11+
"module.terraform.io/name" = local.metadata.module
12+
"module.terraform.io/full-name" = "${local.metadata.package}/${local.metadata.module}"
13+
"module.terraform.io/instance" = local.metadata.name
14+
} : {}
15+
}
16+
17+
18+
###################################################
19+
# CloudWatch Log Group
20+
###################################################
21+
22+
resource "aws_cloudwatch_log_group" "this" {
23+
name = var.name
24+
25+
retention_in_days = var.retention_in_days
26+
kms_key_id = var.encryption_kms_key
27+
28+
tags = merge(
29+
{
30+
"Name" = local.metadata.name
31+
},
32+
local.module_tags,
33+
var.tags,
34+
)
35+
}
36+
37+
38+
###################################################
39+
# Log Streams for CloudWatch Log Group
40+
###################################################
41+
42+
resource "aws_cloudwatch_log_stream" "this" {
43+
for_each = var.streams
44+
45+
name = each.key
46+
log_group_name = aws_cloudwatch_log_group.this.name
47+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
output "arn" {
2+
description = "The ARN of the CloudWatch log group."
3+
value = aws_cloudwatch_log_group.this.arn
4+
}
5+
6+
output "id" {
7+
description = "The ID of the CloudWatch log group."
8+
value = aws_cloudwatch_log_group.this.id
9+
}
10+
11+
output "name" {
12+
description = "The name of CloudWatch log group."
13+
value = aws_cloudwatch_log_group.this.name
14+
}
15+
16+
output "retention_in_days" {
17+
description = "The number of days to retain log events in the log group."
18+
value = aws_cloudwatch_log_group.this.retention_in_days
19+
}
20+
21+
output "encryption_kms_key" {
22+
description = "The ARN of the KMS Key for log data encryption."
23+
value = aws_cloudwatch_log_group.this.kms_key_id
24+
}
25+
26+
output "streams" {
27+
description = "The list of log streams for the log group."
28+
value = {
29+
for name, stream in aws_cloudwatch_log_stream.this :
30+
name => {
31+
arn = stream.arn
32+
name = stream.name
33+
}
34+
}
35+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
locals {
2+
resource_group_name = (var.resource_group_name != ""
3+
? var.resource_group_name
4+
: join(".", [
5+
local.metadata.package,
6+
local.metadata.module,
7+
replace(local.metadata.name, "/[^a-zA-Z0-9_\\.-]/", "-"),
8+
])
9+
)
10+
resource_group_filters = [
11+
for key, value in local.module_tags : {
12+
"Key" = key
13+
"Values" = [value]
14+
}
15+
]
16+
resource_group_query = <<-JSON
17+
{
18+
"ResourceTypeFilters": [
19+
"AWS::AllSupported"
20+
],
21+
"TagFilters": ${jsonencode(local.resource_group_filters)}
22+
}
23+
JSON
24+
}
25+
26+
resource "aws_resourcegroups_group" "this" {
27+
count = (var.resource_group_enabled && var.module_tags_enabled) ? 1 : 0
28+
29+
name = local.resource_group_name
30+
description = var.resource_group_description
31+
32+
resource_query {
33+
type = "TAG_FILTERS_1_0"
34+
query = local.resource_group_query
35+
}
36+
37+
tags = merge(
38+
{
39+
"Name" = local.resource_group_name
40+
},
41+
local.module_tags,
42+
var.tags,
43+
)
44+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
variable "name" {
2+
description = "(Required) The name of the CloudWatch log group."
3+
type = string
4+
}
5+
6+
variable "retention_in_days" {
7+
description = "(Optional) Specify the number of days to retain log events in the 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. Default to `90` days."
8+
type = number
9+
default = 90
10+
}
11+
12+
variable "encryption_kms_key" {
13+
description = "(Optional) The ARN of the KMS Key to use when encrypting log data. Please note, after the AWS KMS CMK is disassociated from the log group, AWS CloudWatch Logs stops encrypting newly ingested data for the log group. All previously ingested data remains encrypted, and AWS CloudWatch Logs requires permissions for the CMK whenever the encrypted data is requested."
14+
type = string
15+
default = null
16+
}
17+
18+
variable "streams" {
19+
description = "(Optional) A list of log streams for the CloudWatch log group."
20+
type = set(string)
21+
default = []
22+
nullable = false
23+
}
24+
25+
variable "tags" {
26+
description = "(Optional) A map of tags to add to all resources."
27+
type = map(string)
28+
default = {}
29+
nullable = false
30+
}
31+
32+
variable "module_tags_enabled" {
33+
description = "(Optional) Whether to create AWS Resource Tags for the module informations."
34+
type = bool
35+
default = true
36+
nullable = false
37+
}
38+
39+
40+
###################################################
41+
# Resource Group
42+
###################################################
43+
44+
variable "resource_group_enabled" {
45+
description = "(Optional) Whether to create Resource Group to find and group AWS resources which are created by this module."
46+
type = bool
47+
default = true
48+
}
49+
50+
variable "resource_group_name" {
51+
description = "(Optional) The name of Resource Group. A Resource Group name can have a maximum of 127 characters, including letters, numbers, hyphens, dots, and underscores. The name cannot start with `AWS` or `aws`."
52+
type = string
53+
default = ""
54+
}
55+
56+
variable "resource_group_description" {
57+
description = "(Optional) The description of Resource Group."
58+
type = string
59+
default = "Managed by Terraform."
60+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_version = ">= 1.2"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = ">= 4.22"
8+
}
9+
}
10+
}

0 commit comments

Comments
 (0)