Skip to content

Commit 826839d

Browse files
committed
Manage the lambda log groups via terraform (#126)
1 parent c8ca901 commit 826839d

File tree

17 files changed

+158
-88
lines changed

17 files changed

+158
-88
lines changed

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Added
11+
12+
- feat: Manage log groups via module. When upgrading you have to import the log groups by AWS into your state. See below the example commands for the default example.
13+
```bash
14+
terraform import module.runners.module.runner_binaries.aws_cloudwatch_log_group.syncer "/aws/lambda/default-syncer"
15+
terraform import module.runners.module.runners.aws_cloudwatch_log_group.scale_up "/aws/lambda/default-scale-up"
16+
terraform import module.runners.module.runners.aws_cloudwatch_log_group.scale_down "/aws/lambda/default-scale-down"
17+
terraform import module.runners.module.webhook.aws_cloudwatch_log_group.webhook "/aws/lambda/default-webhook"
18+
```
19+
1020
## [0.4.0] - 2020-08-10
1121

1222
### Added

README.md

Lines changed: 41 additions & 41 deletions
Large diffs are not rendered by default.

main.tf

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ module "webhook" {
3838
sqs_build_queue = aws_sqs_queue.queued_builds
3939
github_app_webhook_secret = var.github_app.webhook_secret
4040

41-
lambda_zip = var.webhook_lambda_zip
42-
lambda_timeout = var.webhook_lambda_timeout
41+
lambda_zip = var.webhook_lambda_zip
42+
lambda_timeout = var.webhook_lambda_timeout
43+
logging_retention_in_days = var.logging_retention_in_days
4344

4445
role_path = var.role_path
4546
role_permissions_boundary = var.role_permissions_boundary
@@ -79,6 +80,7 @@ module "runners" {
7980
lambda_zip = var.runners_lambda_zip
8081
lambda_timeout_scale_up = var.runners_scale_up_lambda_timeout
8182
lambda_timeout_scale_down = var.runners_scale_down_lambda_timeout
83+
logging_retention_in_days = var.logging_retention_in_days
8284

8385
instance_profile_path = var.instance_profile_path
8486
role_path = var.role_path
@@ -99,8 +101,9 @@ module "runner_binaries" {
99101

100102
runner_architecture = substr(var.instance_type, 0, 2) == "a1" || substr(var.instance_type, 1, 2) == "6g" ? "arm64" : "x64"
101103

102-
lambda_zip = var.runner_binaries_syncer_lambda_zip
103-
lambda_timeout = var.runner_binaries_syncer_lambda_timeout
104+
lambda_zip = var.runner_binaries_syncer_lambda_zip
105+
lambda_timeout = var.runner_binaries_syncer_lambda_timeout
106+
logging_retention_in_days = var.logging_retention_in_days
104107

105108
role_path = var.role_path
106109
role_permissions_boundary = var.role_permissions_boundary

modules/runner-binaries-syncer/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ No requirements.
5454
| lambda\_schedule\_expression | Scheduler expression for action runner binary syncer. | `string` | `"cron(27 * * * ? *)"` | no |
5555
| lambda\_timeout | Time out of the lambda in seconds. | `number` | `300` | no |
5656
| lambda\_zip | File location of the lambda zip file. | `string` | `null` | no |
57+
| logging\_retention\_in\_days | Specifies the number of days you want to retain log events for the lambda log group. Possible values are: 0, 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, and 3653. | `number` | `7` | no |
5758
| role\_path | The path that will be added to the role, if not set the environment name will be used. | `string` | `null` | no |
5859
| role\_permissions\_boundary | Permissions boundary that will be added to the created role for the lambda. | `string` | `null` | no |
5960
| runner\_architecture | The platform architecture for the runner instance (x64, arm64), defaults to 'x64' | `string` | `"x64"` | no |

modules/runner-binaries-syncer/policies/lambda-cloudwatch.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
"Statement": [
44
{
55
"Effect": "Allow",
6-
"Action": [
7-
"logs:CreateLogGroup",
8-
"logs:CreateLogStream",
9-
"logs:PutLogEvents"
10-
],
11-
"Resource": "arn:aws:logs:*:*:*"
6+
"Action": ["logs:CreateLogStream", "logs:PutLogEvents"],
7+
"Resource": "${log_group_arn}"
128
}
139
]
1410
}

modules/runner-binaries-syncer/runner-binaries-syncer.tf

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,12 @@ resource "aws_lambda_function" "syncer" {
2323
tags = var.tags
2424
}
2525

26+
resource "aws_cloudwatch_log_group" "syncer" {
27+
name = "/aws/lambda/${aws_lambda_function.syncer.function_name}"
28+
retention_in_days = var.logging_retention_in_days
29+
tags = var.tags
30+
}
31+
2632
resource "aws_iam_role" "syncer_lambda" {
2733
name = "${var.environment}-action-syncer-lambda-role"
2834
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role_policy.json
@@ -47,7 +53,9 @@ resource "aws_iam_role_policy" "lambda_logging" {
4753
name = "${var.environment}-lambda-logging-policy-syncer"
4854
role = aws_iam_role.syncer_lambda.id
4955

50-
policy = templatefile("${path.module}/policies/lambda-cloudwatch.json", {})
56+
policy = templatefile("${path.module}/policies/lambda-cloudwatch.json", {
57+
log_group_arn = aws_cloudwatch_log_group.syncer.arn
58+
})
5159
}
5260

5361
resource "aws_iam_role_policy" "syncer" {

modules/runner-binaries-syncer/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,3 +54,9 @@ variable "runner_architecture" {
5454
type = string
5555
default = "x64"
5656
}
57+
58+
variable "logging_retention_in_days" {
59+
description = "Specifies the number of days you want to retain log events for the lambda log group. Possible values are: 0, 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, and 3653."
60+
type = number
61+
default = 7
62+
}

modules/runners/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@ No requirements.
7676
| lambda\_timeout\_scale\_down | Time out for the scale down lambda in seconds. | `number` | `60` | no |
7777
| lambda\_timeout\_scale\_up | Time out for the scale up lambda in seconds. | `number` | `60` | no |
7878
| lambda\_zip | File location of the lambda zip file. | `string` | `null` | no |
79+
| logging\_retention\_in\_days | Specifies the number of days you want to retain log events for the lambda log group. Possible values are: 0, 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, and 3653. | `number` | `7` | no |
7980
| market\_options | Market options for the action runner instances. | `string` | `"spot"` | no |
8081
| minimum\_running\_time\_in\_minutes | The time an ec2 action runner should be running at minimum before terminated if non busy. | `number` | `5` | no |
8182
| overrides | This maps provides the possibility to override some defaults. The following attributes are supported: `name_sg` overwrite the `Name` tag for all security groups created by this module. `name_runner_agent_instance` override the `Name` tag for the ec2 instance defined in the auto launch configuration. `name_docker_machine_runners` override the `Name` tag spot instances created by the runner agent. | `map(string)` | <pre>{<br> "name_runner": "",<br> "name_sg": ""<br>}</pre> | no |

modules/runners/policies/lambda-cloudwatch.json

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,8 @@
33
"Statement": [
44
{
55
"Effect": "Allow",
6-
"Action": [
7-
"logs:CreateLogGroup",
8-
"logs:CreateLogStream",
9-
"logs:PutLogEvents"
10-
],
11-
"Resource": "arn:aws:logs:*:*:*"
6+
"Action": ["logs:CreateLogStream", "logs:PutLogEvents"],
7+
"Resource": "${log_group_arn}"
128
}
139
]
1410
}

modules/runners/scale-down.tf

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,12 @@ resource "aws_lambda_function" "scale_down" {
3737
}
3838
}
3939

40+
resource "aws_cloudwatch_log_group" "scale_down" {
41+
name = "/aws/lambda/${aws_lambda_function.scale_down.function_name}"
42+
retention_in_days = var.logging_retention_in_days
43+
tags = var.tags
44+
}
45+
4046
resource "aws_cloudwatch_event_rule" "scale_down" {
4147
name = "${var.environment}-scale-down-rule"
4248
schedule_expression = var.scale_down_schedule_expression
@@ -71,7 +77,9 @@ resource "aws_iam_role_policy" "scale_down" {
7177
}
7278

7379
resource "aws_iam_role_policy" "scale_down_logging" {
74-
name = "${var.environment}-lambda-logging"
75-
role = aws_iam_role.scale_down.name
76-
policy = templatefile("${path.module}/policies/lambda-cloudwatch.json", {})
80+
name = "${var.environment}-lambda-logging"
81+
role = aws_iam_role.scale_down.name
82+
policy = templatefile("${path.module}/policies/lambda-cloudwatch.json", {
83+
log_group_arn = aws_cloudwatch_log_group.scale_down.arn
84+
})
7785
}

0 commit comments

Comments
 (0)