Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Lambdaを使ってJawsDaysをHackしてみよう!
Search
toshihirock
March 12, 2016
2
1.8k
Lambdaを使ってJawsDaysをHackしてみよう!
Lambdaを使ってJawsDaysをHackしてみよう!
2016 3/12 13:00-13:50(50min)
toshihirock
March 12, 2016
Tweet
Share
More Decks by toshihirock
See All by toshihirock
保守、運用で使ってみようLambda!
toshihirock
4
1.8k
CodeDeployを使ったアプリケーションデプロイを考える
toshihirock
0
650
Jenkinsおじさんと楽しい連携ツールたち
toshihirock
1
2.4k
Featured
See All Featured
The Cult of Friendly URLs
andyhume
78
6.2k
Let's Do A Bunch of Simple Stuff to Make Websites Faster
chriscoyier
507
140k
The Power of CSS Pseudo Elements
geoffreycrofte
74
5.4k
Cheating the UX When There Is Nothing More to Optimize - PixelPioneers
stephaniewalter
280
13k
Building Flexible Design Systems
yeseniaperezcruz
328
38k
Testing 201, or: Great Expectations
jmmastey
41
7.2k
Being A Developer After 40
akosma
89
590k
A Philosophy of Restraint
colly
203
16k
Chrome DevTools: State of the Union 2024 - Debugging React & Beyond
addyosmani
3
260
Scaling GitHub
holman
459
140k
The Straight Up "How To Draw Better" Workshop
denniskardys
232
140k
Music & Morning Musume
bryan
46
6.3k
Transcript
LambdaΛͬͯJawsDays ΛHackͯ͠ΈΑ͏! +"84%":4 5PTIJIJSP'VSVOP
ɾWiFi SSID->jawsdays_workshop ɾWiFi ύεϫʔυ->workshop02 ɾࢿྉ-> https://github.com/toshihirock/ jaws2016-lambda-hands-on
લఏ ɾॳ৺ऀ͚ͷߨशͳͷͰқߴ͋͘Γ ·ͤΜɻ͕࣌ؒ༨ͬͨํվ͢ΔͳͲࣗ༝ ʹ༡ΜͰΈ͍ͯͩ͘͞ɻ ɾLambdaͷίʔυ࣮αϯϓϧNode.jsͰ ߦ͓ͬͯΓ·͢ɻJava,PythonͰͷαϯϓϧ ͳ͍ྃ͝ঝ͍ͩ͘͞ɻ
Δ͜ͱɺΒͳ͍͜ͱ Δ͜ͱ ɾϋϯζΦϯܗࣜͰLambdaͰԿ͔Λ࡞ͬͯ ༡ΜͰΈΔ(༡ͼͳͷͰۀ͚Ͱͳ͍) Βͳ͍͜ͱ ɾLambdaͷৄࡉͳઆ໌ ɾTwitterAPIͷৄࡉ
ࣗݾհ ɾݹढ़(@toshihirock) ɾNTTιϑτΣΞגࣜձࣾ ɾAWSۀ1͙Β͍ ɾ͖ - Lambda - Podcast -
ϫʔϧυτϦΨʔ
ຊͷϋϯζΦϯ
-BNCEBΛͬ ͯKBXTEBZTʹπ Πʔτ͠Α͏
LambdaΛͬͯJawsDaysΛHackͬͧ͢ ɾπΠʔτ͍ͤͨ͞༰ͷςΩετΛS3ʹஔ ɾIAMϩʔϧΛ༩ͨ͠LambdaͰS3͔ΒςΩ ετϑΝΠϧΛऔಘ͠ɺϥϯμϜʹϐοΫΞοϓ ɾ༧Ί࡞ͯ͋͠ΔAPIGatewayʹ͚ͯPOST ϦΫΤετΛ࣮ࢪ ɾAPIGatewayܦ༝Ͱ@jaws2016_lambda͔Β ϋογϡλά #jawsdays Λ͚ͯπΠʔτ͞Ε
Δ(࡞ࡁΈ)
֓ཁਤ
ࣝฤ
S3? ɾΦϯϥΠϯετϨʔδ ɾߴ͍ݎ࿚ੑ(99.999999999%) ɾ҆Ձ ɾ༰ྔແ੍ݶ(1ϑΝΠϧ5TB·Ͱ) ࠓճπΠʔτจݴΛஔ͘ॴͱͯ͠ར༻
Lambda? ɾΠϕϯτυϦϒϯ cronͷΑ͏ʹఆظλΠϛϯάͰϢʔβʔ͕ॻ ͍ͨίʔυΛ࣮ߦͰ͖Δ ɾίʔυNode.js,Java,Python ɾ࠷ۙVPCରԠ ɾৄࡉެࣜࢀর
IAMϩʔϧ? ɾϢʔβʔLambdaɺEC2ͳͲͷϦιʔεʹର ׂͯ͠(Role)ΛࢦఆͰ͖Δɻྫ͑ʮS3ͷRead ݖݶʯͳͲ ɾϩʔϧLambda࡞࣌ʹ༩͢Δ͜ͱͰઃఆ ͞ΕͨAPIͷkick͕Ͱ͖Δ ࠓճͷྫͰLambda͔ΒS3ͷόέοτͷใΛ औಘ͍ͨ͠ͷͰLambda͚ͷ্هʹ֘͢Δ IAMϩʔϧΛ࡞͢Δ
࡞ۀͷྲྀΕ ɾS3όέοτʹπΠʔτจݴͷஔ ɾLambda༻IAMϩʔϧͷ࡞ ɾLambda Functionͷ࡞ ɾLambda Functionͷఆظ࣮ߦͷઃఆ
४උฤ(S3)
αʔϏεҰཡ͔ΒS3ͷબ
όέοτͷ࡞ ʮ$SFBUF#VDLFUʯ Λબ όέο τ໊Λ࡞ɻόέοτ ໊SFHJPOͰϢχʔΫͳ ඞཁ͋Γ Ϧʔδϣϯ 5PLZP
ʮ$SFBUFʯ Λબ
S3ஔ͢ΔςΩετϑΝΠϧͷ࡞ ɾ֤ࣗͷPCͷ͖ͳΤσΟλͰπΠʔτͤ͞ ͍ͨจݴूΛ࡞͍ͯͩ͘͠͞ɻจࣈίʔυ UTF-8Ͱอଘ ɾҙͷσϦϛλ۠ΓͰ࡞͍ͯͩ͘͠͞ɻ ҎԼྫσϦϛλվߦ(\r\n) ɾ࡞ޙɺʮtweetList.txtʯͰϩʔΧϧʹอ ଘ
֬ೝ # จࣈίʔυ֬ೝ $nkf -g tweetList.txt # վߦίʔυ֬ೝ.LF = $.CR
= ^MͰදࣔ $cat -e tweetList.txt
S3ςΩετϑΝΠϧͷΞοϓϩʔυ ʮ6QMPBEʯΛ બ ςΩετΛ υϥοάΞϯυυϩο ϓͰΞοϓϩʔυ ʮ4UBSU 6QMPBEʯΛબ ઌ΄Ͳ࡞ͨ͠όέοτΛબޙɺҎԼͷૢ ࡞ͰΞοϓϩʔυ
֬ೝ
४උฤ(IAMϩʔϧ)
αʔϏεҰཡ͔ΒIAMͷબ
ϩʔϧͷ৽ن࡞ ʮ3PMFTʯΛબ ʮ$SFBUF /FX3PMFʯΛબ
ϩʔϧ໊ͷܾఆ ҙͷ 3PMF/BNFΛೖྗ ʮ/FYU 4UFQʯΛબ
ϩʔϧλΠϓͷબ ʮ"84 -BNCEBʯΛબ LambdaʹΞλον͍ͨ͠ϩʔϧΛ࡞͢ΔͷͰLambdaΛ બ͠·͢
ϙϦγʔͷબ ʮTʯͱೖྗ ʮ"NB[PO43F BE0OMZ"DDFTTʯ Λબ ʮ/FYU 4UFQʯΛબ طʹAWSଆͰ༻ҙ͍ͯ͠ΔS3ͷReadOnly ͷϙϦγʔΛ༩
ϨϏϡʔ ֬ೝޙɺ ʮ$SFBUF3PMFʯ Λબ
LambdaΛ࡞Ζ͏!
(ޙͰར༻)ίʔυͷ४උ ɾҎԼͷ༰Λίϐϖͯ͠ҙͷςΩετΤ σΟλʹష͓ͬͯ͘ ɾhttps://github.com/toshihirock/jaws2016- lambda-hands-on/blob/master/index.js
(ޙͰར༻)ίʔυͷ४උ 4ͷઃఆ దٓࣗͷઃఆʹ มߋ "1*(BUFXBZͷ ೝূΩʔΛࢦఆ
αʔϏεҰཡ͔ΒLambdaͷબ
Functionͷ࡞ ※ॳΊͯͷํͷը໘ ʮ$SFBUFB -BNCEBGVODUJPOʯ Λબ ʮ(FU4UBSUFE /PXʯΛબ ※طʹFunction͕͋Δਓͷը໘
Blueprint ར༻͠ͳ͍ ͷͰʮ4LJQʯΛબ
Functionͷઃఆ(1/2) /PEFKT /BNFٴͼ %FTDSJQUJPOԿ Ͱ0, &EJUDPEF JOMJOF ઌ ΄Ͳͷ༰Λ ίϐϖ
Functionͷઃఆ(2/2) ઌ ΄Ͳ࡞ͨ͠4 ΞΫηεͰ͖Δ3PMF ໊ *OEFYIBO EMFS /P71$ ࠷ޙʹ ʮ/FYUʯ
ϨϏϡʔ ઃఆ͕ͳ ͚Εʮ$SFUF GVODUJPOʯ
࡞ ຊޠ͕จࣈԽ͚͠· ͢ɻɻؾʹͳΔਓຊϖʔδ Ͱ࠶ίϐϖͯ͠ߋ৽͢Δͱ Γ·͢ɻ
ςετͯ͠ΈΔ ʮ5FTUʯΛબ
ςετͯ͠ΈΔ ʮ4BWFBOE UFTUʯΛબ ςϯϓϨʔτԿͰ 0, πΠʔτใ4͔ Βऔಘ͢ΔͷͰ
ςετͯ͠ΈΔ ޭ͢Εը໘Լ෦ ʹπΠʔτͨ͠จࣈྻ͕දࣔ ͞ΕΔ ࣦഊ͢Δͱ͍Τϥʔදࣔ ʹͳΔ
ςετͯ͠ΈΔ 5XJUUFSͰ֬ೝ
Τϥʔ͕ൃੜ͍ͯ͠Δ߹ ɾը໘Լ෦(Log output)ʹϩάใ͕දࣔ͞ ΕΔͷͰ֬ೝ ɾඞཁʹԠͯ͡console.logͳͲΛࠐΜͰσ όοάͯ͠ΈΔ
botΒ͘͠ ఆظ࣮ߦͤͯ͞ΈΔ
ఆظ࣮ߦͷઃఆ ʮ&WFOUTPVSDFTʯλ ϒΛબ ʮ"EEFWFOU TPVSDFʯΛબ
ఆظ࣮ߦͷઃఆ ҙͷ࣌ؒؒ ִΛઃఆ ࠷খ ʮ$MPVE8BUDI&WFOUT 4DIFEVMFʯΛબ ໊শΛదٓมߋ 4VCNJUܾͯ͠ఆ
֬ೝ 5XJUUFS্Ͱ֬ೝ ͬͯ֬͠͠ೝ
͕࣌ؒ༨ͬͨਓ
͕࣌ؒ༨ͬͨਓ ɾ͖ʹ৭ʑͬͯΈ͍ͯͩ͘͞ʂʢఆظ࣮ߦ ִؒΛม͑ΔɺจݴͷύλʔϯΛ૿͢ͳͲʣ ɾLambda͔ΒTwitterͷAPIΛୟ͘ͳͲΛ ͯ͠Έ͍ͨ߹Ҏલॻ͍ͨͪ͜ΒΛࢀߟʹͬ ͯΈ͍ͯͩ͘͞ http://qiita.com/toshihirock/items/ d28505442526e0ae7793
Thanks!