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

fix : AWS_EVENTBRIDGE 404 #611

Closed
wants to merge 1 commit into from
Closed

Conversation

imdkbj
Copy link

@imdkbj imdkbj commented Dec 28, 2022

Issue #, if available:
Lambda is getting closed with 404 for trigger from eventbridge. I am not sure this is correct way or not.

Description of changes:
added path handler.

Checklist

  • Tests have been added and are passing
  • Documentation has been updated

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@brettstack
Copy link
Collaborator

@imdkbj could you explain the problem a little more and link to relevant documentation?

@brettstack brettstack closed this Dec 15, 2023
@imdkbj
Copy link
Author

imdkbj commented Jan 3, 2024

Hey @brettstack , I failed to understand , what more info you required as this patch was to fix where lambda was crashing if it has been triggered from the event bridge.

    "errorType": "Error",
    "errorMessage": "Unable to determine event source based on event.",
    "stack": [
        "Error: Unable to determine event source based on event.",
        "    at getEventSourceNameBasedOnEvent (/var/task/node_modules/@codegenie/serverless-express/src/event-sources/utils.js:134:9)",
        "    at proxy (/var/task/node_modules/@codegenie/serverless-express/src/configure.js:38:51)",
        "    at handler (/var/task/node_modules/@codegenie/serverless-express/src/configure.js:99:12)",
        "    at setup (/var/task/lambda.js:12:10)",
        "    at Runtime.handler (/var/task/lambda.js:19:10)",
        "    at Runtime.handleOnceNonStreaming (file:///var/runtime/index.mjs:1173:29)"
    ]
}

@brettstack
Copy link
Collaborator

Thanks for the reply. I'm just looking for "why" it needs to be set to *. Is this unique to event bridge? If so, can the fox be applied to the event bridge request mapper?

@brettstack brettstack reopened this Jan 3, 2024
@imdkbj
Copy link
Author

imdkbj commented Jan 4, 2024

Thanks for the reply.

Hey @brettstack Let me check why I have added * over mapping, Might be due to naming change between event bridge versions or * will simply allow it to trigger from anywhere. Allow me sometime, I will get back soon.

@imdkbj
Copy link
Author

imdkbj commented Jan 9, 2024

Dear @brettstack

While this is quite old PR. I am not able to recall the reason for putting * there. I have found the workaround what has been done to accomplish that as AWS_EVENTBRIDGE doesn't provide event related info, it's just sending the payload.

Payload defined at event bridge.

{ "task": "XYZ", "source": "AWS_EVENTBRIDGE" }

lambda handler

/* eslint-disable consistent-return */
require('source-map-support/register');
const serverlessExpress = require('@vendia/serverless-express');
const app = require('./src/app');
const { initDB } = require('./src/v1/db');
const { serviceMap } = require('./src/v1/service');

let serverlessExpressInstance;

async function handleScheduler(event) {
  // call if event from event bridge
  const scheduler = event?.source === 'AWS_EVENTBRIDGE';
  const task = event?.task;

  if (!scheduler) {
    return false;
  }

  if (!task) {
    console.log('No task defined for scheduler');
    return true;
  }

  console.log('Running scheduler');
  console.log(event);
  const service = serviceMap[task];
  await initDB();
  await service();

  return true;
}

async function setup(event, context) {
  const isScheduler = await handleScheduler(event);
  if (isScheduler) {
    return;
  }
  serverlessExpressInstance = serverlessExpress({
    app
  });
  return serverlessExpressInstance(event, context);
}

function handler(event, context) {
  if (serverlessExpressInstance) {
    return serverlessExpressInstance(event, context);
  }
  return setup(event, context);
}

exports.handler = handler;

@imdkbj imdkbj closed this Jan 9, 2024
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.

2 participants