-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.tf
executable file
·176 lines (145 loc) · 6.22 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
171
172
173
174
175
176
locals {
enabled = coalesce(var.enabled, module.this.enabled, true)
name = coalesce(var.name, module.this.name, "github-runner-${random_string.github_runner_random_suffix.result}")
runner_module_version = "v5.15.1"
aws_account_id = module.this.enabled && var.aws_account_id != "" ? var.aws_account_id : try(data.aws_caller_identity.current[0].account_id, "")
aws_region_name = module.this.enabled && var.aws_region_name != "" ? var.aws_region_name : try(data.aws_region.current[0].name, "")
aws_kv_namespace = trim(coalesce(var.aws_kv_namespace, "github-runner/${module.github_runner_label.id}"), "/")
docker_config_sm_secret_name = "${local.aws_kv_namespace}/config/docker"
webhook_password = coalesce(var.github_app_webhook_password, random_password.webhook.result)
}
data "aws_caller_identity" "current" {
count = module.this.enabled && var.aws_account_id == "" ? 1 : 0
}
data "aws_region" "current" {
count = module.this.enabled && var.aws_region_name == "" ? 1 : 0
}
# ================================================================== runners ===
module "github_runner_label" {
source = "cloudposse/label/null"
version = "0.25.0"
enabled = local.enabled
name = local.name
context = module.this.context
}
# only appliable if name variable was not set
resource "random_string" "github_runner_random_suffix" {
length = 6
special = false
upper = false
}
module "github_runner" {
source = "philips-labs/github-runner/aws"
version = "v5.15.1" # should match local.runner_module_version
prefix = module.github_runner_label.id
enable_ephemeral_runners = var.runner_ephemeral_mode_enabled
enable_organization_runners = var.github_organization_runner_enabled
minimum_running_time_in_minutes = var.runner_min_running_time
runner_extra_labels = var.runner_labels
runner_as_root = true # required for docker
runner_iam_role_managed_policy_arns = [aws_iam_policy.runner.arn]
runner_binaries_s3_sse_configuration = { rule = { apply_server_side_encryption_by_default = { sse_algorithm = "AES256" } } }
runners_maximum_count = var.runner_maximum_count
pool_runner_owner = var.github_organization
scale_up_reserved_concurrent_executions = -1
aws_region = local.aws_region_name
userdata_template = "${path.module}/assets/instance/userdata.sh"
enable_runner_detailed_monitoring = true
enable_ssm_on_runners = true
ami_filter = { name = [var.instance_ami_name], state = ["available"] }
instance_target_capacity_type = lower(var.instance_lifecycle_type)
instance_types = var.instance_types
instance_allocation_strategy = "capacity-optimized"
logging_retention_in_days = var.log_retention
subnet_ids = var.vpc_subnet_ids
vpc_id = var.vpc_id
enable_cloudwatch_agent = false
idle_config = !var.runner_ephemeral_mode_enabled ? [
for x in var.runner_pool_config : {
cron = x.cron
timeZone = x.tz
idleCount = x.count
}
] : []
pool_config = var.runner_ephemeral_mode_enabled ? [
for x in var.runner_pool_config : {
schedule_expression = "cron(${x.cron})"
size = x.count
}
] : []
block_device_mappings = [{
delete_on_termination = true
device_name = "/dev/xvda"
encrypted = true
iops = 1000
kms_key_id = null
snapshot_id = null
throughput = null
volume_size = 100
volume_type = "gp3"
}]
github_app = {
key_base64 = var.github_app_secrets.key
id = var.github_app_secrets.id
webhook_secret = local.webhook_password
}
runner_metadata_options = {
http_endpoint = "enabled"
http_put_response_hop_limit = 1
http_tokens = "required"
instance_metadata_tags = "enabled"
}
ssm_paths = {
root = "${local.aws_kv_namespace}/config"
}
webhook_lambda_zip = "${module.runner_binaries.artifact_package_path}/webhook.zip"
runner_binaries_syncer_lambda_zip = "${module.runner_binaries.artifact_package_path}/runner-binaries-syncer.zip"
runners_lambda_zip = "${module.runner_binaries.artifact_package_path}/runners.zip"
tags = merge(
{ for k, v in module.github_runner_label.tags : k => v if lower(k) != "name" },
{ "ghr:docker_config_sm_secret_name" = local.docker_config_sm_secret_name },
)
depends_on = [module.runner_binaries]
}
resource "random_password" "webhook" {
length = 28
}
module "runner_binaries" {
source = "cruxstack/artifact-packager/docker"
version = "1.3.6"
artifact_src_type = "directory"
artifact_dst_directory = coalesce(var.runner_binaries_path, "${path.module}/dist")
artifact_src_path = "/tmp/runner-binaries"
docker_build_context = "${path.module}/assets/runner-binaries"
docker_build_target = "package"
docker_build_args = { RUNNER_VERSION = trimprefix(local.runner_module_version, "v") }
context = module.github_runner_label.context
}
# ---------------------------------------------------------------------- iam ---
data "aws_iam_policy_document" "runner" {
statement {
sid = "AllowAccessToConfigSecret"
effect = "Allow"
actions = [
"secretsmanager:GetSecretValue",
"secretsmanager:DescribeSecret",
]
resources = [
"arn:aws:secretsmanager:${local.aws_region_name}:${local.aws_account_id}:secret:${local.docker_config_sm_secret_name}-*",
]
}
}
resource "aws_iam_policy" "runner" {
name = "${module.github_runner_label.id}-action-runner-custom"
policy = data.aws_iam_policy_document.runner.json
}
# ============================================================== docker-auth ===
resource "aws_secretsmanager_secret" "docker_config" {
name = local.docker_config_sm_secret_name
}
resource "aws_secretsmanager_secret_version" "docker_login" {
secret_id = aws_secretsmanager_secret.docker_config.id
secret_string = jsonencode({
logins = var.docker_logins
})
}