サーバーレスを用いたデプロイフローや開発フローを考えた時に ServerlessFrameworkについていろいろ調査を行い触ってみたので書いてみます。 基本的な流れとして、コンテナからServerlessFrameworkを使用してみました。
目的
サーバーレスアーキテクチャに関する情報を自ら発信することでサーバーレスに関する情報を集める
環境
[ホストOS]
- Mac OS X El Capitan (ver. 10.11.3)
- zsh 5.0.8 (x86_64-apple-darwin15.0)
[コンテナ]
- Dockerfile
# DockerHubからAmazon Linuxイメージを取得
FROM amazonlinux
# 作成したユーザの情報
MAINTAINER TakuyaOkazaki <xxxxxxxxxx@xxxxxx.xxx>
# ビルド時のみ下記コマンドを実行する
WORKDIR /
RUN curl -kL https://bootstrap.pypa.io/get-pip.py | python
RUN pip install awscli
RUN curl --silent --location https://rpm.nodesource.com/setup_6.x | bash -
RUN yum -y install nodejs
RUN yum -y install gcc-c++ make
RUN npm install -g serverless --unsafe-perm
RUN mkdir -p /home/serverless/work
WORKDIR /home/serverless/work
RUN serverless create -t aws-python -p playpass
- docker-compose .yml
version: '3.2'
services:
deploy:
build: "./"
container_name: playpass_backend_deploy
tty: true
- ディレクトリ構造
.
`-- playpass(プロジェクトの親ディレクトリ)
|-- main
| |-- .serverless(ここにデプロイしたものの成果物が保存されている)
| |-- functions(オーケストレーションの担当: Lambda関数のコード)
| |-- requirements.txt(ConfigurationManagementレイヤの担当: Lambda関数のコードで用いるpythonのライブラリ一覧)
| `-- serverless.yml(ブートストラップレイヤの担当 : AWSで枠を作る)
|-- node_modules(serverless-python-requirementsを使用するために必要)
| |-- archiver
| |-- archiver-utils
| |-- async
| |-- balanced-match
| |-- bl
| |-- bluebird
| |-- brace-expansion
| |-- buffer-crc32
| |-- compress-commons
| |-- concat-map
| |-- core-util-is
| |-- crc
| |-- crc32-stream
| |-- end-of-stream
| |-- fs-extra
| |-- fs.realpath
| |-- glob
| |-- glob-all
| |-- graceful-fs
| |-- inflight
| |-- inherits
| |-- isarray
| |-- jsonfile
| |-- lazystream
| |-- lodash
| |-- minimatch
| |-- minimist
| |-- normalize-path
| |-- once
| |-- path-is-absolute
| |-- process-nextick-args
| |-- readable-stream
| |-- remove-trailing-separator
| |-- safe-buffer
| |-- serverless-python-requirements
| |-- string_decoder
| |-- tar-stream
| |-- universalify
| |-- util-deprecate
| |-- walkdir
| |-- wrappy
| |-- xtend
| |-- yargs
| `-- zip-stream
`-- venv(このプロジェクト内ではpython3系を想定しているため、pythonのバージョンを切り替える様に必要)
|-- bin
|-- include
|-- lib
|-- lib64 -> lib
|-- local
`-- pip-selfcheck.json
- functionsのディレクトリ構造
functions/
`-- hello
`-- handler.py
- handler.pyの中身
# お試しでimportしてみる(ちゃんとサードパーティのライブラリも読み込まれるかどうかの検証のため)
import pytz
import json
def hello(event, context):
body = {
"message": "Go Serverless v1.0! Your function executed successfully!",
"input": event
}
response = {
"statusCode": 200,
"body": json.dumps(body)
}
return response
# Use this code if you don't use the http event with the LAMBDA-PROXY
# integration
"""
return {
"message": "Go Serverless v1.0! Your function executed successfully!",
"event": event
}
"""
~
- serverless.yml(ServerlessFrameworkのサンプル集を少し改造したもの)
service: serverless-test-okazaki
frameworkVersion: ">=1.1.0 対応策として、もう一度同じバケットを生成すれば解消可能
<ul>
<li><p>アクセスキー・シークレットキー・リージョンの設定を間違えると・・・・。
(aws configureでの設定部分)</p></li>
<li><p>よしなにやってくれてる部分をちゃんと把握する必要がある</p></li>
</ul>
<h2>補足</h2>
<ul>
<li><a href="https://github.com/serverless/examples">ServerlessFrameworkのサンプル集</a></li>
<li><a href="https://github.com/UnitedIncome/serverless-python-requirements">python依存解消用ServerlessFrameworkのプラグイン</a></li>
</ul> 岡崎拓哉
2016年に入社した新卒。ドラムとインコが好きな人。
最近は、デザイン駆動設計や関数型プログラミングに興味あり。
マネジメントも覚えていきたい系エンジニア。