13
24

More than 5 years have passed since last update.

APIGatewayでlambdaを呼び出してAPIする

Last updated at Posted at 2017-10-23

自分用メモ。
サーバレスやAPIベースでアプリを作る時の基礎。

1:lambda作る

作る。

2:APIを作る

①APIGatewayでAPIの作成をクリック。
②API名をAPI群の総称(システム名とか)で設定。

3:リソースを作る

API作ったら画面遷移するので、
①[アクション]⇒[リソースの作成]
②リソース名をメソッド名(getRoomEnvとか)で設定
※この名前がAPIエンドポイントのURL末尾になる。
 http://[固有].execute-api.[リージョン].amazonaws.com/[ステージ]/[リソース]

4:メソッドを作る

①[アクション]⇒[メソッドの作成]
②POSTとか好きなのを選ぶ。
③各種設定 
統合タイプ        :lambda関数
lambdaプロキシ統合の使用:チェックしない
lambdaリージョン    :呼び出すlambdaのリージョン
lambda関数       :呼び出すlambdaの関数名

5-1:GETでパラメータを渡す場合

URL末尾にくっつける[param1="hoge"]をlambdaに渡す設定をする。
①メソッドの実行画面からメソッドリクエストを開く
②URLクエリ文字列パラメータでクエリ文字列の追加をクリック
③[param1]を追加。必須なら必須にチェック
④メソッドの実行画面に戻り統合リクエストを開く
⑤本文マッピングテンプレートをクリック
⑥[テンプレートが定義されていない場合]を選択
⑦マッピングテンプレートの追加:application/json

{
  "param1": "$input.params('param1')"
}

5-2:POSTでパラメータを渡す場合

Request bodyをlambdaに渡す設定をする。
①メソッドの実行画面から統合リクエストを開く
②本文マッピングテンプレートをクリック
③[テンプレートが定義されていない場合]を選択
④マッピングテンプレートの追加:application/json

##  See http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-mapping-template-reference.html
##  This template will pass through all parameters including path, querystring, header, stage variables, and context through to the integration endpoint via the body/payload
#set($allParams = $input.params())
{
"body-json" : $input.json('$'),
"params" : {
#foreach($type in $allParams.keySet())
    #set($params = $allParams.get($type))
"$type" : {
    #foreach($paramName in $params.keySet())
    "$paramName" : "$util.escapeJavaScript($params.get($paramName))"
        #if($foreach.hasNext),#end
    #end
}
    #if($foreach.hasNext),#end
#end
},
"stage-variables" : {
#foreach($key in $stageVariables.keySet())
"$key" : "$util.escapeJavaScript($stageVariables.get($key))"
    #if($foreach.hasNext),#end
#end
},
"context" : {
    "account-id" : "$context.identity.accountId",
    "api-id" : "$context.apiId",
    "api-key" : "$context.identity.apiKey",
    "authorizer-principal-id" : "$context.authorizer.principalId",
    "caller" : "$context.identity.caller",
    "cognito-authentication-provider" : "$context.identity.cognitoAuthenticationProvider",
    "cognito-authentication-type" : "$context.identity.cognitoAuthenticationType",
    "cognito-identity-id" : "$context.identity.cognitoIdentityId",
    "cognito-identity-pool-id" : "$context.identity.cognitoIdentityPoolId",
    "http-method" : "$context.httpMethod",
    "stage" : "$context.stage",
    "source-ip" : "$context.identity.sourceIp",
    "user" : "$context.identity.user",
    "user-agent" : "$context.identity.userAgent",
    "user-arn" : "$context.identity.userArn",
    "request-id" : "$context.requestId",
    "resource-id" : "$context.resourceId",
    "resource-path" : "$context.resourcePath"
    }
}

6:テスト

①メソッドの実行画面に戻り左端の[テスト⚡]をクリック
②必要パラメータを入れてテスト

7:デプロイ

①リソースツリーで[アクション]⇒[APIのデプロイ]
②ステージはprodとか。URLにくっつく。
 http://[固有].execute-api.[リージョン].amazonaws.com/[ステージ]/[リソース]

8:確認

①画面左の[ステージ]⇒[prod]⇒[リソース]⇒[メソッド]
②URLを確認してブラウザやPOSTMANで確認

9:お疲れ様でした。

13
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
13
24