Skip to content

Commit d860feb

Browse files
iainlanenpalmgithub-actions[bot]
authored
fix(ami-housekeeper): don't delete referenced AMIs in default config (#4623)
In 472cc5f the default config was migrated to use SSM for AMI lookup. A parameter is created which stores a reference to the AMI. By default, this parameter is called `${var.ssm_paths.root}/${var.ssm_paths.config}/ami_id`. The housekeeper is a process that looks for AMIs which can be deleted because they're no longer used. It does this in a couple of ways: 1. Check the launch template for the AMI ID. 2. Check the SSM parameter. 3. Apply a threshold to not delete AMIs that are too new, according to the config. The problem is that we were looking for SSM parameters like this: ```typescript const ssmParams = await ssmClient.send( new DescribeParametersCommand({ ParameterFilters: [ { Key: "Name", Values: ["ami-id"], Option: "Contains", }, ], }), ); ``` i.e. we were looking for parameters which contain the hardcoded string `ami-id`. This is different to the new default of `ami_id`. So we weren't considering the right AMIs to be in use. What would be a better approach would be to reference the values dynamically. This means resolving from the template, and handling the passed-in options, if there are any. We're documenting that we support wildcards, so also support that here too. The default value in the launch template became `resolve:ssm:<id or AMI>`, so we need to make sure to ask EC2 to resolve for us when looking up the template. In that way we get the actual AMI ID rather than the alias. This can be a bit challenging to understand, so the comments are improved. Comprehensive tests are added to try to ensure this all works as expected. Closes: #4571 --------- Co-authored-by: Niek Palm <[email protected]> Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Niek Palm <[email protected]>
1 parent d3b5e27 commit d860feb

File tree

7 files changed

+640
-46
lines changed

7 files changed

+640
-46
lines changed

examples/prebuilt/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,18 @@ This module shows how to create GitHub action runners using a prebuilt AMI for t
77

88
@@ Usages
99

10+
11+
Steps for the full setup, such as creating a GitHub app can be found in the root module's [README](https://github.com/github-aws-runners/terraform-aws-github-runner). 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.
12+
13+
> This example assumes local built lambda's available. Ensure you have built the lambda's. Alternatively you can download the lambda's. The version needs to be set to a GitHub release version, see https://github.com/github-aws-runners/terraform-aws-github-runner/releases
14+
15+
```bash
16+
cd ../lambdas-download
17+
terraform init
18+
terraform apply -var=module_version=<VERSION>
19+
cd -
20+
```
21+
1022
### Packer Image
1123

