Ryan Kreager
Ryan Kreager

Reputation: 3581

Trigger Amazon SNS message via Amazon Lambda

I have an Amazon Lambda instance and an Amazon SNS instance. The Lambda code watches for changes in our database and I would like it to make a call to Amazon SNS to send pushes to our users. For example:

When a user on one of our forums gets a new message, the Lambda code recognizes this change every time it is run (every 10 minutes) and should send a push to the user's smartphone via SNS.

I'm running into a wall when it comes to the documentation; Amazon's docs only talk about how to trigger Lambda code via SNS, but not the reverse. Does anyone have an example of how I can accomplish this?

Upvotes: 2

Views: 3076

Answers (3)

JMA
JMA

Reputation: 1825

You can use the lambda below to push an SNS message to a user, but you must know what the endpoint ARN is for that user. For example, if in an Android app, when the user logs in you will have the app send a GCM (Google Cloud Messaging) token to your backend (via an API call that triggers a lambda, for example). Your backend, which is connected to GCM, can then use this token to lookup which endpoint ARN corresponds to such user and put that in the lambda below. Alternatively, you can have the app send the endpoint ARN directly to your backend, though I think it might be a bit less secure. Make sure you give IAM permissions to publish to your app via SNS. You can use the lambda below to push the message:

var AWS = require('aws-sdk');
var sns = new AWS.SNS({apiVersion: '2010-03-31'});

exports.handler = (event, context, callback) => {

    console.log(JSON.stringify(event))

var payload = {
"default": "The message string.",
"GCM":"{"+
   "\"notification\":{"+
       "\"body\":\"PUT NOTIFICATION BODY HERE\","+
       "\"title\":\"PUT NOTIFICATION TITLE HERE\""+
   "}"+
"}"
};

  payload = JSON.stringify(payload);

    var params = {
      TargetArn: 'PUT THE ENDPOINT ARN HERE',
      Subject: 'foo2',
      MessageStructure: 'json',
      Message: payload
    }
    sns.publish(params, function(err, data) {
      if (err) console.log(err, err.stack); // an error occurred
      else     console.log(data);           // successful response
    });

};

Upvotes: 0

adamkonrad
adamkonrad

Reputation: 7122

Yes, you can use AWS Lambda to achieve what you want. You also need to give proper IAM Permissions allowing your Lambda IAM Role to publish messages to you SNS Topic.

Example SNS Publish IAM Policy:

{
    "Statement":[ {
        "Effect":"Allow",
        "Action":"sns:Publish",
        "Resource":"arn:aws:sns:*:<your account id>:<your topic id>"
    } ]
}

Upvotes: 1

Mircea
Mircea

Reputation: 10566

There is nothing special about pushing SNS notifications in the context of Lambda. I would think of it as just another external service that you interact with.

What you could do is pull in the AWS SDK in your lambda code and after that use the code to make the SNS calls. You will need to inject the right credentials to be able to call the Amazon SNS API (but you probably do something similar for getting the database endpoint and credentials if you are talking to the database)

Upvotes: 2

Related Questions