Skip to content

Commit 56f7bbf

Browse files
atalmanjeanschmidt
andauthored
Windows AMI: Add possibility to use different owners specified in the filter param (#7004)
fixes: #6688 allow us to use different AMI for testing --------- Co-authored-by: Jean Schmidt <contato@jschmidt.me>
1 parent 2d317b1 commit 56f7bbf

3 files changed

Lines changed: 50 additions & 4 deletions

File tree

.github/scripts/validate_scale_config.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,11 @@
3737
"additionalProperties": False,
3838
"properties": {
3939
"ami_experiment": {"type": "object"},
40-
"ami": {"type": "string"},
40+
"ami": {
41+
"type": "string",
42+
"pattern": "^[A-Za-z0-9 -_. ]+(\|[0-9]+)?$",
43+
"description": "AMI Name|AWS Account (optional).",
44+
},
4145
"disk_size": {"type": "number"},
4246
"instance_type": {"type": "string"},
4347
"is_ephemeral": {"type": "boolean"},

terraform-aws-github-runner/modules/runners/lambdas/runners/src/scale-runners/runners.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,25 @@ describe('findAmiID', () => {
418418
await findAmiID(metrics, 'REGION', 'FILTER2');
419419
expect(mockEC2.describeImages).toBeCalledTimes(3);
420420
});
421+
422+
it('handles filter with separator and custom account ID', async () => {
423+
const result = await findAmiID(metrics, 'REGION', 'my-image|123456789012');
424+
expect(mockEC2.describeImages).toBeCalledTimes(1);
425+
expect(mockEC2.describeImages).toBeCalledWith({
426+
Owners: ['123456789012'],
427+
Filters: [
428+
{
429+
Name: 'name',
430+
Values: ['my-image'],
431+
},
432+
{
433+
Name: 'state',
434+
Values: ['available'],
435+
},
436+
],
437+
});
438+
expect(result).toBe('ami-AGDGADU113');
439+
});
421440
});
422441

423442
describe('tryReuseRunner', () => {

terraform-aws-github-runner/modules/runners/lambdas/runners/src/scale-runners/runners.ts

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,19 +77,42 @@ export function resetRunnersCaches() {
7777

7878
export async function findAmiID(metrics: Metrics, region: string, filter: string, owners = 'amazon'): Promise<string> {
7979
const ec2 = new EC2({ region: region });
80+
// Check if filter contains separator '|' and extract image name and account ID
81+
let imageName = filter;
82+
let actualOwners = owners;
83+
84+
if (filter.includes('|')) {
85+
const parts = filter.split('|');
86+
if (parts.length === 2) {
87+
imageName = parts[0].trim();
88+
const extractedOwner = parts[1].trim();
89+
90+
// Check if the extracted owner is only numbers (AWS account ID format)
91+
if (/^\d+$/.test(extractedOwner)) {
92+
actualOwners = extractedOwner;
93+
} else {
94+
console.error(
95+
`Invalid account ID format: '${extractedOwner}'. Account ID must` +
96+
` contain only numbers. Using default value '${actualOwners}'`,
97+
);
98+
}
99+
}
100+
}
101+
80102
const filters = [
81-
{ Name: 'name', Values: [filter] },
103+
{ Name: 'name', Values: [imageName] },
82104
{ Name: 'state', Values: ['available'] },
83105
];
84-
return redisCached('awsEC2', `findAmiID-${region}-${filter}-${owners}`, 10 * 60, 0.5, () => {
106+
107+
return redisCached('awsEC2', `findAmiID-${region}-${imageName}-${actualOwners}`, 10 * 60, 0.5, () => {
85108
return expBackOff(() => {
86109
return metrics.trackRequestRegion(
87110
region,
88111
metrics.ec2DescribeImagesSuccess,
89112
metrics.ec2DescribeImagesFailure,
90113
() => {
91114
return ec2
92-
.describeImages({ Owners: [owners], Filters: filters })
115+
.describeImages({ Owners: [actualOwners], Filters: filters })
93116
.promise()
94117
.then((data: EC2.DescribeImagesResult) => {
95118
/* istanbul ignore next */

0 commit comments

Comments
 (0)