Quantcast
Channel: Node.jsタグが付けられた新着記事 - Qiita
Viewing all articles
Browse latest Browse all 8922

AWS Lambda入門③(Node編)〜API Gatewayで関数を公開する〜

$
0
0

概要

Api Gatewayとは

  • API GatewayはAWSが提供するサービスでHTTPでアクセス可能なAPIを作成できるサービスです
  • AWSの様々なサービスの呼び出しができるのでクライアントからするとLambdaなどをHTTPアクセスで実行するような感覚で使うことができます

API GatewayでLambda関数を公開する

Hello関数を公開してみる

  • まずは前回までで作ってあるHello関数を公開してみます
  • serverless.ymlに設定を追加します
serverless.yml
# ...省略functions:hello:handler:handler.helloevents:-http:path:hellomethod:get# ...省略
  • handlerの下にいくつか追加しました
    • pathはアクセスするときのpathです
    • methodはアクセスするときのHTTPメソッドです
  • 簡単ですね!

ローカルで動作確認

  • デプロイする前にまずはローカルで動作確認します
  • API Gatewayをローカルで動かすために必要なライブラリを追加します
yarn add -D serverless-offline@next

serverless-offlineのドキュメントは2020/3/5時点でα版の6系で書かれているとのことなので@nextをつけて6系をインストールする

  • serverless.ymlに設定を追加します
serverless.yml
# ...省略plugins:-serverless-offline# 追加した行-serverless-dynamodb-localcustom:serverless-offline:# 追加した行httpPort:8083# 追加した行dynamodb:stages:devstart:port:8082inMemory:truemigrate:trueseed:trueseed:hello:sources:-table:${self:provider.environment.DYNAMODB_TABLE}-hellosources:[./seeds/hello.json]# ...省略
  • pluginsserverless-offlineを追加しました
  • customにサーバ起動時のポート番号を追加しました
    • デフォルトは3000です
  • 以下のコマンドでサーバを起動します
sls offline start
  • ログにURLが表示されているはずです
┌───────────────────────────────────────────┐
   │                                           │
   │   GET | http://localhost:8083/dev/hello   │
   │                                           │
   └───────────────────────────────────────────┘

スクリーンショット 2020-03-06 1.31.34.png

  • うまくいけば画像のような結果になっているはずです
  • Lambdaを直接叩いていた時はstatusCodemessageの2つが返ってきていましたが今回はmessageだけです
  • statusCodeはAPI GatewayがHTTPレスポンスのステータスコードとしてセットして返却してくれています

AWSにデプロイして動作確認

  • ローカルで確認できたのでAWSにデプロイします
  • いつも通りServerlessFrameworkのコマンドを使います
sls deploy
  • デプロイに成功すると作成されたAPIのURLがログに出力されます
    • こんな感じ
Serverless: Stack update finished...
Service Information
service: sls-sample
stage: dev
region: ap-northeast-1
stack: sls-sample-dev
resources: 24
api keys:
  None
endpoints:
  GET - https://xxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/hello
functions:
  hello: sls-sample-dev-hello
  put: sls-sample-dev-put
  get: sls-sample-dev-get
  query: sls-sample-dev-query
  getAll: sls-sample-dev-getAll
layers:
  None
Serverless: Run the "serverless" command to setup monitoring, troubleshooting and testing.
✨  Done in 351.49s.
  • REST Clientでアクセスして確認します

スクリーンショット 2020-03-06 1.34.29.png

  • ステータスコード200で成功しました
  • これでhello関数を世の中に公開することができました

全ての関数を公開する

  • hello関数と同じように残りの関数も公開してみましょう

API Gatewayの設定追加

  • serverless.ymlを修正します
serverless.yml
# ...省略functions:hello:handler:handler.helloevents:-http:path:hellomethod:getput:handler:handler.putenvironment:tableName:${self:provider.environment.DYNAMODB_TABLE}-helloevents:-http:path:messagesmethod:postget:handler:handler.getenvironment:tableName:${self:provider.environment.DYNAMODB_TABLE}-helloevents:-http:path:messages/{id}method:getgetAll:handler:handler.getAllenvironment:tableName:${self:provider.environment.DYNAMODB_TABLE}-helloevents:-http:path:messagesmethod:get# ...省略
  • functionの項目にそれぞれURLとHTTPメソッドを追加しました
    • getのpathはmessages/{id}とすることでidをURLで指定するようにしました
  • 関数を一部修正します
    • get関数がURLに埋め込まれたidを取得できるようする
    • put関数が渡されたパラメータを取得できるようにする
handler.js
// ...省略module.exports.get=asyncevent=>{const{id}=event.pathParameters;// この行を修正constparams={TableName:tableName,Key:{id},};try{constresult=awaitdynamo.get(params).promise();return{statusCode:200,body:JSON.stringify(result.Item),};}catch(error){return{statusCode:error.statusCode,body:error.message,};}};// ...省略module.exports.put=asyncevent=>{constid=String(Date.now());const{message}=JSON.parse(event.body);// この行を修正constparams={TableName:tableName,Item:{id,message},};try{awaitdynamo.put(params).promise();return{statusCode:200,body:JSON.stringify({id,message}),};}catch(error){return{statusCode:error.statusCode,body:error.message,};}};// ...省略

ローカルで動作確認

  • ローカルサーバを立ち上げます
    • ローカルのDBを見に行くようにLOCAL=trueをつけて環境変数を設定しておきます
    • 詳しくは前回のDynamo編をご参照ください
LOCAL=true sls offline start
  • ログにURLが出力されます
┌────────────────────────────────────────────────────┐
   │                                                    │
   │   GET  | http://localhost:8083/dev/hello           │
   │   POST | http://localhost:8083/dev/messages        │
   │   GET  | http://localhost:8083/dev/messages/{id}   │
   │   GET  | http://localhost:8083/dev/messages        │
   │                                                    │
   └────────────────────────────────────────────────────┘
  • REST Clientでアクセスしてみましょう
  • それぞれ成功すると以下のようになるはずです

スクリーンショット 2020-03-06 2.05.46.png
スクリーンショット 2020-03-06 2.06.02.png
スクリーンショット 2020-03-06 2.06.22.png

AWSで動作確認

  • AWSにデプロイしてREST Clientでアクセスしてみましょう
sls deploy
  • 関数の文だけURLが表示されます
endpoints:
  GET  - https://xxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/hello
  POST - https://xxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/messages
  GET  - https://xxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/messages/{id}
  GET  - https://xxxxxxxxx.execute-api.ap-northeast-1.amazonaws.com/dev/messages
functions:
  hello: sls-sample-dev-hello
  put: sls-sample-dev-put
  get: sls-sample-dev-get
  getAll: sls-sample-dev-getAll
  • REST Clientでアクセスしてみてください
  • キャプチャは省略しますがローカルのときと同じようにアクセスできていればOKです

まとめ

  • API Gatewayを使うことでHTTリクエストでLambda関数を実行できました
  • ServerlessFrameworkを使うと簡単にAPI Gatewayの設定をすることができます
  • ローカルで動作させるためのライブラリも整っているため開発もやりやすくていいですね

Viewing all articles
Browse latest Browse all 8922

Trending Articles