99 Tag ,
1010 TerminateInstancesCommand ,
1111 _InstanceType ,
12+ DescribeSpotInstanceRequestsCommand ,
1213} from '@aws-sdk/client-ec2' ;
1314import { createChildLogger } from '@aws-github-runner/aws-powertools-util' ;
1415import { 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+
134162export 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
0 commit comments