LoginSignup
1
1

More than 5 years have passed since last update.

Node.js で AWS の SNS にパブリッシュする

Last updated at Posted at 2017-12-08

Node.js で、AWS の SNS にパブリッシュ(投稿)する方法です。
Arch Linux と Raspbian で動作を確認しました。
topic は変更して下さい。

#! /usr/bin/node
// ---------------------------------------------------------------
//  sns_test.js
//
//                  Dec/08/2017
//
// ---------------------------------------------------------------
// exports.send_sns_handler = function(event, context) {
function send_sns_handler (event, context)
{
    console.error ("*** end_sns_handler *** start ***")
    var aws = require('aws-sdk')
    var sns = new aws.SNS({
        apiVersion: '2010-03-31',
        region: 'ap-northeast-1'
    })


    sns.publish({
        Message: event.body,
        Subject: event.subject,
        TopicArn: event.topic
        }, function(err, data){
        if ( err )
            {
            console.error ("*** end_sns_handler *** error ***")
            context.fail('fail')
            }
    })  
}

// ---------------------------------------------------------------
console.error ("*** 開始 ***")

const event = {
    "topic": 'arn:aws:sns:ap-northeast-1:999999999999:test_sns',
    "subject": "This is the subject of the message. Dec/08/2017",
    "body": "This is the body of the message. Dec/08/2017 PM 13:29"
}

var context=""

send_sns_handler(event,context)

console.error ("*** 終了 ***")
// ---------------------------------------------------------------

python3 のサンプルはこちらです。
python で AWS の SNS にパブリッシュする

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1