9
9
Tag ,
10
10
TerminateInstancesCommand ,
11
11
_InstanceType ,
12
+ DescribeSpotInstanceRequestsCommand ,
12
13
} from '@aws-sdk/client-ec2' ;
13
14
import { createChildLogger } from '@aws-github-runner/aws-powertools-util' ;
14
15
import { getTracedAWSV3Client , tracer } from '@aws-github-runner/aws-powertools-util' ;
@@ -131,6 +132,33 @@ function generateFleetOverrides(
131
132
return result ;
132
133
}
133
134
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
+
134
162
export async function createRunner ( runnerParameters : Runners . RunnerInputParameters ) : Promise < string [ ] > {
135
163
logger . debug ( 'Runner configuration.' , {
136
164
runner : {
@@ -140,10 +168,26 @@ export async function createRunner(runnerParameters: Runners.RunnerInputParamete
140
168
} ,
141
169
} ) ;
142
170
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
+
143
183
const ec2Client = getTracedAWSV3Client ( new EC2Client ( { region : process . env . AWS_REGION } ) ) ;
144
184
const amiIdOverride = await getAmiIdOverride ( runnerParameters ) ;
145
185
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
+ }
147
191
148
192
const instances : string [ ] = await processFleetResult ( fleet , runnerParameters ) ;
149
193
@@ -231,19 +275,8 @@ async function createInstances(
231
275
runnerParameters : Runners . RunnerInputParameters ,
232
276
amiIdOverride : string | undefined ,
233
277
ec2Client : EC2Client ,
278
+ tags : Tag [ ] ,
234
279
) {
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
-
247
280
let fleet : CreateFleetResult ;
248
281
try {
249
282
// see for spec https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_CreateFleet.html
0 commit comments