LANG SELRCT

Apps Script Reference  (Create: Create new Spreadsheet | Create new Apps Script

Wednesday, April 3, 2019

API GatewayでAPIを作成してLambda Functionと紐づけたい(API KEYを設定しない)


LambdaとAPI Gatewayを使ってみる では特に何も考えずにLambda Functionの画面でAPI Gatewayを追加しましたが、今回は別の方法があることを知ったのでそれを試してみた備忘録です。


今回試した方法
API GatewayのコンソールでAPIを作成する


API KEY を設定しない場合(今見ているこの記事)

 1.Lambda Functionを作成する
  名前と言語を決める

 2.API Gatewayで API を作成する
  名前とエンドポイントタイプを決める
  アクションからメソッドを作成する
  Lambda関数の欄に1で作成したFunctionを入れる
  APIをデプロイからステージ名を決めてデプロイする


API KEY を設定する場合(別の記事に書きます)

 3.API KEYを作成する

  名前を決める
  API KEYをGASのスクリプトのプロパティに入れる

 4.Usageを新規作成する
  ステージを追加する
  API KEYをUsageに追加する
  1のFunctionを設定する
  APIをデプロイする



この記事ではAPI KEYを設定しないAPIを作っていきます


STEP 1. Lambda Functionを作成する

「Create function」をクリックします


「Function name」に任意の名前を設定します
「Create function」をクリックします


このような画面になります


2. API Gatewayで API を新規作成する


「Create API」をクリックします


「API name」に任意の名前を入力します
「Create API」をクリックします


「Actions」から
「Create Method」を選択します


「ANY」を選択します


チェックボタンをクリックします


「Lambda Function」にSTEP 1で作成したFunction名を入力します
「Save」をクリックします


「OK」をクリックします


「Actions」から「Deploy API」を選択します


「Deployment stage」は[New Stage]
「Stage name」は beta (任意の名前)
「Deploy」をクリックします


「Invoke URL」をクリックしてみます


このようにブラウザに表示されます


Google Apps Scriptでアクセスしてみる


コード.gs
function runWithoutKey() {
  var url = 'https://API_ID.execute-api.us-east-1.amazonaws.com/beta/';
  var response = UrlFetchApp.fetch(url);
  Logger.log(response);
  Logger.log(JSON.parse(response)['body']);
}


実行結果


関連記事



参考

API Gateway コンソールでサンプルから API を作成してテストする
https://docs.aws.amazon.com/ja_jp/apigateway/latest/developerguide/api-gateway-create-api-from-example-console.html


チュートリアル: Amazon API Gateway で AWS Lambda を使用する
https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/with-on-demand-https-example.html


Create, Configure, and Test Usage Plans with the API Gateway Console
https://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-create-usage-plans-with-console.html

Latest post

Extracting data from Google Sheets with regular expressions

Introduction Regular expressions are a powerful tool that can be used to extract data from text.  In Google Sheets, regular expressions ca...