serverless frameworkで
お手軽Lambda運用
2017.10.14 nseg #93 於 GEEKLAB.NAGANO
自己紹介
にしざわこういち
@koty
GEEKLAB.NAGANO 管理人見習い
SIer(Javaとか.NETとか)→現職(Python/Django/AWS)
FaaS: Function as a Service
● 関数実行基盤
● 最近よく耳にする
AWS Lambda
● AWSのFaaS
● node/Java/C#(.NET Core)/Python を使用可能
● サーバーレス≒デーモンレス
● 状態を持たず、callされるたびにプロセスが上がっては落ちる(実際にはちょっと違う)
http://docs.aws.amazon.com/ja_jp/lambda/latest/dg/welcome.html
AWS Lambda
● https://aws.amazon.com/jp/lambda/pricing/
○ “リクエストのうち毎月最初の 1,000,000 件は無料”
○ “その後は 0.20 USD/1,000,000 件のリクエスト(0.0000002 USD/リクエスト)”
○ 安い
● http経由でcallしたい場合は、API-Gatewayを通す必要がある
AWS Lambda
● API Gatewayの他にも様々な起動トリガーを使える
● CloudWatch Eventsによるcron実行が便利
○ ちょっとした定期実行処理のための EC2が不要に
● RDBとの相性の悪さが言われていたが、最近は良く
なってきている
運用しているLambda
● http://b-sw.co/hokuto/index.html
○ 一日一回各サイトをクロールしてスクレイピング ←これがLambda関数
○ 結果をjsonでS3に保存
○ 上記静的サイトでjsonを読み取り
○ https://github.com/koty/hokuto_program_scraper
● https://twitter.com/glnagano/
○ GEEKLAB.NAGANO関連のイベント情報を定期的に tweet
○ https://github.com/geeklabnagano/glnagano-lambda-bot
● 社内で使っているシステムと Slackとの連携いろいろ
deployが面倒
● 簡単な処理(EC2を起動するとか)であれば、AWS管理コンソールに直接処理を書
けば良いが、、、
● 外部パッケージを使うときは、当該パッケージを含むzipを作る必要がある
● →面倒。herokuのようにrequirements.txt(rubyで言うgemfile)をプラットフォーム
側でダウンロードしてほしい
deployツールを使う
https://serverless.com
“The Serverless Framework is a CLI tool that
allows users to build & deploy auto-scaling,
pay-per-execution, event-driven functions.”
install〜deploy
npm install -g serverless
serverless create -t aws-python3 -p serverless-test
vim ~/.aws/credentials
serverless deploy --aws-profile koty
実行してみる
serverless invoke -f hello --aws-profile koty
/tmp に書き込んでみる
サーバーはあった
Amazon Linux ベースのコンテナ上で動くホストプロセスがいるらしい
requirements.txt を含むアプリのdeploy
● pip install時にコンパイルが走るパッケージはAmazon Linux上でzipを作る必
要がある
● Serverless Python Requirementsというプラグインを使うとlambdaのdockerコ
ンテナ上でzipを作ってくれる
参考:https://qiita.com/mkisono/items/f04297775275c6b50774
5275c6b50774
dockerコンテナ上でzipを作ってくれる利点
● プラグイン内で以下のdockerイメージを利用
  https://github.com/lambci/docker-lambda
pip install時にビルドが行われるパッケージ(lxmlとか)も安心。これまではEC2上で
Lambdaにdeployするzipを作る必要があった。
5275c6b50774
cronの登録
serverless.yml
functions:
hello:
handler: handler.handle
- schedule: cron(50 * ? * * *)
参考:https://github.com/geeklabnagano/glnagano-lambda-bot/blob/master/serverless.yml
環境変数の設定
serverless.yml
provider:
environment:
CONSUMER_KEY: ${file(./serverless.env.yml):CONSUMER_KEY}
CONSUMER_SECRET: ${file(./serverless.env.yml):CONSUMER_SECRET}
ACCESS_TOKEN: ${file(./serverless.env.yml):ACCESS_TOKEN}
ACCESS_TOKEN_SECRET: ${file(./serverless.env.yml):ACCESS_TOKEN_SECRET}
DOCO_API_KEY: ${file(./serverless.env.yml):DOCO_API_KEY}
AWS_S3_BUCKET_NAME: ${file(./serverless.env.yml):AWS_S3_BUCKET_NAME}
参考:https://github.com/geeklabnagano/glnagano-lambda-bot/blob/master/serverless.yml
ヒミツの値はGit管理外のファイルから読み込み
(もっとスマートな書き方あれば教えてください。。)
API Gateway の設定
serverless.yml
functions:
create:
handler: todos/create.create
events:
- http:
path: todos
method: post
cors: true
参考: https://github.com/serverless/examples/blob/master/aws-python-rest-api-with-dynamodb/serverless.yml
Djangoを動かしてみる
● serverless-wsgi というプラグインを利用
https://github.com/logandk/serverless-wsgi
functions:
api:
handler: wsgi.handler
events:
- http: ANY /
- http: ANY {proxy+}
参考:https://github.com/koty/dj-lambda-sample
ルーティングはDjango側に委譲
Djangoを動かしてみる
● ↓進捗。。。
ここまでできればまあ動きそう。
おしまい
● いろんな事例を聞きたい

Serverless frameworkでお手軽lambda運用 at #nseg #93