sst 2.11.1 → 2.11.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/constructs/EventBus.d.ts
CHANGED
|
@@ -32,6 +32,10 @@ export interface EventBusFunctionTargetProps {
|
|
|
32
32
|
* The function to trigger
|
|
33
33
|
*/
|
|
34
34
|
function?: FunctionDefinition;
|
|
35
|
+
/**
|
|
36
|
+
* Number of retries
|
|
37
|
+
*/
|
|
38
|
+
retries?: number;
|
|
35
39
|
cdk?: {
|
|
36
40
|
function?: lambda.IFunction;
|
|
37
41
|
target?: LambdaFunctionTargetProps;
|
|
@@ -212,6 +216,17 @@ export interface EventBusProps {
|
|
|
212
216
|
* ```
|
|
213
217
|
*/
|
|
214
218
|
function?: FunctionProps;
|
|
219
|
+
/**
|
|
220
|
+
* Enable retries with exponential backoff for all lambda function targets in this eventbus
|
|
221
|
+
*
|
|
222
|
+
* @example
|
|
223
|
+
* ```js
|
|
224
|
+
* new EventBus(stack, "Bus", {
|
|
225
|
+
* retries: 20
|
|
226
|
+
* });
|
|
227
|
+
* ```
|
|
228
|
+
*/
|
|
229
|
+
retries?: number;
|
|
215
230
|
};
|
|
216
231
|
/**
|
|
217
232
|
* The rules for the eventbus
|
|
@@ -409,11 +424,18 @@ export declare class EventBus extends Construct implements SSTConstruct {
|
|
|
409
424
|
};
|
|
410
425
|
/** @internal */
|
|
411
426
|
getFunctionBinding(): FunctionBindingProps;
|
|
427
|
+
private retrierQueue;
|
|
428
|
+
private retrierFn;
|
|
429
|
+
private getRetrier;
|
|
412
430
|
private createEventBus;
|
|
413
431
|
private addRule;
|
|
414
432
|
private addTarget;
|
|
415
433
|
private addQueueTarget;
|
|
416
434
|
private addLogGroupTarget;
|
|
417
435
|
private addCdkFunctionTarget;
|
|
436
|
+
private subs;
|
|
437
|
+
subscribe(type: string, target: FunctionDefinition, props?: {
|
|
438
|
+
retries?: number;
|
|
439
|
+
}): this;
|
|
418
440
|
private addFunctionTarget;
|
|
419
441
|
}
|
package/constructs/EventBus.js
CHANGED
|
@@ -1,9 +1,14 @@
|
|
|
1
1
|
import { Construct } from "constructs";
|
|
2
|
+
import * as lambda from "aws-cdk-lib/aws-lambda";
|
|
3
|
+
import * as sqs from "aws-cdk-lib/aws-sqs";
|
|
2
4
|
import * as events from "aws-cdk-lib/aws-events";
|
|
3
5
|
import { LambdaFunction as LambdaFunctionTarget, SqsQueue as SqsQueueTarget, CloudWatchLogGroup as LogGroupTarget, } from "aws-cdk-lib/aws-events-targets";
|
|
4
6
|
import { Queue } from "./Queue.js";
|
|
5
7
|
import { getFunctionRef, isCDKConstruct } from "./Construct.js";
|
|
6
8
|
import { Function as Fn, } from "./Function.js";
|
|
9
|
+
import { SqsEventSource } from "aws-cdk-lib/aws-lambda-event-sources";
|
|
10
|
+
import { SqsDestination } from "aws-cdk-lib/aws-lambda-destinations";
|
|
11
|
+
import path from "path";
|
|
7
12
|
/////////////////////
|
|
8
13
|
// Construct
|
|
9
14
|
/////////////////////
|
|
@@ -224,6 +229,25 @@ export class EventBus extends Construct {
|
|
|
224
229
|
},
|
|
225
230
|
};
|
|
226
231
|
}
|
|
232
|
+
retrierQueue;
|
|
233
|
+
retrierFn;
|
|
234
|
+
getRetrier() {
|
|
235
|
+
if (this.retrierFn && this.retrierQueue) {
|
|
236
|
+
return { fn: this.retrierFn, queue: this.retrierQueue };
|
|
237
|
+
}
|
|
238
|
+
this.retrierQueue = new sqs.Queue(this, `RetrierQueue`);
|
|
239
|
+
this.retrierFn = new lambda.Function(this, `RetrierFunction`, {
|
|
240
|
+
runtime: lambda.Runtime.NODEJS_14_X,
|
|
241
|
+
handler: "index.handler",
|
|
242
|
+
code: lambda.Code.fromAsset(path.join(__dirname, "../support/event-bus-retrier")),
|
|
243
|
+
environment: {
|
|
244
|
+
RETRIER_QUEUE_URL: this.retrierQueue.queueUrl,
|
|
245
|
+
},
|
|
246
|
+
});
|
|
247
|
+
this.retrierFn.addEventSource(new SqsEventSource(this.retrierQueue));
|
|
248
|
+
this.retrierQueue.grantSendMessages(this.retrierFn);
|
|
249
|
+
return { fn: this.retrierFn, queue: this.retrierQueue };
|
|
250
|
+
}
|
|
227
251
|
createEventBus() {
|
|
228
252
|
const app = this.node.root;
|
|
229
253
|
const id = this.node.id;
|
|
@@ -321,14 +345,36 @@ export class EventBus extends Construct {
|
|
|
321
345
|
// Create target
|
|
322
346
|
eventsRule.addTarget(new LambdaFunctionTarget(fn, targetProps));
|
|
323
347
|
}
|
|
348
|
+
subs = new Map();
|
|
349
|
+
subscribe(type, target, props) {
|
|
350
|
+
const count = this.subs.get(type) || 0 + 1;
|
|
351
|
+
this.subs.set(type, count);
|
|
352
|
+
const name = `${type.replaceAll(/[^a-zA-Z_]/g, "_")}_${count}`;
|
|
353
|
+
const fn = Fn.fromDefinition(this, name, target);
|
|
354
|
+
this.addRule(this, name + "_rule", {
|
|
355
|
+
pattern: {
|
|
356
|
+
detailType: [type],
|
|
357
|
+
},
|
|
358
|
+
targets: {
|
|
359
|
+
[name + "_target"]: {
|
|
360
|
+
type: "function",
|
|
361
|
+
function: fn,
|
|
362
|
+
retries: props?.retries,
|
|
363
|
+
},
|
|
364
|
+
},
|
|
365
|
+
});
|
|
366
|
+
return this;
|
|
367
|
+
}
|
|
324
368
|
addFunctionTarget(scope, ruleKey, eventsRule, targetName, target) {
|
|
325
369
|
// Parse target props
|
|
326
370
|
let targetProps;
|
|
327
371
|
let functionDefinition;
|
|
372
|
+
let retries = this.props.defaults?.retries;
|
|
328
373
|
if (target.function) {
|
|
329
374
|
target = target;
|
|
330
375
|
targetProps = target.cdk?.target;
|
|
331
376
|
functionDefinition = target.function;
|
|
377
|
+
retries = target.retries;
|
|
332
378
|
}
|
|
333
379
|
else {
|
|
334
380
|
target = target;
|
|
@@ -339,6 +385,15 @@ export class EventBus extends Construct {
|
|
|
339
385
|
this.targetsData[ruleKey][targetName] = fn;
|
|
340
386
|
// Create target
|
|
341
387
|
eventsRule.addTarget(new LambdaFunctionTarget(fn, targetProps));
|
|
388
|
+
// Configure retrier
|
|
389
|
+
if (retries) {
|
|
390
|
+
const retrier = this.getRetrier();
|
|
391
|
+
retrier.fn.addEnvironment(`RETRIER_${retrier.fn.functionName}`, retries.toString());
|
|
392
|
+
fn.grantInvoke(retrier.fn);
|
|
393
|
+
fn.configureAsyncInvoke({
|
|
394
|
+
onFailure: new SqsDestination(retrier.queue),
|
|
395
|
+
});
|
|
396
|
+
}
|
|
342
397
|
// Attach existing permissions
|
|
343
398
|
this.permissionsAttachedForAllTargets.forEach((permissions) => fn.attachPermissions(permissions));
|
|
344
399
|
fn.bind(this.bindingForAllTargets);
|
|
@@ -1,3 +1,28 @@
|
|
|
1
1
|
export interface EventBusResources {
|
|
2
2
|
}
|
|
3
3
|
export declare const EventBus: EventBusResources;
|
|
4
|
+
import { EventBridgeEvent } from "aws-lambda";
|
|
5
|
+
import { ZodAny, ZodObject, ZodRawShape, z } from "zod";
|
|
6
|
+
export declare function createEventBuilder<Bus extends keyof typeof EventBus, MetadataShape extends ZodRawShape | undefined, MetadataFunction extends () => any>(props: {
|
|
7
|
+
bus: Bus;
|
|
8
|
+
metadata?: MetadataShape;
|
|
9
|
+
metadataFn?: MetadataFunction;
|
|
10
|
+
}): <Type extends string, Shape extends ZodRawShape, Properties = z.objectOutputType<Shape, ZodAny, "strip">>(type: Type, properties: Shape) => {
|
|
11
|
+
publish: undefined extends MetadataShape ? (properties: Properties) => Promise<void> : (properties: Properties, metadata: z.infer<ZodObject<Exclude<MetadataShape, undefined>, "strip", ZodAny>>) => Promise<void>;
|
|
12
|
+
type: Type;
|
|
13
|
+
shape: {
|
|
14
|
+
metadata: Parameters<undefined extends MetadataShape ? (properties: Properties) => Promise<void> : (properties: Properties, metadata: z.infer<ZodObject<Exclude<MetadataShape, undefined>, "strip", ZodAny>>) => Promise<void>>[1];
|
|
15
|
+
properties: Properties;
|
|
16
|
+
metadataFn: ReturnType<MetadataFunction>;
|
|
17
|
+
};
|
|
18
|
+
};
|
|
19
|
+
export type inferEvent<T extends {
|
|
20
|
+
shape: ZodObject<any>;
|
|
21
|
+
}> = z.infer<T["shape"]>;
|
|
22
|
+
export declare function EventHandler<Event extends {
|
|
23
|
+
shape: {
|
|
24
|
+
properties: any;
|
|
25
|
+
metadata: any;
|
|
26
|
+
metadataFn: any;
|
|
27
|
+
};
|
|
28
|
+
}>(_events: Event, cb: (properties: Event["shape"]["properties"], metadata: undefined extends Event["shape"]["metadata"] ? Event["shape"]["metadataFn"] : Event["shape"]["metadata"]) => Promise<void>): (event: EventBridgeEvent<string, any>) => Promise<void>;
|
package/node/event-bus/index.js
CHANGED
|
@@ -1,3 +1,53 @@
|
|
|
1
1
|
import { createProxy } from "../util/index.js";
|
|
2
2
|
export const EventBus =
|
|
3
3
|
/* @__PURE__ */ createProxy("EventBus");
|
|
4
|
+
import { EventBridgeClient, PutEventsCommand, } from "@aws-sdk/client-eventbridge";
|
|
5
|
+
import { z } from "zod";
|
|
6
|
+
const client = new EventBridgeClient({});
|
|
7
|
+
export function createEventBuilder(props) {
|
|
8
|
+
return function createEvent(type, properties) {
|
|
9
|
+
const propertiesSchema = z.object(properties);
|
|
10
|
+
const metadataSchema = props.metadata
|
|
11
|
+
? z.object(props.metadata)
|
|
12
|
+
: undefined;
|
|
13
|
+
const publish = async (properties, metadata) => {
|
|
14
|
+
console.log("publishing", type, properties);
|
|
15
|
+
await client.send(new PutEventsCommand({
|
|
16
|
+
Entries: [
|
|
17
|
+
{
|
|
18
|
+
// @ts-expect-error
|
|
19
|
+
EventBusName: EventBus[props.bus].busName,
|
|
20
|
+
Source: "console",
|
|
21
|
+
Detail: JSON.stringify({
|
|
22
|
+
properties: propertiesSchema.parse(properties),
|
|
23
|
+
metadata: (() => {
|
|
24
|
+
if (metadataSchema) {
|
|
25
|
+
return metadataSchema.parse(metadata);
|
|
26
|
+
}
|
|
27
|
+
if (props.metadataFn) {
|
|
28
|
+
return props.metadataFn();
|
|
29
|
+
}
|
|
30
|
+
})(),
|
|
31
|
+
}),
|
|
32
|
+
DetailType: type,
|
|
33
|
+
},
|
|
34
|
+
],
|
|
35
|
+
}));
|
|
36
|
+
};
|
|
37
|
+
return {
|
|
38
|
+
publish: publish,
|
|
39
|
+
type,
|
|
40
|
+
shape: {
|
|
41
|
+
metadata: {},
|
|
42
|
+
properties: {},
|
|
43
|
+
metadataFn: {},
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
export function EventHandler(_events, cb) {
|
|
49
|
+
return async (event) => {
|
|
50
|
+
console.log("received", event);
|
|
51
|
+
await cb(event.detail.properties, event.detail.metadata);
|
|
52
|
+
};
|
|
53
|
+
}
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"sideEffects": false,
|
|
3
3
|
"name": "sst",
|
|
4
|
-
"version": "2.11.
|
|
4
|
+
"version": "2.11.3",
|
|
5
5
|
"bin": {
|
|
6
6
|
"sst": "cli/sst.js"
|
|
7
7
|
},
|
|
@@ -32,6 +32,7 @@
|
|
|
32
32
|
"@aws-cdk/cloudformation-diff": "2.79.1",
|
|
33
33
|
"@aws-cdk/cx-api": "2.79.1",
|
|
34
34
|
"@aws-sdk/client-cloudformation": "^3.279.0",
|
|
35
|
+
"@aws-sdk/client-eventbridge": "^3.342.0",
|
|
35
36
|
"@aws-sdk/client-iam": "^3.279.0",
|
|
36
37
|
"@aws-sdk/client-iot": "^3.279.0",
|
|
37
38
|
"@aws-sdk/client-iot-data-plane": "^3.279.0",
|
|
@@ -87,12 +88,14 @@
|
|
|
87
88
|
"undici": "^5.12.0",
|
|
88
89
|
"uuid": "^9.0.0",
|
|
89
90
|
"ws": "^8.11.0",
|
|
90
|
-
"yargs": "^17.6.2"
|
|
91
|
+
"yargs": "^17.6.2",
|
|
92
|
+
"zod": "^3.21.4"
|
|
91
93
|
},
|
|
92
94
|
"devDependencies": {
|
|
93
95
|
"@aws-sdk/client-api-gateway": "^3.208.0",
|
|
94
96
|
"@aws-sdk/client-cloudfront": "^3.279.0",
|
|
95
97
|
"@aws-sdk/client-codebuild": "^3.279.0",
|
|
98
|
+
"@aws-sdk/client-sqs": "^3.341.0",
|
|
96
99
|
"@aws-sdk/types": "^3.272.0",
|
|
97
100
|
"@graphql-tools/merge": "^8.3.16",
|
|
98
101
|
"@sls-next/lambda-at-edge": "^3.7.0",
|