I’m using Lambda to call EventBridge `putEvents`. ...
# sst
a
I’m using Lambda to call EventBridge
putEvents
. Most of the time works fine, but sometimes when the lambda starts from cold-start I get timeout when calling eventBridge…. it’s like the EventBridge call never ends and this produces a timeout on the lambda/api-gw …. It’s weird because it doesn’t happen all the time. Checked permissions and those are ok.
a
add a dlq, it’ll handle the failed requests.
a
@Ashishkumar Pandey didn’t know about DLQ on EB… but still, I’m getting timeout pushing the event.
So my lambda timesout.
Not sure how DLQ will solve it.
My problem is pushing the event, not pulling.
a
no, setup the dlq on the function that makes the putEvents call. if the putEvents calls timeout the request will be put into the sqs queue which you can retry with the same function again. It’s a simple retry mechanism.
a
Oh I see.
Yeah that makes sense.
Is weird still, to get timeout.
I mean all the time.
f
hmm you mean Lambda cold start? It feels weird that the Lambda cold start is related to the
putEvents
call timeout.
a
Yeah not sure what is the reason yet.
😞
f
If the
putEvents
call is timing out, you can play around with the aws sdk client’s http timeout and retry.
a
Ok, good idea.
f
I’ve solved a dynamodb timeout issue that might or might not be similar to this. See if it helps https://seed.run/blog/how-to-fix-dynamodb-timeouts-in-serverless-application
a
Thanks @Frank. Also, now that you here… I’m migrating EventBus CDK to SST, and can’t find example of using a Lambda function as Rule here: https://docs.serverless-stack.com/constructs/EventBus#eventbusname
I found example of referencing the source file, but no the Function.
My use-case: I declare a function on my stack, and after that I assign it to a shared Bus.
Copy code
const writeSurveyResponses = new Function(this, 'writeSurveyResponses', {
});

props.tasksEventBus.addRules(this, {
      writeSurveyResponsesEventBridgeRule: {
        targets: [writeSurveyResponses],
      },
    });
f
Yeah that looks right. Did it work?
a
Yes.
It worked, I didn’t deploy yet, but looks like works.
I was thinking to add it to the Doc.
@Frank also, SST EventBridge accepts
targetProps
but it doesn’t accept SST-Queue for
deadLetterQueue
.
Maybe SST has to extend the
TargetBaseProps
from CDK? It has this definition:
Copy code
readonly deadLetterQueue?: sqs.IQueue;
Getting compile issues 😞
Copy code
TypeError: construct[methodName] is not a function
    at /Users/amouly/Projects/yabble/yabble-serverless/node_modules/@serverless-stack/resources/src/util/permission.ts:205:70
    at Array.forEach (<anonymous>)
    at Object.attachPermissionsToRole (/Users/amouly/Projects/yabble/yabble-serverless/node_modules/@serverless-stack/resources/src/util/permission.ts:77:15)
    at Function.attachPermissions (/Users/amouly/Projects/yabble/yabble-serverless/node_modules/@serverless-stack/resources/src/Function.ts:373:7)
    at new Function (/Users/amouly/Projects/yabble/yabble-serverless/node_modules/@serverless-stack/resources/src/Function.ts:357:12)
    at Function.fromDefinition (/Users/amouly/Projects/yabble/yabble-serverless/node_modules/@serverless-stack/resources/src/Function.ts:477:14)
    at Api.createFunctionIntegration (/Users/amouly/Projects/yabble/yabble-serverless/node_modules/@serverless-stack/resources/src/Api.ts:533:23)
    at Api.addRoute (/Users/amouly/Projects/yabble/yabble-serverless/node_modules/@serverless-stack/resources/src/Api.ts:428:26)
    at /Users/amouly/Projects/yabble/yabble-serverless/node_modules/@serverless-stack/resources/src/Api.ts:294:12
    at Array.forEach (<anonymous>)
I see, the problem is the permissions.
With CDK EB, this works defined in my Lambda:
Copy code
permissions: [[this.tasksEventBus, 'grantPutEventsTo']],
But it doesn’t compile with SST EB. But this work with SST EB:
Copy code
permissions: [this.tasksEventBus],
f
Re permissions, hmm.. is
this.tasksEventBus
an instance of
sst.EventBus
? If it is,
events:*
should’ve been granted to the Lambda https://github.com/serverless-stack/serverless-stack/blob/master/packages/resources/src/util/permission.ts#L180-L183
a
Yes.
But I din’t want
*
.
I want to have
putEvents
only.
Like my example with CDK EventBridge.
My first example is with CDK EB, the second example below is SST EB.
f
Ah, sorry yeah I mis-read ur message, yeah
permissions: [[this.tasksEventBus, 'grantPutEventsTo']]
only works if
this.tasksEventBus
were the CDK EventBridge.
a
Yeah noticed this the hard way.
So how can I add proper permissions with SST-EB?
I think adding
*
is not good.
f
Yeah, we should document this better. We wanted to provide a couple of ways to grant permissions depending on how restrict you want the permissions to be: •
permissions: [sstEventBus]
grants events:* to the specified bus •
permissions: ["events:PutEvents"]
grants the specified permission to all buses •
permissions: [[sstEventBus.eventBridgeEventBus, "grantPutEventsTo"]]
grants the specified permission to the specified bus •
permissions: [new iam.PolicyStatement(...)]
the most precise way to grant permissions, also the most cumbersome
a
Ahhh I see!
I have to use
sstEventBus.eventBridgeEventBus
.
That makes sense, I was expecting for SST to decapsulate it.
f
Re: DLQ, if you have an SST queue, you can get the SQS Queue like this
sstQueue.sqsQueue
and you can set that as the
deadLetterQueue
a
Yeah I was also expecting to encapsulate that logic in SST.
Is hard to know sometimes when is supported and when is not.
I mean, for Target we support SST Queue, but not for DLQ.
So kind of hard to know at the beginning, was expecting to use the same constructs in both cases.
f
Yeah, I was just thinking about this.. maybe SST queue should just extend the SQS queue
a
Could be, not sure what extra is adding.
@Frank thanks for the article, might be the same issue with EB, the exponential backoff or whatever is called.