1224
You will need to build your image. This example deployment uses the image example in `/images/linux-amz2`. You must build this image with packer in your AWS account first. Once you have built this you need to provider your owner ID as a variable
@@ -92,6 +104,8 @@ terraform output webhook_secret
92104
| Name | Description | Type | Default | Required |
93105
|------|-------------|------|---------|:--------:|
94106
| <a name="input_ami_name_filter"></a> [ami\_name\_filter](#input\_ami\_name\_filter) | AMI name filter for the action runner AMI. By default amazon linux 2 is used. | `string` | `"github-runner-al2023-x86_64-*"` | no |
107+
| <a name="input_aws_region"></a> [aws\_region](#input\_aws\_region) | AWS region. | `string` | `"eu-west-1"` | no |
108+
| <a name="input_environment"></a> [environment](#input\_environment) | Environment name, used as prefix. | `string` | `null` | no |
95109
| <a name="input_github_app"></a> [github\_app](#input\_github\_app) | GitHub for API usages. | <pre>object({<br/> id = string<br/> key_base64 = string<br/> })</pre> | n/a | yes |
96110
| <a name="input_runner_os"></a> [runner\_os](#input\_runner\_os) | The EC2 Operating System type to use for action runner instances (linux,windows). | `string` | `"linux"` | no |
97111

examples/prebuilt/main.tf

Lines changed: 46 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
locals {
2-
environment = "prebuilt"
3-
aws_region = "eu-west-1"
2+
environment = var.environment != null ? var.environment : "default"
3+
aws_region = var.aws_region
44
}
55

66
resource "random_id" "random" {
@@ -32,9 +32,12 @@ module "runners" {
3232
webhook_secret = random_id.random.hex
3333
}
3434

35-
webhook_lambda_zip = "../lambdas-download/webhook.zip"
36-
runner_binaries_syncer_lambda_zip = "../lambdas-download/runner-binaries-syncer.zip"
37-
runners_lambda_zip = "../lambdas-download/runners.zip"
35+
# link to downloaded lambda zip files.
36+
# When not explicitly set lambda zip files are grabbed from the module requiring lambda build.
37+
#
38+
# webhook_lambda_zip = "../lambdas-download/webhook.zip"
39+
# runner_binaries_syncer_lambda_zip = "../lambdas-download/runner-binaries-syncer.zip"
40+
# runners_lambda_zip = "../lambdas-download/runners.zip"
3841

3942
runner_extra_labels = ["default", "example"]
4043

@@ -56,6 +59,44 @@ module "runners" {
5659

5760
# override scaling down
5861
scale_down_schedule_expression = "cron(* * * * ? *)"
62+
63+
enable_ami_housekeeper = true
64+
ami_housekeeper_cleanup_config = {
65+
ssmParameterNames = ["*/ami_id"]
66+
minimumDaysOld = 1
67+
dryRun = true
68+
amiFilters = [
69+
{
70+
Name = "name"
71+
Values = ["*al2023*"]
72+
}
73+
]
74+
}
75+
76+
# variable "runners_ssm_housekeeper" {
77+
# description = <<EOF
78+
# Configuration for the SSM housekeeper lambda. This lambda deletes token / JIT config from SSM.
79+
80+
# `schedule_expression`: is used to configure the schedule for the lambda.
81+
# `enabled`: enable or disable the lambda trigger via the EventBridge.
82+
# `lambda_memory_size`: lambda memery size limit.
83+
# `lambda_timeout`: timeout for the lambda in seconds.
84+
# `config`: configuration for the lambda function. Token path will be read by default from the module.
85+
# EOF
86+
# type = object({
87+
# schedule_expression = optional(string, "rate(1 day)")
88+
# enabled = optional(bool, true)
89+
# lambda_memory_size = optional(number, 512)
90+
# lambda_timeout = optional(number, 60)
91+
# config = object({
92+
# tokenPath = optional(string)
93+
# minimumDaysOld = optional(number, 1)
94+
# dryRun = optional(bool, false)
95+
# })
96+
# })
97+
# default = { config = {} }
98+
99+
# log_level = "debug"
59100
}
60101

61102
module "webhook_github_app" {

examples/prebuilt/variables.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,20 @@ variable "github_app" {
77
})
88
}
99

10+
variable "environment" {
11+
description = "Environment name, used as prefix."
12+
13+
type = string
14+
default = null
15+
}
16+
17+
variable "aws_region" {
18+
description = "AWS region."
19+
20+
type = string
21+
default = "eu-west-1"
22+
}
23+
1024
variable "runner_os" {
1125
description = "The EC2 Operating System type to use for action runner instances (linux,windows)."
1226

images/.gitignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Created by https://www.toptal.com/developers/gitignore/api/packer
2+
# Edit at https://www.toptal.com/developers/gitignore?templates=packer
3+
4+
### Packer ###
5+
# Cache objects
6+
packer_cache/
7+
8+
# Crash log
9+
crash.log
10+
11+
# https://www.packer.io/guides/hcl/variables
12+
# Exclude all .pkrvars.hcl files, which are likely to contain sensitive data,
13+
# such as password, private keys, and other secrets. These should not be part of
14+
# version control as they are data points which are potentially sensitive and
15+
# subject to change depending on the environment.
16+
#
17+
*.pkrvars.hcl
18+
19+
# For built boxes
20+
*.box
21+
22+
### Packer Patch ###
23+
# ignore temporary output files
24+
output-*/
25+
26+
# End of https://www.toptal.com/developers/gitignore/api/packer
27+
28+
**/manifest.json

0 commit comments

Comments
 (0)