1

I have created a Lambda function (Node.js 12.x) in AWS so SNS messages are pushed to Slack from our ETL tool Matillion.

console.log('Loading function');

const https = require('https');
const url = require('url');
const slack_url = 'https://hooks.slack.com/services/T1MJBHQ95/B01DQ60NUR2/....';
const slack_req_opts = url.parse(slack_url);
slack_req_opts.method = 'POST';
slack_req_opts.headers = { 'Content-Type': 'application/json' };

exports.handler = function (event, context) {
        (event.Records || []).forEach(function (rec) {
                if (rec.Sns) {
                    var req = https.request(slack_req_opts, function (res) {
                        if (res.statusCode === 200) {
                            context.succeed('sns posted to slack');
                        }
                        else {
                            context.fail('status code: ' + res.statusCode);
                        }
                    });

                    req.on('error', function (e) {
                        console.log('problem with request: ' + e.message);
                        context.fail(e.message);
                    });

                    req.write(JSON.stringify({ text: `${rec.Sns.Message}` }));

                        req.end();
                    }
                });
        };

The function will fail with a missing ) after argument list syntax error. I run it thru a linter in Sublime and it throws an error on require and exports being undefined.

My research shows several challenges:

  • I may need a file called eslint.rc but I am unclear why I need to put in.
  • The use of "require" and "exports" appears deprecated.

Can someone please give me pointers how what to focus on to resolve this? Thank you.

4
  • 1
    this works locally ? if possible add full trace of error which you can find out in CloudWatch log or lambda execution log Commented Nov 8, 2020 at 14:56
  • It does not work locally. "Require" and "Exports" are server-based. Commented Nov 8, 2020 at 16:33
  • @user14579431 What do you mean "require and exports are server based""? There is no reason you couldn't test this code on your local computer. Commented Nov 8, 2020 at 17:15
  • Error require is not defined. (no-undef) and exports is not defined. (no-undef) are the errors I get when I run my code locally. That's why I assumed both functions are server-based. Commented Nov 8, 2020 at 17:32

3 Answers 3

3

You made a syntax mistake in your code.

req.write(JSON.stringify({ text: `${rec.Sns.Message}` }));
req.end();

Need to add ) before ; in your req.write() statement.

Sign up to request clarification or add additional context in comments.

1 Comment

Fixed typo (thank you). Now I get a new error message SyntaxError: missing ) after argument list.
1

Just use Axios, it will make your life easier:

console.log('Loading function');

const axios = require('axios');
const url = require('url');
const slack_url = 'https://hooks.slack.com/services/T1MJBHQ95/B01DQ60NUR2/....';

exports.handler = async (event, context) {
   (event.Records || []).forEach(function(rec) {
      if (rec.Sns) {
         axios.post(slack_url, {
            text: rec.Sns.Message
         }, {
            'Content-Type': 'application/json'
         }).done(r => {
           context.succeed('sns posted to slack');
         }).catch(e => {
           context.fail('status code: ' + e.statusCode);
     });
});

Notice that in your code, after 1 sns event he end the invocation, not running all, to run all of them you need to wait for all requests to be done (can use Promise.all for example)

Also, be aware that sns can send directly to HTTPS without any code https://aws.amazon.com/sns/features/

By the way, you can use an external monitoring tool that will show you exactly the outgoing request + all payload as Lumigo (As a disclaimer I work for a company named lumigo)

1 Comment

Your suggestion fails to run in AWS with a errorMessage:SyntaxError: missing ) after argument list error message.
-1

I fixed my issue:

  1. I added the following statement at the top of my file /* eslint-env node, common */
  2. I re-deployed my function.

I believe the redeployment of the function did the trick.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.