LoginSignup
5
2

More than 5 years have passed since last update.

API GatewayとLambdaを使ってTwilioからSMSを送ってみる

Last updated at Posted at 2016-11-23

twilio

事前に

  1. twilioアカウント作成する
  2. 「PROGRAMMABLE SMS」の電話番号を取得する
  3. 「ACCOUNT_SID」「AUTH_TOKEN」「電話番号」をメモる

twilioのライブラリをインストールするよ

mkdir lambda_funcs
cd lambda_funcs
pip install twilio -t .

コードを書くよ

touch sms.py
vi sms.py
sms.py
#coding: UTF-8

from twilio.rest import TwilioRestClient

account_sid = '**********************************'
auth_token  = '********************************'
sms_from    = 'twilioで取得した送信電話番号'

def send(event, context):
    client = TwilioRestClient(account_sid, auth_token)
    message = client.messages.create(
         body = 'Hello World'
        ,to = '自分の電話番号'
        ,from_ = sms_from
    )
    return message.sid

AWS Identity and Access Management

「新しいロールの作成」をするよ

さらに「ポリシーのアタッチ」をするよ

  • AWSLambdaFullAccessをアタッチする

AWS Lambda

「Create a Lambda Function」をするよ

はじめに、ディレクトリの中身をZipで圧縮しておく
(Zipファイル名はなんでもいい)

  • Name: sms
  • Runtime: Python2.7
  • Code entry type: Upload a .ZIP file
  • Handler: sms.send
  • Role: Choose an existing role
  • Existing Role: (AWSLambdaFullAccessがアタッチしてあるロール)

「Add Triger」をするよ

  • API Gatewayを選択

  • API name: LambdaMicroservice

  • Deployment stage: prod

  • Security: Open(とりあえず)

Amazon API Gateway

「APIをデプロイ」するよ

APIをデプロイすると「URL の呼び出し」が確認できる

おわり

https://**********.execute-api.ap-northeast-1.amazonaws.com/prod/{作成したLambdaFunction名}

を、コールすると、自分宛てにSMSが届く

5
2
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
5
2