Hi there,
I use event bridge to send events to labmda functions. I have this working perfectly between different micro-services (different sls projects). However, when I throw an event with a listener in the same sls project, I’m not receiving it! no error nothing. It all worked fine sls offline
with the serverless-offline-aws-eventbridge
but once I deploy to aws… nothing . Please tell me what I’m doing wrong?
I omitted some code here and there. But I provide all the relevant info. In the example below, I call an API call through http (POST /service/items), which creates a bunch of serviceItems in DynamoDB and publishes an event called… serviceItemsCreated. Then a listener listens to this event and does some additional magic.
serverless.yml
custom:
eventBusArn: arn:aws:events:${env:REGION}:${env:AWS_ACCOUNT_ID}:event-bus/waitless-dev-frank
provider:
name: aws
runtime: nodejs12.x
region: ${env:REGION} // eu-central-1
environment:
EVENT_BUS_ARN: ${self:custom.eventBusArn}
REGION: ${env:REGION}
iam:
role:
statements:
- Effect: Allow
Action:
- events:putEvents
Resource:
- ${self:custom.eventBusArn}
functions:
# API
serviceApi:
handler: src/app.handler
events:
- http:
path: /
method: any
serviceItemsCreated:
handler: src/ws.sendEventToServiceConnections
events:
- eventBridge:
eventBus: ${self:custom.eventBusArn}
pattern:
source:
- waitless.api.service
detail-type:
- serviceItemsCreated
src/app.ts
does some generic/security magic and includes ./routes/create-service-items.ts
src/routes/create-service-items.ts
import express, { Request, Response } from 'express';
import { publishEvent } from '../libs/events';
router.post('/service/items', async (req: Request, res: Response) => {
// perform some checks
// generate some serviceItems
// do some db mutation
// publish event serviceItemsCreated
await publishEvent({
source: 'waitless.api.service',
detailType: Types.EventType.serviceItemsCreated,
detail: JSON.stringify(serviceItems)
});
res.status(201).send(serviceItems);
}
);
src/libs/events.ts
I put some logging in this file so I can check in cloudwatch what is happening.
import { CustomError, Types } from '@frank/external-libs';
import AWS from 'aws-sdk';
const EVENT_BUS_ARN = process.env.EVENT_BUS_ARN;
const REGION = process.env.REGION;
console.log('libs / events', 'EVENT_BUS_ARN', EVENT_BUS_ARN);
console.log('libs / events', 'REGION', REGION);
const config = {
region: REGION,
}
const eventBridge = new AWS.EventBridge(config);
export const publishEvent = async (event: Types.Event) => {
console.log('services / events', 'publish event', event);
const params = {
Entries: [
{
EventBusName: EVENT_BUS_ARN,
Source: event.source,
DetailType: event.detailType,
Detail: event.detail,
},
],
};
console.log('services / events', 'params', params);
try {
await eventBridge.putEvents(params).promise();
console.log('yay');
return;
} catch (e) {
console.error(e);
throw new CustomError(500, 'publish event failed', 'EVENTBRIDGE-PublishEventFailed', e);
}
}
and finally… the place where that event should arrive:
src/ws.ts
// WS Send Event to Service Connections
export const sendEventToServiceConnections = async (event: EventBridgeEvent<Types.EventType, Types.ServiceItem[]>) => {
// well pretty much irrelevant cause it never gets called i'll just put a console log here
console.log('here');
}
Hopefully, you have found my error already and made me a happy person. Nevertheless, I will show how I attemt to get to that last part:
-
Kicking off that POST /service/items using Postman. 201 - Created. So far, so good.
-
Database entries have been created. yass!
-
Checking that serviceApi log stream. Looks like that event was published.
-
Verify if my eventBridge rule has been created… yes looking good.
-
Checking the serviceItemsCreated log stream… no log streams!
-
Also, metrics for this rule… are non-existent
I know, long read. Sorry about that. Do you know what i’m doing wrong? Please advice.
Thank you, Frank