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
62
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
420
Starting Python
kawaken
0
61
Goの時刻に関するテスト/Testing time in Go
kawaken
0
64
goaのdesignをラクに書く/Write goa design easily
kawaken
0
63
Goでのチーム開発とコード管理の悩み/Team development and code management in Go
kawaken
0
62
Pyvmomiとansibleのdynamic inventory
kawaken
0
69
Other Decks in Programming
See All in Programming
Ruby's Line Breaks
yui_knk
3
2k
API for docs
soutaro
3
1.5k
監視 やばい
syossan27
11
10k
ComposeでのPicture in Picture
takathemax
0
130
VitestのIn-Source Testingが便利
taro28
8
2.3k
Bedrock × Confluenceで簡単(?)社内RAG
iharuoru
1
100
Unlock the Potential of Swift Code Generation
rockname
0
280
Being an ethical software engineer
xgouchet
PRO
0
220
Boost Your Performance and Developer Productivity with Jakarta EE 11
ivargrimstad
0
470
エンジニアが挑む、限界までの越境
nealle
1
290
MCP調べてみました! / Exploring MCP
uhzz
2
2.3k
State of Namespace
tagomoris
5
2.2k
Featured
See All Featured
The Invisible Side of Design
smashingmag
299
50k
Gamification - CAS2011
davidbonilla
81
5.2k
Fight the Zombie Pattern Library - RWD Summit 2016
marcelosomers
233
17k
Music & Morning Musume
bryan
47
6.5k
A Tale of Four Properties
chriscoyier
158
23k
How GitHub (no longer) Works
holman
314
140k
The Art of Programming - Codeland 2020
erikaheidi
53
13k
Product Roadmaps are Hard
iamctodd
PRO
52
11k
KATA
mclloyd
29
14k
Building Better People: How to give real-time feedback that sticks.
wjessup
367
19k
GitHub's CSS Performance
jonrohan
1030
460k
Exploring the Power of Turbo Streams & Action Cable | RailsConf2023
kevinliebholz
32
5.4k
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Λ͏