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
31
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
380
Starting Python
kawaken
0
32
Goの時刻に関するテスト/Testing time in Go
kawaken
0
36
goaのdesignをラクに書く/Write goa design easily
kawaken
0
27
Goでのチーム開発とコード管理の悩み/Team development and code management in Go
kawaken
0
34
Pyvmomiとansibleのdynamic inventory
kawaken
0
38
Other Decks in Programming
See All in Programming
シェーダーで魅せるMapLibreの動的ラスタータイル
satoshi7190
1
480
3 Effective Rules for Using Signals in Angular
manfredsteyer
PRO
1
100
距離関数を極める! / SESSIONS 2024
gam0022
0
280
Less waste, more joy, and a lot more green: How Quarkus makes Java better
hollycummins
0
100
AI時代におけるSRE、 あるいはエンジニアの生存戦略
pyama86
6
1.1k
Generative AI Use Cases JP (略称:GenU)奮闘記
hideg
1
280
AWS Lambdaから始まった Serverlessの「熱」とキャリアパス / It started with AWS Lambda Serverless “fever” and career path
seike460
PRO
1
250
2024/11/8 関西Kaggler会 2024 #3 / Kaggle Kernel で Gemma 2 × vLLM を動かす。
kohecchi
5
910
とにかくAWS GameDay!AWSは世界の共通言語! / Anyway, AWS GameDay! AWS is the world's lingua franca!
seike460
PRO
1
850
Snowflake x dbtで作るセキュアでアジャイルなデータ基盤
tsoshiro
2
520
現場で役立つモデリング 超入門
masuda220
PRO
15
3.2k
ふかぼれ!CSSセレクターモジュール / Fukabore! CSS Selectors Module
petamoriken
0
150
Featured
See All Featured
Facilitating Awesome Meetings
lara
50
6.1k
How To Stay Up To Date on Web Technology
chriscoyier
788
250k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
26
2.1k
Scaling GitHub
holman
458
140k
[Rails World 2023 - Day 1 Closing Keynote] - The Magic of Rails
eileencodes
33
1.9k
Writing Fast Ruby
sferik
627
61k
The Myth of the Modular Monolith - Day 2 Keynote - Rails World 2024
eileencodes
16
2.1k
Building Flexible Design Systems
yeseniaperezcruz
327
38k
The Invisible Side of Design
smashingmag
298
50k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
Into the Great Unknown - MozCon
thekraken
32
1.5k
Designing on Purpose - Digital PM Summit 2013
jponch
115
7k
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Λ͏