40. @Yasuharu Suzuki40
Serverless Framework@0.5.6のProject作成
参考サイト
http://tech.pero.li/entry/2016/04/08/142649
node.jsの例ですがseverlessフレームワークの情報多め
Serverlessフレームワークで Amazon API Gateway + Lambda のAPIを作った
プロジェクト作成(v1.00とかなり異なる)
> sls project create
_______ __
| _ .-----.----.--.--.-----.----| .-----.-----.-----.
| |___| -__| _| | | -__| _| | -__|__ --|__ --|
|____ |_____|__| ___/|_____|__| |__|_____|_____|_____|
| | | The Serverless Application Framework
| | serverless.com, v0.5.6
`-------'
Serverless: Initializing Serverless Project...
Serverless: Enter a name for this project: (serverless-s15t8i) sls-cors-test02
Serverless: Enter a new stage name for this project: (dev)
Serverless: For the "dev" stage, do you want to use an existing Amazon Web Services profile or create a
new one?
> Existing Profile
Create A New Profile
Serverless: Select a profile for your project:
> default
suzuki_yasuharu-private
Serverless: Creating stage "dev"...
Serverless: Select a new region for your stage:
us-east-1
us-west-2
eu-west-1
eu-central-1
> ap-northeast-1
Serverless: Creating region "ap-northeast-1" in stage "dev"...
Serverless: Deploying resources to stage "dev" in region "ap-northeast-1" via Cloudformation (~3
minutes)...
Serverless: Successfully deployed "dev" resources to "ap-northeast-1"
Serverless: Successfully created region "ap-northeast-1" within stage "dev"
Serverless: Successfully created stage "dev"
Serverless: Successfully initialized project "sls-cors-test02"
Project名
ステージ
AWS Profileの選択
regionの選択
41. @Yasuharu Suzuki
作成したproject内にディレクトリを移動して、endpoint作成
> cd sls-cors-test02
> sls function create functions/hello/world
Serverless: Please, select a runtime for this new Function
nodejs4.3
> python2.7
nodejs (v0.10, soon to be deprecated)
Serverless: For this new Function, would you like to create an Endpoint, Event, or just the Function?
> Create Endpoint
Create Event
Just the Function...
Serverless: Successfully created function: "functions/hello/world"
デフォルトで出力されたソースファイルを表示
> cd functions/hello/world
> cat handler.py
from __future__ import print_function
import json
import logging
log = logging.getLogger()
log.setLevel(logging.DEBUG)
def handler(event, context):
log.debug("Received event {}".format(json.dumps(event)))
return {}
functions/hello/world/handler.handlerのソースを修正。戻り値をちょっと変えただけ。
return {"message":"cors test !"}
41
Serverless Framework@0.5.6のendpoint作成
参考サイト
http://tech.pero.li/entry/2016/04/08/142649
42. @Yasuharu Suzuki42
Serverless Framework@0.5.6のdeploy
ソースを書き換えてローカルで実行。
ローカルで軽く動作確認できるのは便利!
ただしどうやらpythonではコンソール出力がされない様子。
> sls function run
Serverless: Running world...
Serverless: -----------------
Serverless: Success! - This Response Was Returned:
Serverless: {"message":"cors test !"}
参考サイト
http://tech.pero.li/entry/2016/04/08/142649
デプロイ!
> sls dash deploy
-- (途中略) --
Serverless: Select the assets you wish to deploy:
world
function - world
endpoint - world - GET
- - - - -
> Deploy
Cancel
Serverless: Successfully deployed endpoints in "dev" to the following regions:
Serverless: ap-northeast-1 ------------------------
Serverless: GET - world - https://aaa.execute-api.ap-northeast-1.amazonaws.com/dev/world
curlで叩いてみる
とても簡単ですね!
> curl https://aaa.execute-api.ap-northeast-1.amazonaws.com/dev/world
{"message": "cors test !"}