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
AWS Lambda + Go
Search
Kentaro Kawano
June 16, 2017
Programming
0
36
AWS Lambda + Go
AWS LambdaでGoを使うに当たっての基本的なことの紹介
Kentaro Kawano
June 16, 2017
Tweet
Share
More Decks by Kentaro Kawano
See All by Kentaro Kawano
Goでのレシーバーとnilの関係 / Receiver and nil relationship in Go
kawaken
0
390
Starting Python
kawaken
0
39
Goの時刻に関するテスト/Testing time in Go
kawaken
0
42
goaのdesignをラクに書く/Write goa design easily
kawaken
0
33
Goでのチーム開発とコード管理の悩み/Team development and code management in Go
kawaken
0
38
Pyvmomiとansibleのdynamic inventory
kawaken
0
46
Other Decks in Programming
See All in Programming
採用事例の少ないSvelteを選んだ理由と それを正解にするためにやっていること
oekazuma
2
1k
선언형 UI에서의 상태관리
l2hyunwoo
0
140
ドメインイベント増えすぎ問題
h0r15h0
1
100
たのしいparse.y
ydah
3
120
Webエンジニア主体のモバイルチームの 生産性を高く保つためにやったこと
igreenwood
0
330
バグを見つけた?それAppleに直してもらおう!
uetyo
0
170
fs2-io を試してたらバグを見つけて直した話
chencmd
0
220
SymfonyCon Vienna 2025: Twig, still relevant in 2025?
fabpot
3
1.2k
42 best practices for Symfony, a decade later
tucksaun
1
180
あれやってみてー駆動から成長を加速させる / areyattemite-driven
nashiusagi
1
200
useSyncExternalStoreを使いまくる
ssssota
6
1k
CSC305 Lecture 25
javiergs
PRO
0
130
Featured
See All Featured
How to train your dragon (web standard)
notwaldorf
88
5.7k
Building a Modern Day E-commerce SEO Strategy
aleyda
38
7k
Rails Girls Zürich Keynote
gr2m
94
13k
Building Adaptive Systems
keathley
38
2.3k
Documentation Writing (for coders)
carmenintech
66
4.5k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
28
4.3k
Building a Scalable Design System with Sketch
lauravandoore
460
33k
Navigating Team Friction
lara
183
15k
A Tale of Four Properties
chriscoyier
157
23k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
How to Think Like a Performance Engineer
csswizardry
22
1.2k
Why You Should Never Use an ORM
jnunemaker
PRO
54
9.1k
Transcript
AWS Lambda + Go 2017/06/16 γφδʔϚʔέςΟϯά(ג) Տ݈ଠ࿕
ࣗݾհ • Տ ݈ଠ࿕ • γφδʔϚʔέςΟϯά(ג) ϓϩμΫτ։ൃ෦ ॴଐ • Synergy!
ͷϚϧννϟωϧରԠ͕ओۀ • ΞϓϦϓογϡج൫ߏங • GoͰߏங → ܰྔԽ͍ͨ͠ • LINE ϝοηʔδϯάج൫ߏங • callback෦ΛGoͰ
AWS Lambda ͓͞Β͍
AWS Lambda ͱ • Function as a Service • AWSͷαʔϏε্ͷΠϕϯτʹରԠ
• ྫ) S3ʹը૾͕Ξοϓϩʔυ͞Εͨͱ͖ɺϦαΠ ζ͢Δ • Node.jsɺJavaɺC#ɺPython͕αϙʔτ͞Ε͍ͯΔ • Lambda Function ͱ͍͏୯ҐͰཧ
Lambda FunctionͷྫʢNode.jsʣ exports.handler = (event, context, callback) => { //
Do Something callback(null, 'Hello from Lambda'); }; // event: Πϕϯτใ // context: ϥϯλΠϜใ // callback: ݺͼग़͠ݩʹใΛฦͨ͢Ίͷؔ
αϙʔτ͞Ε͍ͯͳ͍ͷʹ ͳΜͰGoͰΔͷʁ
ͳΜͰGo? • ඪ४ͷݴޠͱൺֱͯ͠ɺḮ৭ͳ͍ύϑΥʔϚϯε • ࣾͷϥΠϒϥϦ͕༻Ͱ͖Δ • ׳Ε • ίετ
GoͰͰ͖Δ͜ͱGoͰΓ͍ͨʂʂʂ
Ͳ͏ͬͯGoΛ͏͔
ApexΛ༻͢Δ
Apex ͱ • Lambda FunctionΛཧ͢Δπʔϧ • ެࣜҎ֎ͷݴޠαϙʔτ • Functionͷ࣮ʹ͑ΔϥΠϒϥϦఏڙ •
࣮ɺϏϧυɺσϓϩΠΛҰ؏ͯ͠ߦ͑Δ • Node.jsͷhandler + GoͷόΠφϦΛಉࠝͯ͠σϓϩ Π
AWS Lambda ࣮ߦͷΈʢApex+Goʣ 1. Πϕϯτൃੜ 2. Lambda FunctionΛίϯςφ্Ͱىಈ 3. نఆͷhandlerΛݺͼग़͢(Node.js)
4. Node.js͔ΒspawnͰmainΛ࣮ߦ • apexͷhandlerΛ࣮ߦ
Apex جຊίϚϯυ # ϓϩδΣΫτॳظԽ(IAM role, policyͷ࡞) $ apex init #
ϏϧυͷΈ $ apex build > hello.zip # ϏϧυɺσϓϩΠΛ࣮ߦ $ apex deploy # functionͷϦετʢAWS্ͷใ֬ೝͰ͖Δʣ $ apex list
Apex ϑΝΠϧߏ . ├ ─ ─ functions │ ├ ─
─ bot # σΟϨΫτϦ୯ҐͰཧ │ ├ ─ ─ function.json # function͝ͱͷઃఆ │ └ ─ ─ main.go ├ ─ ─ .apexignore # ແࢹ͢ΔϑΝΠϧͷઃఆ └ ─ ─ project.json # ϓϩδΣΫτશମͷઃఆ
project.json { "name": "line-kensho", "description": "lineͷݕূ༻", "memory": 128, "timeout": 5,
"role": "arn:aws:iam::...", "environment": {}, "runtime": "golang" }
function.json { "environment": { "CHANNEL_SECRET": ”...", "CHANNEL_TOKEN": ”..." } }
function.jsonͷํ͕༏ઌ͞ΕΔ
.apexignore • gitignoreͱಉ͡ॻࣜ • Ϗϧυͷࡍʹzipͷରʹ͠ͳ͍ • *.go ͰιʔείʔυΛআ֎
Lambda Function ͷྫʢApex+Goʣ import "github.com/apex/go-apex" func main() { apex.HandleFunc( func(e
json.RawMessage, c *apex.Context) (interface{}, error) { return "Hello from Lambda", nil }) } // json.RawMessage: Πϕϯτใ // apex.Context: ϥϯλΠϜใ // return: ݺͼग़͠ݩʹฦ͢ใ
ҙ
ҙ • ඪ४ग़ྗͷ༰Functionͷ݁Ռͱͯ͠ѻ͏ • ϩάඪ४Τϥʔग़ྗʹ • ΓͷՔಇՄೳ͕࣌ؒऔಘͰ͖ͳ͍ • environment ϚωδϝϯτίϯιʔϧͰؙݟ͑
• ೝূใ͖ͪΜͱཧ͠ͳ͍ͱ…ʢݕ౼தʣ
AWS API Gatewayͱͷ࿈ܞ
API Gatewayͱͷ࿈ܞ • API GatewayͰAPIΛ࡞ • ϦιʔεɺϝιουΛఆٛ • integrationʹLambda FunctionΛࢦఆ
• ProxyϦιʔεͱͯ͠࿈ܞ͢Δ • ϦΫΤετͷใ͕JSONͰͬͯ͘Δ
API Gatewayͱ࿈ܞޙͷ࣮ type Request struct { Body string `json:"body"` Headers
map[string]string `json:"headers"` HTTPMethod string `json:"httpMethod"` Path string `json:"path"` PathParameters map[string]string `json:"pathParameters"` QueryStringParameters map[string]string `json:"queryStringParameters”` // ...ଞ͍Ζ͍Ζ... } func main() { apex.HandleFunc( func(e json.RawMessage, c *apex.Context) (interface{}, error) { // ͜͜ͰϦΫΤετͷใΛ json.UnmarshalͳͲ͢Δඞཁ͕͋Δ => πϥΠ }) }
github.com/fujiwara/ridge
ridge ͱ • ApexΛϥοϓ • net/http.Request, net/http.ResponsWriter͕͑ Δ • logύοέʔδ͕͑Δ
ridgeͰͷ࣮ import "github.com/fujiwara/ridge" func main() { var mux = http.NewServeMux()
mux.HandleFunc("/", handleRoot) ridge.Run(":8080", "/api", mux) } func handleHello(w http.ResponseWriter, r *http.Request) { w.Header().Set("Content-Type", "text/plain") fmt.Fprintf(w, "Hello %s\n", r.FormValue("name")) } // ͍ͭͲ͓Γͷ࣮ͰΒͪ͘Μ
ridgeͰͷҙ • ridge.Run ͷprefixAWS্Ͱແޮ • handlerΛ͢ͱ͖ʹStrip͢Δ // ridge.Run(":8080", ”/callback", handler)
stripped := http.StripPrefix("/callback", handler) ridge.Run(":8080", "", stripped)
·ͱΊ
·ͱΊ • AWS LambdaͰGo͑Δ • ApexΛ͏ • API Gatewayͱ࿈ܞ͢ΔͳΒridgeΛ͏