11import { createChildLogger } from '@aws-github-runner/aws-powertools-util' ;
2- import { getParameter , putParameter } from '@aws-github-runner/aws-ssm-util' ;
32import { Octokit } from '@octokit/rest' ;
43
54import { metricGitHubAppRateLimit } from '../github/rate-limit' ;
5+ import { createRunnerConfigStore } from './runner-config-storage' ;
66import { ActionRequestMessage , CreateGitHubRunnerConfig , EphemeralRunnerConfig , RunnerGroup } from './types' ;
77
88const logger = createChildLogger ( 'github-runner' ) ;
@@ -171,34 +171,28 @@ export async function getRunnerGroupId(
171171 let runnerGroupId : number | undefined = 1 ;
172172 if ( githubRunnerConfig . runnerType === 'Org' && githubRunnerConfig . runnerGroup !== undefined ) {
173173 let runnerGroup : string | undefined ;
174- // check if runner group id is already stored in SSM Parameter Store and
175- // use it if it exists to avoid API call to GitHub
174+ const runnerConfigStore = createRunnerConfigStore ( githubRunnerConfig ) ;
175+ const runnerGroupCacheKey = `runner-group/${ githubRunnerConfig . runnerGroup } ` ;
176+ // check if runner group id is already cached and use it if it exists to
177+ // avoid an API call to GitHub
176178 try {
177- runnerGroup = await getParameter (
178- `${ githubRunnerConfig . ssmConfigPath } /runner-group/${ githubRunnerConfig . runnerGroup } ` ,
179- ) ;
179+ runnerGroup = await runnerConfigStore . getConfigValue ( runnerGroupCacheKey ) ;
180180 } catch ( err ) {
181181 logger . debug ( 'Handling error:' , err as Error ) ;
182182 logger . warn (
183- `SSM Parameter "${ githubRunnerConfig . ssmConfigPath } /runner-group/${ githubRunnerConfig . runnerGroup } "
184- for Runner group ${ githubRunnerConfig . runnerGroup } does not exist` ,
183+ `Runner group cache entry "${ runnerGroupCacheKey } " for Runner group ${ githubRunnerConfig . runnerGroup } does not exist` ,
185184 ) ;
186185 }
187186 if ( runnerGroup === undefined ) {
188187 // get runner group id from GitHub
189188 runnerGroupId = await getRunnerGroupByName ( ghClient , githubRunnerConfig ) ;
190- // store runner group id in SSM
189+ // store runner group id in the configured runner config storage
191190 try {
192- await putParameter (
193- `${ githubRunnerConfig . ssmConfigPath } /runner-group/${ githubRunnerConfig . runnerGroup } ` ,
194- runnerGroupId . toString ( ) ,
195- false ,
196- {
197- tags : githubRunnerConfig . ssmParameterStoreTags ,
198- } ,
199- ) ;
191+ await runnerConfigStore . putConfigValue ( runnerGroupCacheKey , runnerGroupId . toString ( ) , {
192+ tags : githubRunnerConfig . ssmParameterStoreTags ,
193+ } ) ;
200194 } catch ( err ) {
201- logger . debug ( 'Error storing runner group id in SSM Parameter Store ' , err as Error ) ;
195+ logger . debug ( 'Error storing runner group id' , err as Error ) ;
202196 throw err ;
203197 }
204198 } else {
@@ -241,10 +235,10 @@ export async function createStartRunnerConfig(
241235 }
242236}
243237
244- function addDelay ( runnerIds : string [ ] ) {
238+ function addDelay ( runnerIds : string [ ] , enabled = true ) {
245239 const delay = async ( ms : number ) => new Promise ( ( resolve ) => setTimeout ( resolve , ms ) ) ;
246240 const ssmParameterStoreMaxThroughput = 40 ;
247- const isDelay = runnerIds . length >= ssmParameterStoreMaxThroughput ;
241+ const isDelay = enabled && runnerIds . length >= ssmParameterStoreMaxThroughput ;
248242 return { isDelay, delay } ;
249243}
250244
@@ -259,7 +253,8 @@ async function createRegistrationTokenConfig(
259253 ghClient : Octokit ,
260254 options : StartRunnerConfigOptions ,
261255) : Promise < string [ ] > {
262- const { isDelay, delay } = addDelay ( runnerIds ) ;
256+ const runnerConfigStore = createRunnerConfigStore ( githubRunnerConfig ) ;
257+ const { isDelay, delay } = addDelay ( runnerIds , runnerConfigStore . delayWritesForSsmThroughput ) ;
263258 const token = await getGithubRunnerRegistrationToken ( githubRunnerConfig , ghClient ) ;
264259 const runnerServiceConfig = generateRunnerServiceConfig ( githubRunnerConfig , token ) ;
265260
@@ -268,7 +263,7 @@ async function createRegistrationTokenConfig(
268263 } ) ;
269264
270265 for ( const runnerId of runnerIds ) {
271- await putParameter ( ` ${ githubRunnerConfig . ssmTokenPath } / ${ runnerId } ` , runnerServiceConfig . join ( ' ' ) , true , {
266+ await runnerConfigStore . putRunnerConfig ( runnerId , runnerServiceConfig . join ( ' ' ) , {
272267 tags : [ ...( options . getSsmParameterTags ?.( runnerId ) ?? [ ] ) , ...githubRunnerConfig . ssmParameterStoreTags ] ,
273268 } ) ;
274269 if ( isDelay ) {
@@ -293,7 +288,8 @@ async function createJitConfig(
293288 options : StartRunnerConfigOptions ,
294289) : Promise < string [ ] > {
295290 const runnerGroupId = await getRunnerGroupId ( githubRunnerConfig , ghClient ) ;
296- const { isDelay, delay } = addDelay ( runnerIds ) ;
291+ const runnerConfigStore = createRunnerConfigStore ( githubRunnerConfig ) ;
292+ const { isDelay, delay } = addDelay ( runnerIds , runnerConfigStore . delayWritesForSsmThroughput ) ;
297293 const runnerLabels = githubRunnerConfig . runnerLabels . split ( ',' ) ;
298294 const failedRunnerIds : string [ ] = [ ] ;
299295
@@ -331,11 +327,11 @@ async function createJitConfig(
331327 runnerLabels,
332328 } ) ;
333329
334- // store jit config in ssm parameter store
330+ // store jit config in the configured runner config storage
335331 logger . debug ( 'Runner JIT config for ephemeral runner generated.' , {
336332 instance : runnerId ,
337333 } ) ;
338- await putParameter ( ` ${ githubRunnerConfig . ssmTokenPath } / ${ runnerId } ` , runnerConfig . data . encoded_jit_config , true , {
334+ await runnerConfigStore . putRunnerConfig ( runnerId , runnerConfig . data . encoded_jit_config , {
339335 tags : [ ...( options . getSsmParameterTags ?.( runnerId ) ?? [ ] ) , ...githubRunnerConfig . ssmParameterStoreTags ] ,
340336 } ) ;
341337 if ( isDelay ) {
0 commit comments