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
56
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
410
Starting Python
kawaken
0
56
Goの時刻に関するテスト/Testing time in Go
kawaken
0
62
goaのdesignをラクに書く/Write goa design easily
kawaken
0
58
Goでのチーム開発とコード管理の悩み/Team development and code management in Go
kawaken
0
56
Pyvmomiとansibleのdynamic inventory
kawaken
0
64
Other Decks in Programming
See All in Programming
小さく段階的リリースすることで深夜メンテを回避する
mkmk884
2
130
Go1.24で testing.B.Loopが爆誕
kuro_kurorrr
0
160
Windows版PHPのビルド手順とPHP 8.4における変更点
matsuo_atsushi
0
370
Devinのメモリ活用の学びを自社サービスにどう組み込むか?
itarutomy
0
1.7k
Modern Angular:Renovation for Your Applications @angularDays 2025 Munich
manfredsteyer
PRO
0
140
Compose Navigation実装の見通しを良くする
hiroaki404
0
180
CRE Meetup!ユーザー信頼性を支えるエンジニアリング実践例の発表資料です
tmnb
0
350
Day0 初心者向けワークショップ実践!ソフトウェアテストの第一歩
satohiroyuki
0
410
php-fpm がリクエスト処理する仕組みを追う / Tracing-How-php-fpm-Handles-Requests
shin1x1
5
820
リアクティブシステムの変遷から理解するalien-signals / Learning alien-signals from the evolution of reactive systems
yamanoku
2
1k
RubyKaigiで手に入れた HHKB Studioのための HIDRawドライバ
iberianpig
0
1k
20250326_生成AIによる_レビュー承認システムの実現.pdf
takahiromatsui
17
5.5k
Featured
See All Featured
Rebuilding a faster, lazier Slack
samanthasiow
80
8.9k
How to Ace a Technical Interview
jacobian
276
23k
10 Git Anti Patterns You Should be Aware of
lemiorhan
PRO
656
60k
Art, The Web, and Tiny UX
lynnandtonic
298
20k
Being A Developer After 40
akosma
90
590k
Templates, Plugins, & Blocks: Oh My! Creating the theme that thinks of everything
marktimemedia
30
2.3k
Creating an realtime collaboration tool: Agile Flush - .NET Oxford
marcduiker
28
2k
Improving Core Web Vitals using Speculation Rules API
sergeychernyshev
12
610
Java REST API Framework Comparison - PWX 2021
mraible
29
8.5k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
231
53k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5.4k
Helping Users Find Their Own Way: Creating Modern Search Experiences
danielanewman
29
2.5k
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Λ͏