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.7k
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.7k
CodeDeployを使ったアプリケーションデプロイを考える
toshihirock
0
630
Jenkinsおじさんと楽しい連携ツールたち
toshihirock
1
2.3k
Featured
See All Featured
How to name files
jennybc
75
98k
Raft: Consensus for Rubyists
vanstee
135
6.5k
Embracing the Ebb and Flow
colly
83
4.4k
Designing on Purpose - Digital PM Summit 2013
jponch
113
6.8k
Six Lessons from altMBA
skipperchong
26
3.3k
YesSQL, Process and Tooling at Scale
rocio
167
14k
Building Adaptive Systems
keathley
36
2.1k
RailsConf & Balkan Ruby 2019: The Past, Present, and Future of Rails at GitHub
eileencodes
131
32k
Fantastic passwords and where to find them - at NoRuKo
philnash
48
2.8k
The Cult of Friendly URLs
andyhume
76
6k
GraphQLの誤解/rethinking-graphql
sonatard
65
9.8k
A Modern Web Designer's Workflow
chriscoyier
691
190k
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!