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
25
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
350
Starting Python
kawaken
0
28
Goの時刻に関するテスト/Testing time in Go
kawaken
0
27
goaのdesignをラクに書く/Write goa design easily
kawaken
0
25
Goでのチーム開発とコード管理の悩み/Team development and code management in Go
kawaken
0
31
Pyvmomiとansibleのdynamic inventory
kawaken
0
33
Other Decks in Programming
See All in Programming
Crafting Cross-Platform Adventures: Building a Game Engine with Kotlin Multiplatform
dwursteisen
0
220
Modernisation Progressive d’Applications PHP
hhamon
0
110
Prompt Cachingは本当に効果的なのか検証してみた.pdf
ttnyt8701
0
540
The Sequel to a Dream of Ruby Parser's Grammar
ydah
1
220
『ドメイン駆動設計をはじめよう』中核の業務領域
masuda220
PRO
5
1k
Amazon BedrockでサーバレスなAIお料理ボットを作成する!!
tosuri13
0
230
Understand the mechanism! Let's do screenshots tests of Compose Previews with various variations / 仕組みから理解する!Composeプレビューを様々なバリエーションでスクリーンショットテストしよう
sumio
3
870
ECMAScript、Web標準の型はどう管理されているか / How ECMAScript and Web standards types are maintained
petamoriken
3
390
Using Livebook to build and deploy internal tools @ ElixirConf 2024
hugobarauna
0
250
オートマトン学習しろ / Do automata learning
makenowjust
3
130
watsonx.ai Dojo #2 生成AIを使ったアプリ開発入門編
oniak3ibm
PRO
0
240
Go1.23で入った errorsパッケージの小さなアプデ
kuro_kurorrr
2
400
Featured
See All Featured
Fireside Chat
paigeccino
31
2.9k
The Invisible Customer
myddelton
119
13k
WebSockets: Embracing the real-time Web
robhawkes
59
7.3k
For a Future-Friendly Web
brad_frost
174
9.3k
Designing for humans not robots
tammielis
248
25k
Being A Developer After 40
akosma
84
590k
StorybookのUI Testing Handbookを読んだ
zakiyama
26
5.1k
What the flash - Photography Introduction
edds
67
11k
Refactoring Trust on Your Teams (GOTO; Chicago 2020)
rmw
29
2.6k
Become a Pro
speakerdeck
PRO
22
4.9k
Agile that works and the tools we love
rasmusluckow
327
20k
Building a Scalable Design System with Sketch
lauravandoore
458
32k
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Λ͏