Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option ephemeral runners #1374

Merged
merged 31 commits into from
Dec 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
0b5d97f
add option ephemeral runners
npalm Dec 10, 2021
585d5ed
fix tests
npalm Nov 3, 2021
89e3a37
Add retry mechanisme for scaling errors
npalm Nov 30, 2021
d70e338
Add retry mechanisme for scaling errors
npalm Dec 10, 2021
4c982e5
Add tests for lamda handler
npalm Dec 1, 2021
e9f360e
Add basic test for ephemeral case
npalm Dec 1, 2021
f26673a
Add basic test for scale down in lambda wrapper
npalm Dec 1, 2021
ad16d80
Ensure check_runs are ignored for ephemeral runners
npalm Dec 10, 2021
5fb7237
limit termination to only the instance itself
npalm Dec 2, 2021
b6b4fa3
fix: add logging context to runner lambda (#1399)
npalm Dec 10, 2021
14ac0db
feat: Add hooks for prebuilt images (AMI), including amazon linux pac…
npalm Dec 22, 2021
4687395
add option ephemeral runners
npalm Dec 10, 2021
0b6a8e7
Add retry mechanisme for scaling errors
npalm Dec 10, 2021
99399a1
add dead letter queue, and refactor
npalm Dec 22, 2021
3f46be8
cleanup
npalm Dec 10, 2021
4eb82ac
cleanup
npalm Dec 10, 2021
bfff210
sync develop
npalm Dec 10, 2021
f0e9880
review fix
npalm Dec 10, 2021
5cda294
review fix
npalm Dec 10, 2021
4acb137
review vfix
npalm Dec 10, 2021
7b074c8
review vfix
npalm Dec 10, 2021
3b8f28a
fix review
npalm Dec 10, 2021
2dd5848
process review comments
npalm Dec 13, 2021
6c66965
process review comments
npalm Dec 13, 2021
abeac1a
review comment
npalm Dec 13, 2021
17dba00
process review comments
npalm Dec 14, 2021
ec7af4c
Update examples/ephemeral/README.md
npalm Dec 14, 2021
e08bd36
Process review comments
npalm Dec 14, 2021
7c9f512
Add docs
npalm Dec 22, 2021
49f15a0
review comments
npalm Dec 22, 2021
3269019
update docs
npalm Dec 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .ci/build-yarn.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

# Build all the lambda's, output on the default place (inside the lambda module)

lambdaSrcDirs=("modules/runner-binaries-syncer/lambdas/runner-binaries-syncer" "modules/runners/lambdas/runners" "modules/webhook/lambdas/webhook")
repoRoot=$(dirname $(dirname $(realpath ${BASH_SOURCE[0]})))

for lambdaDir in ${lambdaSrcDirs[@]}; do
cd "$repoRoot/${lambdaDir}"
yarn && yarn run dist
done
47 changes: 41 additions & 6 deletions README.md

Large diffs are not rendered by default.

12 changes: 9 additions & 3 deletions examples/default/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ module "runners" {
webhook_secret = random_id.random.hex
}

# Grab zip files via lambda_download
webhook_lambda_zip = "lambdas-download/webhook.zip"
runner_binaries_syncer_lambda_zip = "lambdas-download/runner-binaries-syncer.zip"
runners_lambda_zip = "lambdas-download/runners.zip"
enable_organization_runners = false
runner_extra_labels = "default,example"

enable_organization_runners = false
runner_extra_labels = "default,example"

# enable access to the runners via SSM
enable_ssm_on_runners = true
Expand All @@ -61,7 +63,11 @@ module "runners" {
instance_types = ["m5.large", "c5.large"]

# override delay of events in seconds
delay_webhook_event = 5
delay_webhook_event = 5
runners_maximum_count = 1

# set up a fifo queue to remain order
fifo_build_queue = true

# override scaling down
scale_down_schedule_expression = "cron(* * * * ? *)"
Expand Down
57 changes: 57 additions & 0 deletions examples/ephemeral/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 30 additions & 0 deletions examples/ephemeral/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Action runners deployment ephemeral example

ScottGuymer marked this conversation as resolved.
Show resolved Hide resolved
This example is based on the default setup, but shows how runners can be used with the ephemeral flag enabled. Once enabled, ephemeral runners will be used for one job only. Each job requires a fresh instance. This feature should be used in combination with the `workflow_job` event. See GitHub webhook endpoint configuration(link needed here). It is also suggested to use a pre-build AMI to minimize runner launch times.
## Usages

Steps for the full setup, such as creating a GitHub app can be found in the root module's [README](../../README.md). First download the Lambda releases from GitHub. Alternatively you can build the lambdas locally with Node or Docker, there is a simple build script in `<root>/.ci/build.sh`. In the `main.tf` you can simply remove the location of the lambda zip files, the default location will work in this case.

> Ensure you have set the version in `lambdas-download/main.tf` for running the example. The version needs to be set to a GitHub release version, see https://github.com/philips-labs/terraform-aws-github-runner/releases

```bash
cd lambdas-download
terraform init
terraform apply
cd ..
```

Before running Terraform, ensure the GitHub app is configured. See the [configuration details](../../README.md#usages) for more details.

```bash
terraform init
terraform apply
```

You can receive the webhook details by running:

```bash
terraform output -raw webhook_secret
```

Be-aware some shells will print some end of line character `%`.
25 changes: 25 additions & 0 deletions examples/ephemeral/lambdas-download/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
locals {
version = "<REPLACE_BY_GITHUB_RELEASE_VERSION>"
}

module "lambdas" {
source = "../../../modules/download-lambda"
lambdas = [
{
name = "webhook"
tag = local.version
},
{
name = "runners"
tag = local.version
},
{
name = "runner-binaries-syncer"
tag = local.version
}
]
}

output "files" {
value = module.lambdas.files
}
71 changes: 71 additions & 0 deletions examples/ephemeral/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
locals {
environment = "ephemeraal"
aws_region = "eu-west-1"
}

resource "random_id" "random" {
byte_length = 20
}

data "aws_caller_identity" "current" {}

module "runners" {
source = "../../"
create_service_linked_role_spot = true
aws_region = local.aws_region
vpc_id = module.vpc.vpc_id
subnet_ids = module.vpc.private_subnets

environment = local.environment
tags = {
Project = "ProjectX"
}

github_app = {
key_base64 = var.github_app_key_base64
id = var.github_app_id
webhook_secret = random_id.random.hex
}

# Grab the lambda packages from local directory. Must run /.ci/build.sh first
webhook_lambda_zip = "../../lambda_output/webhook.zip"
runner_binaries_syncer_lambda_zip = "../../lambda_output/runner-binaries-syncer.zip"
runners_lambda_zip = "../../lambda_output/runners.zip"

enable_organization_runners = true
runner_extra_labels = "default,example"

# enable access to the runners via SSM
enable_ssm_on_runners = true

# Let the module manage the service linked role
# create_service_linked_role_spot = true

instance_types = ["m5.large", "c5.large"]

# override delay of events in seconds
delay_webhook_event = 0

# Ensure you set the number not too low, each build require a new instance
runners_maximum_count = 20

# override scaling down
scale_down_schedule_expression = "cron(* * * * ? *)"

enable_ephemeral_runners = true

# configure your pre-built AMI
# enabled_userdata = false
# ami_filter = { name = ["github-runner-amzn2-x86_64-2021*"] }
# ami_owners = [data.aws_caller_identity.current.account_id]

# Enable logging
# log_level = "debug"

# Setup a dead letter queue, by default scale up lambda will kepp retrying to process event in case of scaling error.
# redrive_policy_build_queue = {
# enabled = true
# maxReceiveCount = 50 # 50 retries every 30 seconds => 25 minutes
# deadLetterTargetArn = null
# }
}
15 changes: 15 additions & 0 deletions examples/ephemeral/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
output "runners" {
value = {
lambda_syncer_name = module.runners.binaries_syncer.lambda.function_name
}
}

output "webhook_endpoint" {
value = module.runners.webhook.endpoint
}

output "webhook_secret" {
sensitive = true
value = random_id.random.hex
}

3 changes: 3 additions & 0 deletions examples/ephemeral/providers.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
provider "aws" {
region = local.aws_region
}
5 changes: 5 additions & 0 deletions examples/ephemeral/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

variable "github_app_key_base64" {}

variable "github_app_id" {}

15 changes: 15 additions & 0 deletions examples/ephemeral/versions.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 3.27"
}
local = {
source = "hashicorp/local"
}
random = {
source = "hashicorp/random"
}
}
required_version = ">= 0.14"
}
7 changes: 7 additions & 0 deletions examples/ephemeral/vpc.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module "vpc" {
source = "git::https://github.com/philips-software/terraform-aws-vpc.git?ref=2.2.0"

environment = local.environment
aws_region = local.aws_region
create_private_hosted_zone = false
}
21 changes: 17 additions & 4 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,24 @@ resource "random_string" "random" {
}

