4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AWS SNSでSMSを送信する

Last updated at Posted at 2019-06-17

やりたかったこと

AWSのLambdaとSNSを連携させて、SMSを送信する。
複数の人に一斉に送信するのではなく、必要な時に一人にだけ送信できれば良い・・・

やってみた

Lambda

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

exports.handler = async (event) => {
    
    try{
        
        let phoneNumber = event.number;
        let message = event.message;
        
        var params = {
            Message: message, /* required */
            PhoneNumber: phoneNumber
        };
        
        let res = await sendSMS(params);
        return res;
        
    }catch(e){
        console.log('ERROR!',e)
        
    }
    
};

const sendSMS = (params) => {
    return new Promise(function (resolve, reject) {
        
        sns.publish(params, function(err, data) {
            if (err){
                console.log('ERROR! : SendSMS')
                console.log(err, err.stack); 
                reject(err)
            }else{
                console.log('SUCCESS! : SendSMS')
                console.log(data);  
                resolve(data)
            }
        });
        
    });
    
}
4
5
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
4
5

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?