Skip to content

Commit 17792c3

Browse files
committed
fix missing permission, update test example and add more logging
1 parent d78f12a commit 17792c3

File tree

6 files changed

+109
-5
lines changed

6 files changed

+109
-5
lines changed

examples/prebuilt/README.md

Lines changed: 12 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

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

lambdas/functions/ami-housekeeper/src/ami.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,15 @@ async function getAmiInLatestTemplates(options: AmiCleanupOptions): Promise<(str
217217

218218
// Discover launch templates, optionally filtered by specific names. If no
219219
// names provided, this will return all launch templates in the account
220+
logger.debug('Describing launch templates', {
221+
launchTemplateNames: options.launchTemplateNames,
222+
});
220223
const launchTemplates = await ec2Client.send(
221224
new DescribeLaunchTemplatesCommand({
222225
LaunchTemplateNames: options.launchTemplateNames,
223226
}),
224227
);
228+
logger.debug('Found launch templates', { launchTemplates });
225229

226230
// For each template, fetch the default version and resolve any SSM aliases.
227231
const amiIdsNested = await Promise.all(
@@ -236,10 +240,12 @@ async function getAmiInLatestTemplates(options: AmiCleanupOptions): Promise<(str
236240
}),
237241
);
238242

243+
logger.debug('Found launch template versions', { versionsResp });
239244
return (versionsResp.LaunchTemplateVersions ?? []).map((v) => v.LaunchTemplateData?.ImageId);
240245
}),
241246
);
242247

248+
logger.debug('Found AMIs in launch templates', { amiIdsNested });
243249
return amiIdsNested.flat();
244250
}
245251

@@ -283,6 +289,7 @@ async function getAmisReferedInSSM(options: AmiCleanupOptions): Promise<(string
283289

284290
try {
285291
// Discover parameters matching the wildcard patterns
292+
logger.debug('Describing SSM parameter', { filters });
286293
const ssmParameters = await ssmClient.send(new DescribeParametersCommand({ ParameterFilters: filters }));
287294

288295
// Fetch the actual values of discovered parameters
@@ -296,6 +303,7 @@ async function getAmisReferedInSSM(options: AmiCleanupOptions): Promise<(string
296303

297304
// Combine results from both explicit and wildcard parameter resolution
298305
const values = await Promise.all([explicitValuesPromise, wildcardValues]);
306+
logger.debug('Resolved SSM parameter values', { values });
299307
return values.flat();
300308
}
301309

modules/ami-housekeeper/policies/lambda-ami-housekeeper.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
"ec2:DeregisterImage",
1212
"ec2:DeleteSnapshot",
1313
"ssm:DescribeParameters",
14+
"ssm:GetParameters",
1415
"ssm:GetParameter"
1516
],
1617
"Resource": "*"

0 commit comments

Comments
 (0)