-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
170 lines (137 loc) · 4 KB
/
main.tf
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# terraform init
# export AWS_ACCESS_KEY_ID=
# export AWS_SECRET_ACCESS_KEY=
# export AWS_REGION=
# export TF_VAR_name=
# export TF_VAR_github_app_key_base64=
# export TF_VAR_github_app_id=
# export TF_VAR_github_webhook_secret=
# export TF_VAR_email=
# export TF_VAR_repository_white_list=
# export TF_VAR_runner_white_list=
# terraform apply
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.84.0"
}
local = {
source = "hashicorp/local"
}
random = {
source = "hashicorp/random"
}
}
required_version = "~> 1.3.7"
}
locals {
tags = {
Name = "Custom GitHub Runners"
Url = "https://github.com/ipdxco/custom-github-runners"
}
filters = [
{
name = "Connection Refused"
metric = "ConnectionRefused"
pattern = "Connection refused"
group = "runner-startup"
},
{
name = "Parameter Not Found"
metric = "ParameterNotFound"
pattern = "ParameterNotFound"
group = "runner-startup"
},
{
name = "HTTP Request Timed Out"
metric = "HttpRequestTimedOut"
pattern = "HTTP request timed out"
group = "runner-startup"
}
]
}
provider "aws" {}
variable "name" {}
variable "github_app_key_base64" {}
variable "github_app_id" {}
variable "github_webhook_secret" {}
variable "email" {}
variable "repository_white_list" {
type = list(string)
}
variable "runner_white_list" {
type = list(string)
}
data "aws_region" "default" {}
data "aws_s3_bucket" "custom-github-runners" {
bucket = var.name
}
data "aws_caller_identity" "current" {}
# RETENTION
resource "aws_s3_bucket_lifecycle_configuration" "custom-github-runners_v2" {
count = length(local.runners) > 0 ? 1 : 0
bucket = data.aws_s3_bucket.custom-github-runners.id
# artifacts.tf
dynamic "rule" {
for_each = local.runners
content {
id = rule.key
filter {
prefix = "${rule.key}/"
}
expiration {
days = 90
}
status = "Enabled"
}
}
}
# LOG TO METRICS
resource "aws_cloudwatch_log_metric_filter" "custom-github-runners" {
for_each = { for config in flatten([
for runner in keys(module.multi-runner.runners_map) : [
for filter in local.filters : merge(filter, { runner = runner })
]
]) : "/github-self-hosted-runners/multi-${config.runner}/${config.group}/${config.metric}" => config }
name = each.value.name
pattern = each.value.pattern
log_group_name = "/github-self-hosted-runners/multi-${each.value.runner}/${each.value.group}"
metric_transformation {
name = each.value.metric
namespace = "/github-self-hosted-runners/multi-${each.value.runner}/${each.value.group}"
value = "1"
default_value = null
unit = "Count"
}
}
resource "aws_sns_topic" "custom-github-runners" {
name = "custom-github-runners"
}
resource "aws_sns_topic_subscription" "custom-github-runners" {
count = var.email != "" ? 1 : 0
topic_arn = aws_sns_topic.custom-github-runners.arn
protocol = "email"
endpoint = var.email
}
resource "aws_cloudwatch_metric_alarm" "custom-github-runners" {
for_each = aws_cloudwatch_log_metric_filter.custom-github-runners
alarm_name = "${each.value.metric_transformation[0].namespace}/${each.value.metric_transformation[0].name}"
alarm_description = "${each.value.name} exceeded threshold"
actions_enabled = true
alarm_actions = [aws_sns_topic.custom-github-runners.arn]
ok_actions = []
insufficient_data_actions = []
comparison_operator = "GreaterThanThreshold"
evaluation_periods = "1"
threshold = "0"
unit = "Count"
datapoints_to_alarm = "1"
treat_missing_data = "notBreaching"
# conflicts with metric_query
metric_name = each.value.metric_transformation[0].name
namespace = each.value.metric_transformation[0].namespace
period = "600"
statistic = "Sum"
tags = local.tags
}