Skip to content

Commit 2af3700

Browse files
committed
refactor: move spot requests tag into lambda
1 parent 272fce0 commit 2af3700

File tree

2 files changed

+46
-30
lines changed

2 files changed

+46
-30
lines changed

lambdas/functions/control-plane/src/aws/runners.ts

Lines changed: 46 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
Tag,
1010
TerminateInstancesCommand,
1111
_InstanceType,
12+
DescribeSpotInstanceRequestsCommand,
1213
} from '@aws-sdk/client-ec2';
1314
import { createChildLogger } from '@aws-github-runner/aws-powertools-util';
1415
import { getTracedAWSV3Client, tracer } from '@aws-github-runner/aws-powertools-util';
@@ -131,6 +132,33 @@ function generateFleetOverrides(
131132
return result;
132133
}
133134

135+
async function tagSpotInstanceRequests(ec2Client: EC2Client, fleet: CreateFleetResult, tags: Tag[]) {
136+
const instanceIds = fleet.Instances?.flatMap((i) => i.InstanceIds ?? []) ?? [];
137+
138+
if (!instanceIds.length) {
139+
return;
140+
}
141+
142+
const describeReq = await ec2Client.send(
143+
new DescribeSpotInstanceRequestsCommand({
144+
Filters: [{ Name: 'instance-id', Values: instanceIds }],
145+
}),
146+
);
147+
148+
const spotInstanceRequestIds = describeReq.SpotInstanceRequests?.map((req) => req.SpotInstanceRequestId).filter(
149+
Boolean,
150+
) as string[];
151+
152+
if (spotInstanceRequestIds.length > 0) {
153+
await ec2Client.send(
154+
new CreateTagsCommand({
155+
Resources: spotInstanceRequestIds,
156+
Tags: tags,
157+
}),
158+
);
159+
}
160+
}
161+
134162
export async function createRunner(runnerParameters: Runners.RunnerInputParameters): Promise<string[]> {
135163
logger.debug('Runner configuration.', {
136164
runner: {
@@ -140,10 +168,26 @@ export async function createRunner(runnerParameters: Runners.RunnerInputParamete
140168
},
141169
});
142170

171+
const tags = [
172+
{ Key: 'ghr:Application', Value: 'github-action-runner' },
173+
{ Key: 'ghr:created_by', Value: runnerParameters.numberOfRunners === 1 ? 'scale-up-lambda' : 'pool-lambda' },
174+
{ Key: 'ghr:Type', Value: runnerParameters.runnerType },
175+
{ Key: 'ghr:Owner', Value: runnerParameters.runnerOwner },
176+
];
177+
178+
if (runnerParameters.tracingEnabled) {
179+
const traceId = tracer.getRootXrayTraceId();
180+
tags.push({ Key: 'ghr:trace_id', Value: traceId! });
181+
}
182+
143183
const ec2Client = getTracedAWSV3Client(new EC2Client({ region: process.env.AWS_REGION }));
144184
const amiIdOverride = await getAmiIdOverride(runnerParameters);
145185

146-
const fleet: CreateFleetResult = await createInstances(runnerParameters, amiIdOverride, ec2Client);
186+
const fleet: CreateFleetResult = await createInstances(runnerParameters, amiIdOverride, ec2Client, tags);
187+
188+
if (runnerParameters.ec2instanceCriteria.targetCapacityType === 'spot') {
189+
await tagSpotInstanceRequests(ec2Client, fleet, tags);
190+
}
147191

148192
const instances: string[] = await processFleetResult(fleet, runnerParameters);
149193

@@ -231,19 +275,8 @@ async function createInstances(
231275
runnerParameters: Runners.RunnerInputParameters,
232276
amiIdOverride: string | undefined,
233277
ec2Client: EC2Client,
278+
tags: Tag[],
234279
) {
235-
const tags = [
236-
{ Key: 'ghr:Application', Value: 'github-action-runner' },
237-
{ Key: 'ghr:created_by', Value: runnerParameters.numberOfRunners === 1 ? 'scale-up-lambda' : 'pool-lambda' },
238-
{ Key: 'ghr:Type', Value: runnerParameters.runnerType },
239-
{ Key: 'ghr:Owner', Value: runnerParameters.runnerOwner },
240-
];
241-
242-
if (runnerParameters.tracingEnabled) {
243-
const traceId = tracer.getRootXrayTraceId();
244-
tags.push({ Key: 'ghr:trace_id', Value: traceId! });
245-
}
246-
247280
let fleet: CreateFleetResult;
248281
try {
249282
// see for spec https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateFleet.html

modules/runners/main.tf

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -206,23 +206,6 @@ resource "aws_launch_template" "runner" {
206206
)
207207
}
208208

209-
dynamic "tag_specifications" {
210-
for_each = var.instance_target_capacity_type == "spot" && var.enable_on_demand_failover_for_errors == null ? [1] : [] # Include the block only if the value is "spot" and on_demand_failover_for_errors is not enabled
211-
content {
212-
resource_type = "spot-instances-request"
213-
tags = merge(
214-
local.tags,
215-
{
216-
"Name" = format("%s", local.name_runner)
217-
},
218-
{
219-
"ghr:runner_name_prefix" = var.runner_name_prefix
220-
},
221-
var.runner_ec2_tags
222-
)
223-
}
224-
}
225-
226209
tag_specifications {
227210
resource_type = "network-interface"
228211
tags = merge(

0 commit comments

Comments
 (0)