Skip to content
This repository was archived by the owner on Jan 16, 2025. It is now read-only.

Commit 9f7d32d

Browse files
feat: Add option to disable lambda to sync runner binaries (#2314)
- Added variable enable_runner_binaries_syncer to disable syncer - Refactored the internals of the runner moduel. When using the internal modules directoy be-aware of breaking changes, you have to set s3_runner_binaries. - Updated example for pre-build AMI to disable the syncer module. - Narrowed down the bucket permission of the runner to only the relevant distribution. Co-authored-by: GuptaNavdeep1983 <[email protected]>
1 parent eae4ebc commit 9f7d32d

15 files changed

+63
-36
lines changed

Diff for: .editorconfig

+11
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,13 @@
1+
# EditorConfig is awesome: https://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
16
[*]
7+
indent_style = space
8+
indent_size = 2
9+
tab_width = 2
210
end_of_line = lf
11+
charset = utf-8
12+
trim_trailing_whitespace = true
13+
insert_final_newline = true

Diff for: .vscode/extensions.json

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
// Extension identifier format: ${publisher}.${name}. Example: vscode.csharp
66
"editorconfig.editorconfig",
77
"yzhang.markdown-all-in-one",
8-
"sonarsource.sonarlint-vscode",
98
"hashicorp.terraform"
109
]
1110
}

