Home > Enterprise >  AWS SNS not attaching a Lambda trigger when the subscription is created via the SDK
AWS SNS not attaching a Lambda trigger when the subscription is created via the SDK

Time:11-29

I want to use a SNS subscription to trigger a Lambda function. First I have created a topic and subscription via the AWS SDK:

    import {SubscribeCommand } from "@aws-sdk/client-sns";
    import {snsClient } from "./libs/snsClient.js";

    // Set the parameters
    const params = {
      Protocol: "lambda" /* required */,
      TopicArn: "TOPIC_ARN", //TOPIC_ARN
      Endpoint: "LAMBDA_FUNCTION_ARN", //LAMBDA_FUNCTION_ARN
    };

    const run = async () => {
      try {
        const data = await snsClient.send(new SubscribeCommand(params));
        console.log("Success.",  data);
        return data; // For unit tests.
      } catch (err) {
        console.log("Error", err.stack);
      }
    };
    run();

It is creating the subscription, but when I check the Lambda, this sub is not appearing as a Lambda trigger. empty trigger

When I create the subscription manually without using the SDK it's working. enter image description here

I would like to know why my subscription that created with the SDK is not showing in the Lambda's trigger.

CodePudding user response:

Looks like you are missing the IAM permissions. SNS needs explicit permission to invoke your Lambda.

See How do I subscribe a Lambda function to an Amazon SNS topic in the same account? in the docs. The lambda add-permission CLI step from the example is the same as the AddPermissionCommand in the SDK Lambda client. Provide your SNS topic's ARN.

  • Related