Skip to content

Instantly share code, notes, and snippets.

@shundy
Last active November 2, 2017 11:15
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save shundy/13ef6dfd98b4d99bab556c7a29edd112 to your computer and use it in GitHub Desktop.
Save shundy/13ef6dfd98b4d99bab556c7a29edd112 to your computer and use it in GitHub Desktop.
Serverless Workshop

Event Gateway

Node のインストール

brew install nodebrew
echo 'export PATH=$PATH:$HOME/.nodebrew/ current/bin' >> ~/.bash_profile
mkdir -p ~/.nodebrew/src
nodebrew install-binary v6.11.5

Serverless Framework のインストール

npm install -g serverless

作業ディレクトリの作成

mkdir eg-work && cd eg-work

Event Gatewayのインストール

wget https://raw.githubusercontent.com/serverless/event-gateway/master/install.sh

bash install.sh 0.5.15

https://github.com/serverless/event-gateway/releases (Windows)

Event Gateway起動

./bin/event-gateway -dev

.\event-gateway.exe -dev (Windows)

credentials

$ cat ~/.aws/credentials
[default]
aws_access_key_id = AKIAJ2ADWNEQBKZ76VOQ
aws_secret_access_key = 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
region = us-east-1

Function登録

curl --request POST \
 --url http://127.0.0.1:4001/v1/functions \
 --header 'content-type: application/json' \
 --data '{"functionId": "hello", "provider":{"type":"awslambda", "arn": "arn:aws:lambda:us-east-1:825880940331:function:sls-workshop-dev-postprocess", "region": "us-east-1"}}'

登録されたFunctionの確認

curl --request GET --url http://127.0.0.1:4001/v1/functions

EventのSubscription

curl --request POST \
--url http://127.0.0.1:4001/v1/subscriptions \
--header 'content-type: application/json' \
--data '{"functionId": "hello", "event":"awesomeEvent"}'

Subscriptionの確認

curl --request GET --url http://127.0.0.1:4001/v1/subscriptions

EventのEmit

curl --request POST \
--url http://127.0.0.1:4000/ \
--header 'content-type: application/json' --header 'event: awesomeEvent' \
--data '{"userId": "123"}'

kubeless

GCPのコンパネ

https://console.cloud.google.com

Google Container Engineの有効化

https://console.cloud.google.com/kubernetes/list

CLOUD SHELL上にServerlessをインストール

sudo npm install -g serverless

kubelessのpodを作成

For Mac

export RELEASE=v0.2.3
kubectl create ns kubeless
kubectl create -f https://github.com/kubeless/kubeless/releases/download/$RELEASE/kubeless-$RELEASE.yaml

For Windows

set RELEASE=v0.2.3`
kubectl create ns kubeless
kubectl create -f https://github.com/kubeless/kubeless/releases/download/%RELEASE%/kubeless-%RELEASE%.yaml

kubelessのpodを確認

kubectl get pods -n kubeless

Kubeless Serverless Pluginでfunctionを デプロイする

git clone https://github.com/serverless/serverless-kubeless.git

cd serverless-kubeless/examples/get-python/ && npm install

serverless deploy

デプロイされたfunctionの確認

kubectl get function

functionの実行

serverless invoke -f hello -l

ログの確認

serverless logs -f hello

サービスの削除

serverless remove

EG経由でGoogle Big Queryにデータ登録

EG用のサンプルリポジトリをローカルにclone

git clone https://github.com/serverless/event-gateway-example.git

セットアップ

cd event-gateway-example && npm install

Big Queryの準備

https://cloud.google.com/bigquery/quickstart-web-ui

テーブルの作成

cd event-gateway-example/services/analytics/ && node setup.js

作成完了

https://bigquery.cloud.google.com/welcome

EG立ち上げ

sls login

cd event-gateway-example/services/analytics && sls run

httpイベントのsubscription

curl --request POST --url http://localhost:4001/v1/subscriptions --header 'content-type: application/json' --data '{"functionId": "analytics-analytics", "event": "http", "method": "POST", "path": "/users"}'

httpイベントのsubscription

curl -XGET http://localhost:4001/v1/subscriptions | jq

Webフロント画面の立ち上げ

cd event-gateway-example/frontend && npm start

Google Big Query上で動作確認

SELECT
   event,
   receivedAt,
   JSON_EXTRACT(data, '$.email') AS email, data
FROM [<your-project-id>:emit_demo.test_events]
   ORDER BY receivedAt DESC LIMIT 1000 

本日のスライド

https://speakerdeck.com/horike37/slsconfworkshop

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment