11import middy from '@middy/core' ;
2+ import type { MiddlewareObj } from '@middy/core' ;
23import { logger , setContext } from '@aws-github-runner/aws-powertools-util' ;
34import { captureLambdaHandler , tracer } from '@aws-github-runner/aws-powertools-util' ;
45import { Context , type SQSBatchItemFailure , type SQSBatchResponse , SQSEvent } from 'aws-lambda' ;
@@ -10,7 +11,13 @@ import { type ActionRequestMessage, type ActionRequestMessageSQS, scaleUp } from
1011import { SSMCleanupOptions , cleanSSMTokens } from './scale-runners/ssm-housekeeper' ;
1112import { checkAndRetryJob } from './scale-runners/job-retry' ;
1213
13- async function scaleUpHandlerFn ( event : SQSEvent , context : Context ) : Promise < SQSBatchResponse > {
14+ // Type assertion helper for AWS PowerTools middleware compatibility with Middy v7
15+ // PowerTools returns MiddlewareLikeObj which is runtime-compatible but has stricter types
16+ const asMiddleware = < TEvent , TResult > (
17+ middleware : ReturnType < typeof captureLambdaHandler > ,
18+ ) : MiddlewareObj < TEvent , TResult , Error , Context > => middleware as MiddlewareObj < TEvent , TResult , Error , Context > ;
19+
20+ async function handleScaleUp ( event : SQSEvent , context : Context ) : Promise < SQSBatchResponse > {
1421 setContext ( context , 'lambda.ts' ) ;
1522 logger . logEventIfEnabled ( event ) ;
1623
@@ -64,7 +71,7 @@ async function scaleUpHandlerFn(event: SQSEvent, context: Context): Promise<SQSB
6471 }
6572}
6673
67- async function scaleDownHandlerFn ( event : unknown , context : Context ) : Promise < void > {
74+ async function handleScaleDown ( event : unknown , context : Context ) : Promise < void > {
6875 setContext ( context , 'lambda.ts' ) ;
6976 logger . logEventIfEnabled ( event ) ;
7077
@@ -75,7 +82,7 @@ async function scaleDownHandlerFn(event: unknown, context: Context): Promise<voi
7582 }
7683}
7784
78- async function adjustPoolFn ( event : PoolEvent , context : Context ) : Promise < void > {
85+ async function handleAdjustPool ( event : PoolEvent , context : Context ) : Promise < void > {
7986 setContext ( context , 'lambda.ts' ) ;
8087 logger . logEventIfEnabled ( event ) ;
8188
@@ -87,7 +94,7 @@ async function adjustPoolFn(event: PoolEvent, context: Context): Promise<void> {
8794 return Promise . resolve ( ) ;
8895}
8996
90- async function ssmHousekeeperFn ( event : unknown , context : Context ) : Promise < void > {
97+ async function handleSSMHousekeeper ( event : unknown , context : Context ) : Promise < void > {
9198 setContext ( context , 'lambda.ts' ) ;
9299 logger . logEventIfEnabled ( event ) ;
93100 const config = JSON . parse ( process . env . SSM_CLEANUP_CONFIG ) as SSMCleanupOptions ;
@@ -99,16 +106,20 @@ async function ssmHousekeeperFn(event: unknown, context: Context): Promise<void>
99106 }
100107}
101108
102- // Export wrapped handlers for middy v7
103- const handler = captureLambdaHandler ( tracer ) ;
104- // eslint-disable-next-line @typescript-eslint/no-explicit-any
105- export const scaleUpHandler = handler ? middy ( scaleUpHandlerFn ) . use ( handler as any ) : scaleUpHandlerFn ;
106- // eslint-disable-next-line @typescript-eslint/no-explicit-any
107- export const scaleDownHandler = handler ? middy ( scaleDownHandlerFn ) . use ( handler as any ) : scaleDownHandlerFn ;
108- // eslint-disable-next-line @typescript-eslint/no-explicit-any
109- export const adjustPool = handler ? middy ( adjustPoolFn ) . use ( handler as any ) : adjustPoolFn ;
110- // eslint-disable-next-line @typescript-eslint/no-explicit-any
111- export const ssmHousekeeper = handler ? middy ( ssmHousekeeperFn ) . use ( handler as any ) : ssmHousekeeperFn ;
109+ // Export handlers with AWS PowerTools middleware
110+ const powertoolsMiddleware = captureLambdaHandler ( tracer ) ;
111+ export const scaleUpHandler = powertoolsMiddleware
112+ ? middy ( handleScaleUp ) . use ( asMiddleware < SQSEvent , SQSBatchResponse > ( powertoolsMiddleware ) )
113+ : handleScaleUp ;
114+ export const scaleDownHandler = powertoolsMiddleware
115+ ? middy ( handleScaleDown ) . use ( asMiddleware < unknown , void > ( powertoolsMiddleware ) )
116+ : handleScaleDown ;
117+ export const adjustPool = powertoolsMiddleware
118+ ? middy ( handleAdjustPool ) . use ( asMiddleware < PoolEvent , void > ( powertoolsMiddleware ) )
119+ : handleAdjustPool ;
120+ export const ssmHousekeeper = powertoolsMiddleware
121+ ? middy ( handleSSMHousekeeper ) . use ( asMiddleware < unknown , void > ( powertoolsMiddleware ) )
122+ : handleSSMHousekeeper ;
112123
113124export async function jobRetryCheck ( event : SQSEvent , context : Context ) : Promise < void > {
114125 setContext ( context , 'lambda.ts' ) ;
0 commit comments