1
0

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 1 year has passed since last update.

AWS LambdaからTwilio Functionsに値を渡す

Last updated at Posted at 2022-03-04

これはなに?

AWSLambdaからTwilio Fnctionsへ値を渡すときに使用した例です。

例えばですが、CloudWatchAlarmの発生をトリガーにLambdaが動き、
LambdaからTwilio Functionsが呼び出されSMSの通知内容でアラート名をお知らせするなど行えると思います。

Lambda側

下記にて紹介されているコードを転用して作成しております。
今回はCloudWatchAlarmよりアラートが上がった際のアラート名を連携するようにしています。

index.js
exports.handler = function (event, context, callback) {
  const crypto = require('crypto');
  const https = require('https');

  // Twilio Account Sid and Auth Token
  const authToken = process.env.TWILIO_AUTH_TOKEN;

  // Twilio Function Domain
  let functionDomain = process.env.FUNCTION_DOMAIN;

  // Make a X-Twilio-Signature
  const signature = crypto.createHmac('sha1', authToken)
    .update(new Buffer.from(`https://${functionDomain}/serial-call-studio`, 'utf-8'))
    .digest('Base64');

  const message = JSON.parse(event.Records[0].Sns.Message);
  const alarmName = message.AlarmName;

  const data = JSON.stringify({
    alarm_name: alarmName,
  });

  // HTTPS Options
  const options = {
    method: "POST",
    hostname: functionDomain,
    port: 443,
    path: '/serial-call-studio',
    headers: {
      'X-Twilio-Signature': signature
    }
  };

  // Function request
  const req = https.request(options, (res) => {
    let result = '';
    res.setEncoding('utf8');
    res.on('data', (chunk) => {
      result = chunk;
    });
    res.on('end', () => {
      callback(null, result);
    })
  });

  // Error handling
  req.on('error', (e) => {
    callback(e);
  });
  req.write(data);
  req.end();
}

Twilio Function側

下記のような形で、event.<パラメーター名>で値を取得することが出来ます。

twilio_function.js
exports.handler = function (context, event, callback) {

  let alarm_name = event.alarm_name;
  console.log(alarm_name)
};

1
0
1

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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?