Diff for: README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -309,8 +309,7 @@ The example for [ephemeral runners](./examples/ephemeral) is based on the [defau
309309

310310
### Prebuilt Images
311311

312-
This module also allows you to run agents from a prebuilt AMI to gain faster startup times. You can find more information in [the image README.md](/images/README.md)
313-
312+
This module also allows you to run agents from a prebuilt AMI to gain faster startup times. You can find more information in [the image README.md](/images/README.md). When the GitHub runner is part of the AMI you can disable the binary syncer by setting `enable_runner_binaries_syncer = false`.
314313

315314
## Examples
316315

@@ -407,6 +406,7 @@ In case the setup does not work as intended follow the trace of events:
407406
| <a name="input_enable_job_queued_check"></a> [enable\_job\_queued\_check](#input\_enable\_job\_queued\_check) | Only scale if the job event received by the scale up lambda is is in the state queued. By default enabled for non ephemeral runners and disabled for ephemeral. Set this variable to overwrite the default behavior. | `bool` | `null` | no |
408407
| <a name="input_enable_managed_runner_security_group"></a> [enable\_managed\_runner\_security\_group](#input\_enable\_managed\_runner\_security\_group) | Enabling the default managed security group creation. Unmanaged security groups can be specified via `runner_additional_security_group_ids`. | `bool` | `true` | no |
409408
| <a name="input_enable_organization_runners"></a> [enable\_organization\_runners](#input\_enable\_organization\_runners) | Register runners to organization, instead of repo level | `bool` | `false` | no |
409+
| <a name="input_enable_runner_binaries_syncer"></a> [enable\_runner\_binaries\_syncer](#input\_enable\_runner\_binaries\_syncer) | Option to disable the lambda to sync GitHub runner distribution, usefull when using a pre-build AMI. | `bool` | `true` | no |
410410
| <a name="input_enable_runner_detailed_monitoring"></a> [enable\_runner\_detailed\_monitoring](#input\_enable\_runner\_detailed\_monitoring) | Should detailed monitoring be enabled for the runner. Set this to true if you want to use detailed monitoring. See https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/using-cloudwatch-new.html for details. | `bool` | `false` | no |
411411
| <a name="input_enable_ssm_on_runners"></a> [enable\_ssm\_on\_runners](#input\_enable\_ssm\_on\_runners) | Enable to allow access the runner instances for debugging purposes via SSM. Note that this adds additional permissions to the runner instances. | `bool` | `false` | no |
412412
| <a name="input_enabled_userdata"></a> [enabled\_userdata](#input\_enabled\_userdata) | Should the userdata script be enabled for the runner. Set this to false if you are using your own prebuilt AMI. | `bool` | `true` | no |

Diff for: examples/prebuilt/README.md

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# Action runners deployment with prebuilt image
22

3-
This module shows how to create GitHub action runners using a prebuilt AMI for the runners
3+
This module shows how to create GitHub action runners using a prebuilt AMI for the runners.
4+
5+
- Configured to run with org level runners.
6+
- GitHub runner binary syncer is not deployed.
47

58
## Usages
69

@@ -56,7 +59,7 @@ module "runners" {
5659
...
5760
# set the name of the ami to use
5861
ami_filter = { name = ["github-runner-amzn2-x86_64-2021*"] }
59-
# provide the owner id of
62+
# provide the owner id of
6063
ami_owners = ["<your owner id>"]
6164
6265
enabled_userdata = false

Diff for: examples/prebuilt/main.tf

+5-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ module "runners" {
1515
vpc_id = module.vpc.vpc_id
1616
subnet_ids = module.vpc.private_subnets
1717

18-
prefix = local.environment
18+
prefix = local.environment
19+
enable_organization_runners = false
1920

2021
github_app = {
2122
key_base64 = var.github_app_key_base64
@@ -36,6 +37,9 @@ module "runners" {
3637
ami_filter = { name = [var.ami_name_filter] }
3738
ami_owners = [data.aws_caller_identity.current.account_id]
3839

40+
# disable binary syncer since github agent is already installed in the AMI.
41+
enable_runner_binaries_syncer = false
42+
3943
# enable access to the runners via SSM
4044
enable_ssm_on_runners = true
4145

Diff for: examples/prebuilt/outputs.tf

-6
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,3 @@
1-
output "runners" {
2-
value = {
3-
lambda_syncer_name = module.runners.binaries_syncer.lambda.function_name
4-
}
5-
}
6-
71
output "webhook_endpoint" {
82
value = module.runners.webhook.endpoint
93
}

Diff for: images/linux-amzn2/github_agent.linux.pkr.hcl

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ packer {
1010
variable "runner_version" {
1111
description = "The version (no v prefix) of the runner software to install https://github.com/actions/runner/releases"
1212
type = string
13-
default = "2.286.1"
13+
default = "2.295.0"
1414
}
1515

1616
variable "region" {

Diff for: main.tf

+8-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ locals {
33
"ghr:environment" = var.prefix
44
})
55

6-
s3_action_runner_url = "s3://${module.runner_binaries.bucket.id}/${module.runner_binaries.runner_distribution_object_key}"
76
github_app_parameters = {
87
id = module.ssm.parameters.github_app_id
98
key_base64 = module.ssm.parameters.github_app_key_base64
@@ -134,8 +133,11 @@ module "runners" {
134133
prefix = var.prefix
135134
tags = local.tags
136135

137-
s3_bucket_runner_binaries = module.runner_binaries.bucket
138-
s3_location_runner_binaries = local.s3_action_runner_url
136+
s3_runner_binaries = var.enable_runner_binaries_syncer ? {
137+
arn = module.runner_binaries[0].bucket.arn
138+
id = module.runner_binaries[0].bucket.id
139+
key = module.runner_binaries[0].runner_distribution_object_key
140+
} : null
139141

140142
runner_os = var.runner_os
141143
instance_types = var.instance_types
@@ -169,6 +171,7 @@ module "runners" {
169171
runner_additional_security_group_ids = var.runner_additional_security_group_ids
170172
metadata_options = var.runner_metadata_options
171173

174+
enable_runner_binaries_syncer = var.enable_runner_binaries_syncer
172175
lambda_s3_bucket = var.lambda_s3_bucket
173176
runners_lambda_s3_key = var.runners_lambda_s3_key
174177
runners_lambda_s3_object_version = var.runners_lambda_s3_object_version
@@ -218,6 +221,8 @@ module "runners" {
218221
}
219222

220223
module "runner_binaries" {
224+
count = var.enable_runner_binaries_syncer ? 1 : 0
225+
221226
source = "./modules/runner-binaries-syncer"
222227

223228
aws_region = var.aws_region

Diff for: modules/runners/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ yarn run dist
127127
| <a name="input_enable_job_queued_check"></a> [enable\_job\_queued\_check](#input\_enable\_job\_queued\_check) | Only scale if the job event received by the scale up lambda is is in the state queued. By default enabled for non ephemeral runners and disabled for ephemeral. Set this variable to overwrite the default behavior. | `bool` | `null` | no |
128128
| <a name="input_enable_managed_runner_security_group"></a> [enable\_managed\_runner\_security\_group](#input\_enable\_managed\_runner\_security\_group) | Enabling the default managed security group creation. Unmanaged security groups can be specified via `runner_additional_security_group_ids`. | `bool` | `true` | no |
129129
| <a name="input_enable_organization_runners"></a> [enable\_organization\_runners](#input\_enable\_organization\_runners) | n/a | `bool` | n/a | yes |
130+
| <a name="input_enable_runner_binaries_syncer"></a> [enable\_runner\_binaries\_syncer](#input\_enable\_runner\_binaries\_syncer) | Option to disable the lambda to sync GitHub runner distribution, usefull when using a pre-build AMI. | `bool` | `true` | no |
130131
| <a name="input_enable_runner_detailed_monitoring"></a> [enable\_runner\_detailed\_monitoring](#input\_enable\_runner\_detailed\_monitoring) | Enable detailed monitoring for runners | `bool` | `false` | no |
131132
| <a name="input_enable_ssm_on_runners"></a> [enable\_ssm\_on\_runners](#input\_enable\_ssm\_on\_runners) | Enable to allow access to the runner instances for debugging purposes via SSM. Note that this adds additional permissions to the runner instances. | `bool` | n/a | yes |
132133
| <a name="input_enabled_userdata"></a> [enabled\_userdata](#input\_enabled\_userdata) | Should the userdata script be enabled for the runner. Set this to false if you are using your own prebuilt AMI | `bool` | `true` | no |
@@ -180,8 +181,7 @@ yarn run dist
180181
| <a name="input_runners_lambda_s3_key"></a> [runners\_lambda\_s3\_key](#input\_runners\_lambda\_s3\_key) | S3 key for runners lambda function. Required if using S3 bucket to specify lambdas. | `any` | `null` | no |
181182
| <a name="input_runners_lambda_s3_object_version"></a> [runners\_lambda\_s3\_object\_version](#input\_runners\_lambda\_s3\_object\_version) | S3 object version for runners lambda function. Useful if S3 versioning is enabled on source bucket. | `any` | `null` | no |
182183
| <a name="input_runners_maximum_count"></a> [runners\_maximum\_count](#input\_runners\_maximum\_count) | The maximum number of runners that will be created. | `number` | `3` | no |
183-
| <a name="input_s3_bucket_runner_binaries"></a> [s3\_bucket\_runner\_binaries](#input\_s3\_bucket\_runner\_binaries) | n/a | <pre>object({<br> arn = string<br> })</pre> | n/a | yes |
184-
| <a name="input_s3_location_runner_binaries"></a> [s3\_location\_runner\_binaries](#input\_s3\_location\_runner\_binaries) | S3 location of runner distribution. | `string` | n/a | yes |
184+
| <a name="input_s3_runner_binaries"></a> [s3\_runner\_binaries](#input\_s3\_runner\_binaries) | Bucket details for cached GitHub binary. | <pre>object({<br> arn = string<br> id = string<br> key = string<br> })</pre> | n/a | yes |
185185
| <a name="input_scale_down_schedule_expression"></a> [scale\_down\_schedule\_expression](#input\_scale\_down\_schedule\_expression) | Scheduler expression to check every x for scale down. | `string` | `"cron(*/5 * * * ? *)"` | no |
186186
| <a name="input_scale_up_reserved_concurrent_executions"></a> [scale\_up\_reserved\_concurrent\_executions](#input\_scale\_up\_reserved\_concurrent\_executions) | Amount of reserved concurrent executions for the scale-up lambda function. A value of 0 disables lambda from being triggered and -1 removes any concurrency limitations. | `number` | `1` | no |
187187
| <a name="input_sqs_build_queue"></a> [sqs\_build\_queue](#input\_sqs\_build\_queue) | SQS queue to consume accepted build events. | <pre>object({<br> arn = string<br> })</pre> | n/a | yes |

Diff for: modules/runners/main.tf

+1-2
Original file line numberDiff line numberDiff line change
@@ -122,11 +122,10 @@ resource "aws_launch_template" "runner" {
122122
)
123123
}
124124

125-
126125
user_data = var.enabled_userdata ? base64encode(templatefile(local.userdata_template, {
127126
pre_install = var.userdata_pre_install
128127
install_runner = templatefile(local.userdata_install_runner[var.runner_os], {
129-
S3_LOCATION_RUNNER_DISTRIBUTION = var.s3_location_runner_binaries
128+
S3_LOCATION_RUNNER_DISTRIBUTION = var.enable_runner_binaries_syncer ? "s3://${var.s3_runner_binaries.id}/${var.s3_runner_binaries.key}" : ""
130129
RUNNER_ARCHITECTURE = var.runner_architecture
131130
})
132131
post_install = var.userdata_post_install

Diff for: modules/runners/policies-runner.tf

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,13 @@ resource "aws_iam_role_policy" "ssm_parameters" {
3333
}
3434

3535
resource "aws_iam_role_policy" "dist_bucket" {
36+
count = var.enable_runner_binaries_syncer ? 1 : 0
37+
3638
name = "distribution-bucket"
3739
role = aws_iam_role.runner.name
3840
policy = templatefile("${path.module}/policies/instance-s3-policy.json",
3941
{
40-
s3_arn = var.s3_bucket_runner_binaries.arn
42+
s3_arn = "${var.s3_runner_binaries.arn}/${var.s3_runner_binaries.key}"
4143
}
4244
)
4345
}

Diff for: modules/runners/policies/instance-s3-policy.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"Sid": "githubActionDist",
66
"Effect": "Allow",
77
"Action": ["s3:GetObject", "s3:GetObjectAcl"],
8-
"Resource": ["${s3_arn}/*"]
8+
"Resource": ["${s3_arn}"]
99
}
1010
]
1111
}

Diff for: modules/runners/variables.tf

+11-7
Original file line numberDiff line numberDiff line change
@@ -46,17 +46,15 @@ variable "prefix" {
4646
default = "github-actions"
4747
}
4848

49-
variable "s3_bucket_runner_binaries" {
49+
variable "s3_runner_binaries" {
50+
description = "Bucket details for cached GitHub binary."
5051
type = object({
5152
arn = string
53+
id = string
54+
key = string
5255
})
5356
}
5457

55-
variable "s3_location_runner_binaries" {
56-
description = "S3 location of runner distribution."
57-
type = string
58-
}
59-
6058
variable "block_device_mappings" {
6159
description = "The EC2 instance block device configuration. Takes the following keys: `device_name`, `delete_on_termination`, `volume_type`, `volume_size`, `encrypted`, `iops`, `throughput`, `kms_key_id`, `snapshot_id`."
6260
type = list(object({
@@ -566,4 +564,10 @@ variable "lambda_architecture" {
566564
condition = contains(["arm64", "x86_64"], var.lambda_architecture)
567565
error_message = "`lambda_architecture` value is not valid, valid values are: `arm64` and `x86_64`."
568566
}
569-
}
567+
}
568+
569+
variable "enable_runner_binaries_syncer" {
570+
description = "Option to disable the lambda to sync GitHub runner distribution, usefull when using a pre-build AMI."
571+
type = bool
572+
default = true
573+
}

Diff for: outputs.tf

+6-6
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@ output "runners" {
1414
}
1515

1616
output "binaries_syncer" {
17-
value = {
18-
lambda = module.runner_binaries.lambda
19-
lambda_role = module.runner_binaries.lambda_role
20-
location = local.s3_action_runner_url
21-
bucket = module.runner_binaries.bucket
22-
}
17+
value = var.enable_runner_binaries_syncer ? {
18+
lambda = module.runner_binaries[0].lambda
19+
lambda_role = module.runner_binaries[0].lambda_role
20+
location = "s3://${module.runner_binaries[0].bucket.id}/module.runner_binaries[0].bucket.key"
21+
bucket = module.runner_binaries[0].bucket
22+
} : null
2323
}
2424

2525
output "webhook" {

Diff for: variables.tf

+7-1
Original file line numberDiff line numberDiff line change
@@ -662,4 +662,10 @@ variable "lambda_architecture" {
662662
condition = contains(["arm64", "x86_64"], var.lambda_architecture)
663663
error_message = "`lambda_architecture` value is not valid, valid values are: `arm64` and `x86_64`."
664664
}
665-
}
665+
}
666+
667+
variable "enable_runner_binaries_syncer" {
668+
description = "Option to disable the lambda to sync GitHub runner distribution, usefull when using a pre-build AMI."
669+
type = bool
670+
default = true
671+
}

0 commit comments

Comments
 (0)