Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: bus stack #55

Merged
merged 2 commits into from
Nov 30, 2022
Merged

feat: bus stack #55

merged 2 commits into from
Nov 30, 2022

Conversation

vasco-santos
Copy link
Contributor

@vasco-santos vasco-santos commented Nov 29, 2022

Adds bus stack with global custom EventBridge (future will also include Websocket API).

As part of this refactor to use Bus Stack, I also looked into removing the need to have a lambda function on the Event Bridge exit just to transform S3Event into a SQS Topic. A new transformation target rules was created to achieve this. Initially, I intended to keep same structure as what E-IPFS indexer receives (${message.region}/${message.bucketName}/${message.key}), so that we could also remove that handler. This ended up not working in any way with AWS Events Rules + InputTransformerProperty + RuleTargetInput. Ended up just relying on a new JSON string with the expected properties, which also is nicer because we don't need to apply regexes to remove things from the string as before. We still need to handle E-IPFS indexing with a lambda, but that interaction will also change in the near future.

This was tested with deployed resources and everything is working as expected - CAR uploaded to CARPARK triggers lambda to copy, and *.idx file is written into SATNAV bucket

Other notes:

  • renamed event-bridge/index.js file to event-bridge/source.js to better represent what that function does. It is the source event for the event bridge (so, lambda that forwards S3 Put Event to the Event bridge)
  • hooks up writes to Satnav bucket to trigger Events - previously not done because of stacks circular dependency as described in feat: satnav stack #49)

Closes #53

@seed-deploy
Copy link

seed-deploy bot commented Nov 29, 2022

View stack outputs

@seed-deploy seed-deploy bot temporarily deployed to pr55 November 29, 2022 13:24 Inactive
@seed-deploy seed-deploy bot temporarily deployed to pr55 November 29, 2022 13:33 Inactive
@seed-deploy seed-deploy bot temporarily deployed to pr55 November 29, 2022 13:59 Inactive
@seed-deploy seed-deploy bot temporarily deployed to pr55 November 29, 2022 14:02 Inactive
@seed-deploy seed-deploy bot temporarily deployed to pr55 November 29, 2022 14:09 Inactive
@seed-deploy seed-deploy bot temporarily deployed to pr55 November 29, 2022 14:33 Inactive
@seed-deploy seed-deploy bot temporarily deployed to pr55 November 29, 2022 14:40 Inactive
@seed-deploy seed-deploy bot temporarily deployed to pr55 November 29, 2022 14:56 Inactive
@seed-deploy seed-deploy bot temporarily deployed to pr55 November 29, 2022 15:01 Inactive
@seed-deploy seed-deploy bot temporarily deployed to pr55 November 29, 2022 15:13 Inactive
@seed-deploy seed-deploy bot temporarily deployed to pr55 November 29, 2022 15:43 Inactive
@vasco-santos vasco-santos marked this pull request as ready for review November 29, 2022 16:04
target: {
message: awsEvents.RuleTargetInput.fromObject({
region: awsEvents.EventField.fromPath('$.detail.region'),
bucket: awsEvents.EventField.fromPath('$.detail.bucketName'),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason not to match the original property name here? I am wary of renaming properties across events as it makes it harder to guess what a properties name will be when coding handlers for them.

Suggested change
bucket: awsEvents.EventField.fromPath('$.detail.bucketName'),
bucketName: awsEvents.EventField.fromPath('$.detail.bucketName'),

Copy link
Contributor

@olizilla olizilla left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice bus 🚌✨

None of the suggestions and comments here are blocking... just flagging things as we go.


return {
bucketRegion,
bucketName,
bucketRegion: region,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

discussion: same here... region becomes bucketRegion... can we normalise to avoid the renames?

* @param {string} eventBusName
*/
export async function notifyBus(event, eventBridge, eventBusName) {
const entries = event.Records
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const entries = event.Records
const s3Events = event.Records

Comment on lines 21 to 29
if (entries.length > 0) {
const feedbackEntries = entries.map((entry) => ({
EventBusName: eventBusName,
Source: SATNAV_EVENT_BRIDGE_SOURCE_EVENT,
DetailType: 'satnav_index_added',
Detail: JSON.stringify(entry),
}))
await eventBridge.putEvents({ Entries: feedbackEntries })
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: i think of this as mapping s3 events to bus events so suggestion is to name the lists to suggest that to the reader

Suggested change
if (entries.length > 0) {
const feedbackEntries = entries.map((entry) => ({
EventBusName: eventBusName,
Source: SATNAV_EVENT_BRIDGE_SOURCE_EVENT,
DetailType: 'satnav_index_added',
Detail: JSON.stringify(entry),
}))
await eventBridge.putEvents({ Entries: feedbackEntries })
}
if (s3Events.length > 0) {
const busEvents = s3Events.map((entry) => ({
EventBusName: eventBusName,
Source: SATNAV_EVENT_BRIDGE_SOURCE_EVENT,
DetailType: 'satnav_index_added',
Detail: JSON.stringify(entry),
}))
await eventBridge.putEvents({ Entries: busEvents })
}


const eventBusName = 'event-bus-arn'

test('notifies event bus when new satnav bucket is written', async t => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test('notifies event bus when new satnav bucket is written', async t => {
test('notifies event bus when an .idx file is added to the satnav bucket', async t => {

t.is(response.statusCode, 200)
})

test('does not notify event bus when satnav bucket is written with non idx files', async t => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
test('does not notify event bus when satnav bucket is written with non idx files', async t => {
test('does not notify event bus when a non .idx file is added to the satnav bucket', async t => {

const carId = sqsEvent.Records[0].body
const info = carId.match(/([^/]+)\/([^/]+)\/(.+)/)
if (!info) {
const body = sqsEvent.Records[0].body
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we end up with a lib module at some point? I wish we didn't have to do this boilerplate at all!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think we should. I will create issue

@@ -22,7 +23,9 @@ export function SatnavStack({ stack }) {
const stackConfig = getConfig(stack.stage)

// Get carpark reference
const { carparkBucket, carparkEventBus } = use(CarparkStack)
const { carparkBucket } = use(CarparkStack)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we can limit the permissions here so that the index writer can only read files from the bucket.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should look into overall permissions yes. I will create issue

@vasco-santos vasco-santos mentioned this pull request Nov 30, 2022
1 task
@seed-deploy seed-deploy bot temporarily deployed to pr55 November 30, 2022 13:47 Inactive
@vasco-santos vasco-santos merged commit 6cca987 into main Nov 30, 2022
@vasco-santos vasco-santos deleted the feat/bus-stack branch November 30, 2022 13:54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

EventBus Stack
2 participants