sst 2.11.1 → 2.11.2

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.
@@ -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
  }
@@ -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);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "sideEffects": false,
3
3
  "name": "sst",
4
- "version": "2.11.1",
4
+ "version": "2.11.2",
5
5
  "bin": {
6
6
  "sst": "cli/sst.js"
7
7
  },
@@ -93,6 +93,7 @@
93
93
  "@aws-sdk/client-api-gateway": "^3.208.0",
94
94
  "@aws-sdk/client-cloudfront": "^3.279.0",
95
95
  "@aws-sdk/client-codebuild": "^3.279.0",
96
+ "@aws-sdk/client-sqs": "^3.341.0",
96
97
  "@aws-sdk/types": "^3.272.0",
97
98
  "@graphql-tools/merge": "^8.3.16",
98
99
  "@sls-next/lambda-at-edge": "^3.7.0",