Can't create SNS platform endpoint in AWS Lambda

Hi everyone,

I created a simple function that adds a platform endpoint to an AWS SNS mobile platform application.
If I run the function locally like this:

`sls offline start`

it works perfect. From the AWS Dashboard I can see the newly created platform endpoint.
If I instead upload the function to the cloud it doesn’t work. It behaves as if it is just ignoring the method sns.createPlatformEndpoint(params, context.done);

This is my entire lambda function, if it can helps.

module.exports.handler = async (event, context, callback) => {
        let sns = new AWS.SNS({
            region: 'us-east-1',
        });
        let params = {
            PlatformApplicationArn: 'arn:aws:sns:us-east-1:------------:app/---/-----',
            Token: 'e1XYZ...',
        };
        sns.createPlatformEndpoint(params, context.done);

        console.log('context.functionName: ', context.functionName)

        //Sending back response
        // res.status(resCode).json(jsonResponse);
        return {
            statusCode: 200,
            body: JSON.stringify({message: "Created token in mobile platform."})
        }
};

Does anybody of you have an idea of what might be going wrong?

Thanks in advance,
Victor

Can’t say that I’m familiar with your particular use case, but I have come across situations where methods appeared to be ignored entirely.

In my case, it was due to calling an asynchronous method without waiting on it’s response. Here’s a snippet from my app that posts a message to an SNS topic:

I’m not 100% certain what’s going on with createPlatformEndpoint, but could it be the case you’re exiting the method without it finishing execution?

I came across an article that may help: Getting-nodejs-and-lambda-to-play-nicely. I found this snippet may address your particular scenario:

If you test outside of Lambda, these patterns work fine in nodejs, because the default node runtime waits for all tasks to complete before exiting. Context.succeed, context.done, and context.fail however, are more than just bookkeeping – they cause the request to return after the current task completes, even if other tasks remain in the queue. Generally that’s not what you want if those tasks represent incomplete callbacks.

Good luck!

Hi I just read your post and please try with this method

module.exports.handler = async (event) => {
        let sns = new AWS.SNS({
            region: 'us-east-1',
        });
        let params = {
            PlatformApplicationArn: 'arn:aws:sns:us-east-1:------------:app/---/-----',
            Token: 'e1XYZ...',
        };
        await sns.createPlatformEndpoint(params).promise();

        console.log('context.functionName: ', context.functionName)

        //Sending back response
        // res.status(resCode).json(jsonResponse);
        return {
            statusCode: 200,
            body: JSON.stringify({message: "Created token in mobile platform."})
        }
};

I hope this can work for you.

1 Like