resource "aws_sqs_queue" "queued_builds" {
name = "${var.environment}-queued-builds.fifo"
name = "${var.environment}-queued-builds${var.fifo_build_queue ? ".fifo" : ""}"
delay_seconds = var.delay_webhook_event
visibility_timeout_seconds = var.runners_scale_up_lambda_timeout
message_retention_seconds = var.job_queue_retention_in_seconds
fifo_queue = true
receive_wait_time_seconds = 10
content_based_deduplication = true
fifo_queue = var.fifo_build_queue
receive_wait_time_seconds = 0
content_based_deduplication = var.fifo_build_queue
redrive_policy = var.redrive_build_queue.enabled ? jsonencode({
deadLetterTargetArn = aws_sqs_queue.queued_builds_dlq[0].arn,
maxReceiveCount = var.redrive_build_queue.maxReceiveCount
}) : null

tags = var.tags
}

resource "aws_sqs_queue" "queued_builds_dlq" {
count = var.redrive_build_queue.enabled ? 1 : 0
name = "${var.environment}-queued-builds_dead_letter"

tags = var.tags
}
Expand All @@ -48,6 +59,7 @@ module "webhook" {
kms_key_arn = var.kms_key_arn

sqs_build_queue = aws_sqs_queue.queued_builds
sqs_build_queue_fifo = var.fifo_build_queue
github_app_webhook_secret_arn = module.ssm.parameters.github_app_webhook_secret.arn

lambda_s3_bucket = var.lambda_s3_bucket
Expand Down Expand Up @@ -92,6 +104,7 @@ module "runners" {
sqs_build_queue = aws_sqs_queue.queued_builds
github_app_parameters = local.github_app_parameters
enable_organization_runners = var.enable_organization_runners
enable_ephemeral_runners = var.enable_ephemeral_runners
scale_down_schedule_expression = var.scale_down_schedule_expression
minimum_running_time_in_minutes = var.minimum_running_time_in_minutes
runner_boot_time_in_minutes = var.runner_boot_time_in_minutes
Expand Down
Loading