-
Notifications
You must be signed in to change notification settings - Fork 726
Expand file tree
/
Copy pathlambda.ts
More file actions
61 lines (51 loc) · 2.43 KB
/
lambda.ts
File metadata and controls
61 lines (51 loc) · 2.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import middy from '@middy/core';
import type { MiddlewareObj } from '@middy/core';
import { captureLambdaHandler, logger, metrics, setContext, tracer } from '@aws-github-runner/aws-powertools-util';
import { logMetrics } from '@aws-lambda-powertools/metrics/middleware';
import { Context } from 'aws-lambda';
import { handle as handleTerminationWarning } from './termination-warning';
import { handle as handleTermination } from './termination';
import { BidEvictedDetail, BidEvictedEvent, SpotInterruptionWarning, SpotTerminationDetail } from './types';
import { Config } from './ConfigResolver';
// Type assertion helper for AWS PowerTools middleware compatibility with Middy v7
const asMiddleware = <TEvent, TResult>(
middleware: ReturnType<typeof captureLambdaHandler> | ReturnType<typeof logMetrics>,
): MiddlewareObj<TEvent, TResult, Error, Context> => middleware as MiddlewareObj<TEvent, TResult, Error, Context>;
const config = new Config();
async function handleInterruptionWarning(
event: SpotInterruptionWarning<SpotTerminationDetail>,
context: Context,
): Promise<void> {
setContext(context, 'lambda.ts');
logger.logEventIfEnabled(event);
logger.debug('Configuration of the lambda', { config });
try {
await handleTerminationWarning(event, config);
} catch (e) {
logger.error(`${(e as Error).message}`, { error: e as Error });
}
}
async function handleBidEvicted(event: BidEvictedEvent<BidEvictedDetail>, context: Context): Promise<void> {
setContext(context, 'lambda.ts');
logger.logEventIfEnabled(event);
logger.debug('Configuration of the lambda', { config });
try {
await handleTermination(event, config);
} catch (e) {
logger.error(`${(e as Error).message}`, { error: e as Error });
}
}
// Export handlers with AWS PowerTools middleware
const tracingMiddleware = captureLambdaHandler(tracer);
const metricsMiddleware = logMetrics(metrics);
const interruptionWarningHandler = middy(handleInterruptionWarning);
if (tracingMiddleware) {
logger.debug('Adding captureLambdaHandler middleware');
interruptionWarningHandler.use(asMiddleware<SpotInterruptionWarning<SpotTerminationDetail>, void>(tracingMiddleware));
}
if (metricsMiddleware) {
logger.debug('Adding logMetrics middleware');
interruptionWarningHandler.use(asMiddleware<SpotInterruptionWarning<SpotTerminationDetail>, void>(metricsMiddleware));
}
export const interruptionWarning = interruptionWarningHandler;
export const termination = handleBidEvicted;