Upgrade to Pro — share decks privately, control downloads, hide ads and more …

AWS Lambda と API GatewayでRails使わずに済んだ話

Affc58cd0f63630fb0c5bcec49902546?s=47 ryonext
November 19, 2015

AWS Lambda と API GatewayでRails使わずに済んだ話

Affc58cd0f63630fb0c5bcec49902546?s=128

ryonext

November 19, 2015
Tweet

Transcript

  1. "84-BNCEBͱ"1* (BUFXBZͰ3BJMT࢖Θͣʹࡁ Μͩ࿩ !SZPOFYU

  2. ࣗݾ঺հ w 5XJUUFS!SZPOFYU w 3BJMTͰ͓࢓ࣄͯ͠ΔήʔϚʔܥΤϯδχΞͰ͢ w 4QMBUPPO4 ʹͳΔͷ͸ఘΊ·ͨ͠ w ࣍͸ϞϯϋϯΫϩεͩʂ

  3. ൃ୺ w ੩తϖʔδͰ໰͍߹ΘͤϑΥʔϜ͚ͩཉ͍͠ͱ ͍͏ཁ͕݅͋ͬͨ w NBJM@UP (PPHMF'PSN͸/( w ໰͍߹ΘͤϑΥʔϜͷೖྗޙɺϢʔβʹࣗಈ ฦ৴ͱӡӦଆʹ໰͍߹Θͤ಺༰ΛૹΔ

  4. ͜Ε͚ͩͰ8FC ΞϓϦԽͭΒ͍ w ͜ͷཁ͕݅ͳ͚Ε͹4Ͱ੩తϗεςΟϯάՄೳ w &$ཱͯͯɺσϓϩΠ؀ڥ࡞ͬͯɺ؂ࢹ͠ ͯɾɾɾͱ͔΍Δ͜ͱʹͳΔ ʋТA

  5. ͦ͜Ͱ"1*(BUFXBZͰ͢Α w "1*(BUFXBZͰϦΫΤετΛड͚ͯ w -BNEBͰ4&4Λར༻ͯ͠ϝʔϧΛૹΔ

  6. "84-BNEBͱ͸ w "84͕ΘΓͱ࠷ۙ࢝ΊͨαʔϏεͰɺಛఆͷε ΫϦϓτΛ͋ΔΠϕϯτʹԠͯ͡૸ΒͤΔͱ͍ ͏͜ͱ͕Ͱ͖Δɻ w ࠷ۙɺ"1*(BUFXBZ͕Ͱ͖ͯɺ૊Έ߹ΘͤΔ͜ ͱͰʮಛఆͷϦΫΤετΛड͚ͨΒ-BNEBΛ૸ ΒͤΔʯͱ͍͏"1*αʔόͷ໾ׂ͕Ͱ͖ΔΑ͏ʹ ͳͬͨɻ

  7. ࠓճͷ࢖༻ࣄྫ w ৽ن࡞੒NJDSPTFSWJDFIUUQFOEQPJOU QZUIPOΛ࢖༻ɻ w طଘͷίʔυ͸"NB[PO%ZOBNP%#ʹೖΕΔ ίʔυʹͳ͍ͬͯΔ΋ͷΛେ͖͘มߋ͠ɺҎԼ ͷΑ͏ʹमਖ਼ɻ

  8. 2 import boto3 3 import json 4 5 OUR_EMAIL =

    "ӡӦαΠυͷϝʔϧΞυϨε" 6 7 def send_email(to, reply, subject, body): 8 client = boto3.client('ses', region_name='us-east-1') 9 10 response = client.send_email( 11 Source=OUR_EMAIL, 12 Destination={ 13 'ToAddresses': [ 14 to, 15 ] 16 }, 17 Message={ 18 'Subject': { 19 'Data': subject, 20 }, 21 'Body': { 22 'Text': { 23 'Data': body, 24 }, 25 } 26 }, 27 ReplyToAddresses=[ 28 reply, 29 ], 30 ReturnPath=OUR_EMAIL 31 ) 32 33 def send_inquiry_mail(email, message): 34 send_email(OUR_EMAIL, email, "໰͍߹Θ໊ͤ݅", message) 35 36 def send_auto_reply_mail(name, email): 37 message = "ࣗಈฦ৴" 38 send_email(email, OUR_EMAIL, "ࣗಈฦ৴໊݅", message) 39 40 def lambda_handler(event, context): 41 email = event["email"] 42 message = event["message"] 43 send_inquiry_mail(email, message) 44 send_auto_reply_mail(email)
  9. w MBNCEB@IBOEMFS͕ϦΫΤετͷड͚෇͚Λߦ͏ DPOUSPMMFSʹͳΓɺFWFOUͷதʹSFRVFTU QBSBNFUFS͕ೖ͍ͬͯΔɻ

  10. *".ʹΑΔαʔϏεར༻ڐՄ w -BNEB͔Β4FTΛར༻Մೳʹ͢ΔͨΊʹɺ*". SPMFΛఆٛ͢Δඞཁ͕͋Δɻ

  11. { "Version": "2012-10-17", "Statement": [ { "Effect": "Allow", "Action": [

    "logs:CreateLogGroup", "logs:CreateLogStream", "logs:PutLogEvents" ], "Resource": "arn:aws:logs:*:*:*" }, { "Effect": "Allow", "Action": [ "ses:*" ], "Resource": "*" } ] }
  12. "1*(BUFXBZ w ࡞੒ͨ͠-BNEBͷϖʔδ͔ΒFOEQPJOU͕௥ՃՄ ೳɻ

  13. None
  14. 4FDVSJUZ w "84*". w 0QFO w 0QFOXJUIBDDFTTLFZ w ͕͋Δɻ*".͸ಛఆͷΠϯελϯε͔Βݺ͹ΕΔ༻్ͷ "1*ʹ࢖͏΋ͷͩΖ͏ɻ0QFOXJUIBDDFTTLFZ͸ɺΩʔ

    ΛຒΊࠐΜͩಛఆͷΫϥΠΞϯτ͔Βݺ͹ΕΔ΋ͷͳͷ ͰεϚϗΫϥΠΞϯτΞϓϦʹ࢖͏΋ͷͩͱࢥΘΕΔɻ
  15. "1*(BUFXBZ࡞੒ը໘

  16. w .PEFMͷ࡞੒ͳͲ΋Ͱ͖Δ͕ɺࠓճͷൣғͰ͸ ಛʹඞཁͳ͔ͬͨɻ

  17. ஫ҙ఺ w +BWBTDSJQU͔ΒݺͿ৔߹͸ΫϩεΦϦδϯͷ੒ ໿ʹҾ͔͔ͬΔͷͰରॲ͕ඞཁ w "NB[PO"1*(BUFXBZΛΫϩεΦϦδϯͰݺ ͼग़͢ $034 ʛ%FWFMPQFST*0IUUQ EFWDMBTTNFUIPEKQDMPVEBXTBNB[POBQJ

    HBUFXBZDPST