Hey guys, A quick question. How can I reference th...
# help
a
Hey guys, A quick question. How can I reference the live version of a function in sns topic subscribers? My code looks like this
Copy code
// WebhookEventsProcessor
const webhookEventsProcessor = new Function(this, 'WebhookEventsProcessor', {
  handler: 'src/webhookEventsProcessor.handler',
  functionName: `${scope.stage}-webhook-events-processor`,
  memorySize: 256,
  timeout: 30,
  environment: {
    ...globalEnvironment,
    SEND_WEBHOOK_EVENTS_QUEUE: sendWebhookEventsQueue.sqsQueue.queueUrl,
  },
  permissions: ['sns', 'sqs'],
  currentVersionOptions: {
    provisionedConcurrentExecutions: 1,
  },
  vpc: appVpc,
  vpcSubnets: {
    subnets: appVpc.privateSubnets,
  },
});
const webhookEventsProcessorLive = webhookEventsProcessor.currentVersion;
const messagingEventsTopic = new Topic(this, 'MessagingEvents', {
  subscribers: [
    {
      function: webhookEventsProcessorLive,
    },
  ],
});
and the error I'm getting when trying to build this is
Copy code
{"awsRequestId":"NOT SET YET","level":"error","message":"(node:24544) UnhandledPromiseRejectionWarning: Error: Invalid function definition for the \"Subscriber_0\" Fun
ction\n    at Function.fromDefinition (/code/messaging/node_modules/@serverless-stack/resources/src/Function.ts:433:11)\n    at Topic.addFunctionSubscriber (/code/mess
aging/node_modules/@serverless-stack/resources/src/Topic.ts:203:19)\n    at Topic.addSubscriber (/code/messaging/node_modules/@serverless-stack/resources/src/Topic.ts:
158:12)\n    at /code/messaging/node_modules/@serverless-stack/resources/src/Topic.ts:117:46\n    at Array.forEach (<anonymous>)\n    at Topic.addSubscribers (/code/me
ssaging/node_modules/@serverless-stack/resources/src/Topic.ts:117:17)\n    at new Topic (/code/messaging/node_modules/@serverless-stack/resources/src/Topic.ts:72:10)\n
    at new MindfulMessagingStack (/code/messaging/services/sst/lib/MindfulMessagingStack.ts:146:34)\n    at Object.main (/code/messaging/services/sst/lib/index.ts:27:2
5)\n    at processTicksAndRejections (internal/process/task_queues.js:95:5)\n(Use `node --trace-warnings ...` to show where the warning was created) "}
{"awsRequestId":"NOT SET YET","level":"error","message":"(node:24544) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by th
rowing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). To terminate the node process on unhandled pro
mise rejection, use the CLI flag `--unhandled-rejections=strict` (see <https://nodejs.org/api/cli.html#cli_unhandled_rejections_mode>). (rejection id: 1) "}
{"awsRequestId":"NOT SET YET","level":"error","message":"(node:24544) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise
 rejections that are not handled will terminate the Node.js process with a non-zero exit code. "}
{"awsRequestId":"NOT SET YET","level":"info","message":"\u001b[90mLinting Lambda function source\u001b[39m "}
f
Hey @Aram, instead of:
Copy code
subscribers: [
    {
      function: webhookEventsProcessorLive,
    },
  ],
Try this:
Copy code
subscribers: [
    {
      function: webhookEventsProcessor,
    },
  ],
You need to use the function as the subscriber, instead of the version.
a
@Frank that doesn't seem to work, it adds the trigger to the original function not the tagged version
this is what worked for us
Copy code
// WebhookEventsProcessor
const webhookEventsProcessor = new Function(this, 'WebhookEventsProcessor', {
  handler: 'src/webhookEventsProcessor.handler',
  functionName: `${scope.stage}-webhook-events-processor`,
  memorySize: 256,
  timeout: 30,
  environment: {
    ...globalEnvironment,
    SEND_WEBHOOK_EVENTS_QUEUE: sendWebhookEventsQueue.sqsQueue.queueUrl,
  },
  permissions: ['sns', 'sqs'],
  currentVersionOptions: {
    provisionedConcurrentExecutions: 1,
  },
});
const webhookEventsProcessorLive = webhookEventsProcessor.currentVersion;

const messagingEventsTopic = new sns.Topic(this, 'MessagingEvents');
messagingEventsTopic.addSubscription(new subscriptions.LambdaSubscription(webhookEventsProcessorLive));
f
Ah I see. Currently
sst.Topic
doesn’t take a version for the subscriber. Let me open an issue for it.
But since you are pointing to the
webhookEventsProcessor.currentVersion
, isn’t it the same as adding the trigger to the original function?
a
it invokes the original function because trigger is added to it but that doesn't work for us because we try to keep lambdas hot. This is a messaging app and we need a few of our lambdas be up all the time to deliver the messages as soon as possible, so invoking the original function still requires that to start up and the process the event which takes a lot of time
f