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

Commit 87104e8

Browse files
feat: add AMI house keeping lambda (#3570)
## Description Quite common to build AMI's with packer. Cleaning up old images could be a challenge. This housekeeping lambda can remove old AMI based on a few criteria. The AMI housekeepr is implemented as an extra terraform module with a lambda that runs once a day. The housekeepr can be configured to exclude images based on reference in SSM, and tags. Images. The module is deisabled by default and can used as part of the main module, multi-runner or stand alone. This PR also updates outdated lambda dependencies. And updates terraform workflows to 1.5.6 as main version. ## Tested - [x] default example (with new housekeeper) - [x] multi runner example --------- Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com>
1 parent 76f45dc commit 87104e8

39 files changed

+2599
-950
lines changed

.github/workflows/terraform.yml

+14-15
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,7 @@ on:
44
branches:
55
- main
66
pull_request:
7-
paths-ignore:
8-
- "modules/*/lambdas/**"
7+
paths: ["**/*.tf", "**/*.hcl", ".github/workflows/terraform.yml"]
98

109
permissions:
1110
contents: read
@@ -18,7 +17,7 @@ jobs:
1817
name: Verify module
1918
strategy:
2019
matrix:
21-
terraform: [1.3.2, "latest"]
20+
terraform: [1.5.6, "latest"]
2221
runs-on: ubuntu-latest
2322
container:
2423
image: hashicorp/terraform:${{ matrix.terraform }}
@@ -30,9 +29,10 @@ jobs:
3029
touch lambdas/functions/webhook/webhook.zip
3130
touch lambdas/functions/control-plane/runners.zip
3231
touch lambdas/functions/gh-agent-syncer/runner-binaries-syncer.zip
32+
touch lambdas/functions/ami-housekeeper/ami-housekeeper.zip
3333
- name: terraform init
3434
run: terraform init -get -backend=false -input=false
35-
- if: contains(matrix.terraform, '1.3.')
35+
- if: contains(matrix.terraform, '1.5.')
3636
name: check terraform formatting
3737
run: terraform fmt -recursive -check=true -write=false
3838
- if: contains(matrix.terraform, 'latest') # check formatting for the latest release but avoid failing the build
@@ -41,17 +41,17 @@ jobs:
4141
continue-on-error: true
4242
- name: validate terraform
4343
run: terraform validate
44-
- if: contains(matrix.terraform, '1.3.')
44+
- if: contains(matrix.terraform, '1.5.')
4545
name: Fix for actions/cache on alpine
4646
run: apk add --no-cache tar
4747
continue-on-error: true
48-
- if: contains(matrix.terraform, '1.3.')
48+
- if: contains(matrix.terraform, '1.5.')
4949
uses: actions/[email protected]
5050
name: Cache TFLint plugin dir
5151
with:
5252
path: ~/.tflint.d/plugins
5353
key: tflint-${{ hashFiles('.tflint.hcl') }}
54-
- if: contains(matrix.terraform, '1.3.')
54+
- if: contains(matrix.terraform, '1.5.')
5555
name: Setup TFLint
5656
uses: terraform-linters/setup-tflint@v4
5757
with:
@@ -69,7 +69,7 @@ jobs:
6969
matrix:
7070
terraform: [1.3.2, "latest"]
7171
module:
72-
["download-lambda", "multi-runner", "runner-binaries-syncer", "runners", "setup-iam-permissions", "ssm", "webhook"]
72+
["ami-housekeeper", "download-lambda", "multi-runner", "runner-binaries-syncer", "runners", "setup-iam-permissions", "ssm", "webhook"]
7373
defaults:
7474
run:
7575
working-directory: modules/${{ matrix.module }}
@@ -116,7 +116,7 @@ jobs:
116116
strategy:
117117
fail-fast: false
118118
matrix:
119-
terraform: [1.3.2, "latest"]
119+
terraform: [1.5.6, "latest"]
120120
example:
121121
["default", "ubuntu", "prebuilt", "arm64", "ephemeral", "windows", "multi-runner"]
122122
defaults:
@@ -129,7 +129,7 @@ jobs:
129129
- uses: actions/checkout@8ade135a41bc03ea155e62e844d188df1ea18608
130130
- name: terraform init
131131
run: terraform init -get -backend=false -input=false
132-
- if: contains(matrix.terraform, '1.3.')
132+
- if: contains(matrix.terraform, '1.5.')
133133
name: check terraform formatting
134134
run: terraform fmt -recursive -check=true -write=false
135135
- if: contains(matrix.terraform, 'latest') # check formatting for the latest release but avoid failing the build
@@ -138,25 +138,24 @@ jobs:
138138
continue-on-error: true
139139
- name: validate terraform
140140
run: terraform validate
141-
- if: contains(matrix.terraform, '1.3.')
141+
- if: contains(matrix.terraform, '1.5.')
142142
name: Fix for actions/cache on alpine
143143
run: apk add --no-cache tar
144144
continue-on-error: true
145-
- if: contains(matrix.terraform, '1.3.')
145+
- if: contains(matrix.terraform, '1.5.')
146146
uses: actions/[email protected]
147147
name: Cache TFLint plugin dir
148148
with:
149149
path: ~/.tflint.d/plugins
150150
key: tflint-${{ hashFiles('.tflint.hcl') }}
151-
- if: contains(matrix.terraform, '1.3.')
151+
- if: contains(matrix.terraform, '1.5.')
152152
name: Setup TFLint
153153
uses: terraform-linters/setup-tflint@v3
154154
with:
155155
github_token: ${{ secrets.GITHUB_TOKEN }}
156-
- if: contains(matrix.terraform, '1.3.')
156+
- if: contains(matrix.terraform, '1.5.')
157157
name: Run TFLint
158158
working-directory: ${{ github.workspace }}
159159
run: |
160160
tflint --init -c ${GITHUB_WORKSPACE}/.tflint.hcl --chdir modules/${{ matrix.module }}
161161
tflint -f compact -c ${GITHUB_WORKSPACE}/.tflint.hcl --var-file ${GITHUB_WORKSPACE}/.github/lint/tflint.tfvars --chdir examples/${{ matrix.example }}
162-

.github/workflows/update-docs.yml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ name: Update docs
22
on:
33
push:
44
paths:
5-
- "*.tf"
6-
- "*.md"
5+
- "**/*.tf"
6+
- "**/*.md"
77
- ".github/workflows/update-docs.yml"
88

99
permissions:

.vscode/gh-runners.code-workspace

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44
"name": "✨ root",
55
"path": ".."
66
},
7+
{
8+
"name": "🚀 @lambda/functions/ami-housekeeper",
9+
"path": "../lambdas/functions/ami-housekeeper"
10+
},
711
{
812
"name": "🚀 @lambda/functions/control-plane",
913
"path": "../lambdas/functions/control-plane"

README.md

+9-1
Original file line numberDiff line numberDiff line change
@@ -337,7 +337,7 @@ The example for [ephemeral runners](./examples/ephemeral) is based on the [defau
337337

338338
### Prebuilt Images
339339

340-
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)
340+
This module also allows you to run agents from a prebuilt AMI to gain faster startup times. The module provides several examples to build your own custom AMI. To remove old images, an [AMI housekeeper module](./modules/ami-housekeeper/README.md) can be used. You can find more information in [the image README.md](/images/README.md) for building custom images.
341341

342342
### Experimental - Optional queue to publish GitHub workflow job events
343343

@@ -468,6 +468,7 @@ We welcome any improvement to the standard module to make the default as secure
468468

469469
| Name | Source | Version |
470470
|------|--------|---------|
471+
| <a name="module_ami_housekeeper"></a> [ami\_housekeeper](#module\_ami\_housekeeper) | ./modules/ami-housekeeper | n/a |
471472
| <a name="module_runner_binaries"></a> [runner\_binaries](#module\_runner\_binaries) | ./modules/runner-binaries-syncer | n/a |
472473
| <a name="module_runners"></a> [runners](#module\_runners) | ./modules/runners | n/a |
473474
| <a name="module_ssm"></a> [ssm](#module\_ssm) | ./modules/ssm | n/a |
@@ -491,6 +492,12 @@ We welcome any improvement to the standard module to make the default as secure
491492
| Name | Description | Type | Default | Required |
492493
|------|-------------|------|---------|:--------:|
493494
| <a name="input_ami_filter"></a> [ami\_filter](#input\_ami\_filter) | Map of lists used to create the AMI filter for the action runner AMI. | `map(list(string))` | <pre>{<br> "state": [<br> "available"<br> ]<br>}</pre> | no |
495+
| <a name="input_ami_housekeeper_cleanup_config"></a> [ami\_housekeeper\_cleanup\_config](#input\_ami\_housekeeper\_cleanup\_config) | Configuration for AMI cleanup.<br><br> `amiFilters` - Filters to use when searching for AMIs to cleanup. Default filter for images owned by the account and that are available.<br> `dryRun` - If true, no AMIs will be deregistered. Default false.<br> `launchTemplateNames` - Launch template names to use when searching for AMIs to cleanup. Default no launch templates.<br> `maxItems` - The maximum numer of AMI's tha will be queried for cleanup. Default no maximum.<br> `minimumDaysOld` - Minimum number of days old an AMI must be to be considered for cleanup. Default 30.<br> `ssmParameterNames` - SSM parameter names to use when searching for AMIs to cleanup. This parameter should be set when using SSM to configure the AMI to use. Default no SSM parameters. | <pre>object({<br> amiFilters = optional(list(object({<br> Name = string<br> Values = list(string)<br> })),<br> [{<br> Name : "state",<br> Values : ["available"],<br> },<br> {<br> Name : "image-type",<br> Values : ["machine"],<br> }]<br> )<br> dryRun = optional(bool, false)<br> launchTemplateNames = optional(list(string))<br> maxItems = optional(number)<br> minimumDaysOld = optional(number, 30)<br> ssmParameterNames = optional(list(string))<br> })</pre> | `{}` | no |
496+
| <a name="input_ami_housekeeper_lambda_s3_key"></a> [ami\_housekeeper\_lambda\_s3\_key](#input\_ami\_housekeeper\_lambda\_s3\_key) | S3 key for syncer lambda function. Required if using S3 bucket to specify lambdas. | `string` | `null` | no |
497+
| <a name="input_ami_housekeeper_lambda_s3_object_version"></a> [ami\_housekeeper\_lambda\_s3\_object\_version](#input\_ami\_housekeeper\_lambda\_s3\_object\_version) | S3 object version for syncer lambda function. Useful if S3 versioning is enabled on source bucket. | `string` | `null` | no |
498+
| <a name="input_ami_housekeeper_lambda_schedule_expression"></a> [ami\_housekeeper\_lambda\_schedule\_expression](#input\_ami\_housekeeper\_lambda\_schedule\_expression) | Scheduler expression for action runner binary syncer. | `string` | `"rate(1 day)"` | no |
499+
| <a name="input_ami_housekeeper_lambda_timeout"></a> [ami\_housekeeper\_lambda\_timeout](#input\_ami\_housekeeper\_lambda\_timeout) | Time out of the lambda in seconds. | `number` | `300` | no |
500+
| <a name="input_ami_housekeeper_lambda_zip"></a> [ami\_housekeeper\_lambda\_zip](#input\_ami\_housekeeper\_lambda\_zip) | File location of the lambda zip file. | `string` | `null` | no |
494501
| <a name="input_ami_id_ssm_parameter_name"></a> [ami\_id\_ssm\_parameter\_name](#input\_ami\_id\_ssm\_parameter\_name) | Externally managed SSM parameter (of data type aws:ec2:image) that contains the AMI ID to launch runner instances from. Overrides ami\_filter | `string` | `null` | no |
495502
| <a name="input_ami_kms_key_arn"></a> [ami\_kms\_key\_arn](#input\_ami\_kms\_key\_arn) | Optional CMK Key ARN to be used to launch an instance from a shared encrypted AMI | `string` | `null` | no |
496503
| <a name="input_ami_owners"></a> [ami\_owners](#input\_ami\_owners) | The list of owners used to select the AMI of action runner instances. | `list(string)` | <pre>[<br> "amazon"<br>]</pre> | no |
@@ -501,6 +508,7 @@ We welcome any improvement to the standard module to make the default as secure
501508
| <a name="input_create_service_linked_role_spot"></a> [create\_service\_linked\_role\_spot](#input\_create\_service\_linked\_role\_spot) | (optional) create the service linked role for spot instances that is required by the scale-up lambda. | `bool` | `false` | no |
502509
| <a name="input_delay_webhook_event"></a> [delay\_webhook\_event](#input\_delay\_webhook\_event) | The number of seconds the event accepted by the webhook is invisible on the queue before the scale up lambda will receive the event. | `number` | `30` | no |
503510
| <a name="input_disable_runner_autoupdate"></a> [disable\_runner\_autoupdate](#input\_disable\_runner\_autoupdate) | Disable the auto update of the github runner agent. Be aware there is a grace period of 30 days, see also the [GitHub article](https://github.blog/changelog/2022-02-01-github-actions-self-hosted-runners-can-now-disable-automatic-updates/) | `bool` | `false` | no |
511+
| <a name="input_enable_ami_housekeeper"></a> [enable\_ami\_housekeeper](#input\_enable\_ami\_housekeeper) | Option to disable the lambda to clean up old AMIs. | `bool` | `false` | no |
504512
| <a name="input_enable_cloudwatch_agent"></a> [enable\_cloudwatch\_agent](#input\_enable\_cloudwatch\_agent) | Enables the cloudwatch agent on the ec2 runner instances. The runner uses a default config that can be overridden via `cloudwatch_config`. | `bool` | `true` | no |
505513
| <a name="input_enable_ephemeral_runners"></a> [enable\_ephemeral\_runners](#input\_enable\_ephemeral\_runners) | Enable ephemeral runners, runners will only be used once. | `bool` | `false` | no |
506514
| <a name="input_enable_event_rule_binaries_syncer"></a> [enable\_event\_rule\_binaries\_syncer](#input\_enable\_event\_rule\_binaries\_syncer) | Option to disable EventBridge Lambda trigger for the binary syncer, useful to stop automatic updates of binary distribution. | `bool` | `true` | no |

examples/default/main.tf

+13
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,19 @@ module "runners" {
9696

9797
# Enable debug logging for the lambda functions
9898
# log_level = "debug"
99+
100+
enable_ami_housekeeper = true
101+
ami_housekeeper_cleanup_config = {
102+
ssmParameterNames = ["*/ami-id"]
103+
minimumDaysOld = 10
104+
amiFilters = [
105+
{
106+
Name = "name"
107+
Values = ["*al2023*"]
108+
}
109+
]
110+
}
111+
99112
}
100113

101114
module "webhook_github_app" {

images/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ These images share the same scripting as used in the user-data mechanism in `/mo
66

77
The examples in `linux-al2023` and `windows-core-2019` also upload a `start-runner` script that uses the exact same startup process as used in the user-data mechanism. This means that the image created here does not need any extra scripts injected or changes to boot up and connect to GH.
88

9+
To remove old images the [AMI house keeper module](./modules/ami-housekeeper/README.md) can be used.
10+
911
## Building your own
1012

1113
To build these images you first need to install packer.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import type { Config } from 'jest';
2+
3+
import defaultConfig from '../../jest.base.config';
4+
5+
const config: Config = {
6+
...defaultConfig,
7+
coverageThreshold: {
8+
global: {
9+
statements: 100,
10+
branches: 100,
11+
functions: 100,
12+
lines: 100,
13+
},
14+
},
15+
};
16+
17+
export default config;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
{
2+
"name": "@terraform-aws-github-runner/ami-housekeeper",
3+
"version": "1.0.0",
4+
"main": "lambda.ts",
5+
"license": "MIT",
6+
"scripts": {
7+
"start": "ts-node-dev src/local.ts",
8+
"test": "NODE_ENV=test jest",
9+
"test:watch": "NODE_ENV=test jest --watch",
10+
"lint": "yarn eslint src",
11+
"watch": "ts-node-dev --respawn --exit-child src/local.ts",
12+
"build": "ncc build src/lambda.ts -o dist",
13+
"dist": "yarn build && cd dist && zip ../ami-housekeeper.zip index.js",
14+
"format": "prettier --write \"**/*.ts\"",
15+
"format-check": "prettier --check \"**/*.ts\"",
16+
"all": "yarn build && yarn format && yarn lint && yarn test"
17+
},
18+
"devDependencies": {
19+
"@trivago/prettier-plugin-sort-imports": "^4.2.1",
20+
"@types/aws-lambda": "^8.10.125",
21+
"@types/jest": "^29.5.6",
22+
"@types/node": "^20.8.9",
23+
"@typescript-eslint/eslint-plugin": "^6.9.0",
24+
"@typescript-eslint/parser": "^6.9.0",
25+
"@vercel/ncc": "^0.38.1",
26+
"aws-sdk-client-mock": "^3.0.0",
27+
"aws-sdk-client-mock-jest": "^3.0.0",
28+
"eslint": "^8.52.0",
29+
"eslint-plugin-prettier": "5.0.1",
30+
"jest": "^29.5",
31+
"jest-mock": "^29.5.0",
32+
"jest-mock-extended": "^3.0.4",
33+
"nock": "^13.3.6",
34+
"prettier": "3.0.3",
35+
"ts-jest": "^29.1.0",
36+
"ts-node": "^10.9.1",
37+
"ts-node-dev": "^2.0.0"
38+
},
39+
"dependencies": {
40+
"@aws-sdk/client-ec2": "^3.436.0",
41+
"@aws-sdk/client-ssm": "^3.436.0",
42+
"@aws-sdk/types": "^3.433.0",
43+
"@terraform-aws-github-runner/aws-powertools-util": "*",
44+
"@terraform-aws-github-runner/aws-ssm-util": "*",
45+
"cron-parser": "^4.8.1",
46+
"typescript": "^5.0.4"
47+
}
48+
}

0 commit comments

Comments
 (0)