2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

API Gateway から Lambda にアクセスする

Last updated at Posted at 2017-10-18

次の Lambda 関数に、API Gateway からアクセスする方法です。

index.js
// ---------------------------------------------------------------------
//
//	index.js
//
//						Nov/03/2017
//
// ---------------------------------------------------------------------
console.log ("*** start *** index.js ***")

exports.handler = function (event,context,callback)
{
	var name = (event.name === undefined ? 'No-Name' : event.name)
	console.log('"Hello":"' + name + '"')
	console.log ("*** handler *** start ***")
	console.log ("event.key1 = " + event.key1)
	console.log ("event.key2 = " + event.key2)
	console.log ("event.key3 = " + event.key3)
	console.log ("おはようございます。")
	var dt = new Date()
	const year = dt.getFullYear()
	const month = dt.getMonth()+1
	const date = dt.getDate()
	const hours = dt.getHours()
	const minutes = dt.getMinutes()
	const seconds = dt.getSeconds()
	var str_out = "" + year + "-" + month + "-" + date
	str_out	+= " " + hours + ":" + minutes + ":" + seconds
	console.log(str_out)
	console.log(dt.toUTCString())
	console.log ("version Nov/03/2017 PM 14:16")
	console.log ("*** handler *** end ***")

	const rvalue = {"Hello": name,
			"city": "シモツケ"}
	callback(null, rvalue) // SUCCESS with message
}

// ---------------------------------------------------------------------

この関数を、morning_function という名前で Lambda に登録します。
登録の方法は、こちらです。
aws cli でラムダを使う

次との違いは、uri が、http か、lambda の違いです。
aws cli で API Gateway の API を作成する

  1. RestApi を作成。出力にrest-api-id が表示される
restapi_create.sh
#
aws apigateway create-rest-api --name 'Afternoon AAA' \
	--region ap-northeast-1
#

確認方法

aws apigateway get-rest-apis


2) RestApi のルートリソース ID を取得

```bash:restapi_resource.sh
#
REST_API_ID=1awvlqh2cc
#
aws apigateway get-resources \
	--rest-api-id $REST_API_ID \
	--region ap-northeast-1
#
  1. Method を作成します。

gateway_oct1801.png

gateway_oct1802.png

  1. テストをします。

gateway_oct1804.png

gateway_oct1805.png

  1. デプロイします。

deploy_aa.png

gateway_oct1807.png

##テスト##

URL の呼び出し が
https://oa7ucmnfhj.execute-api.ap-northeast-1.amazonaws.com/test01"
とします。

  1. curl
restapi_test.sh
#
REST_API_ID=oa7ucmnfhj
#
URL="https://"$REST_API_ID".execute-api.ap-northeast-1.amazonaws.com/test01"
#
curl  -H "Content-Type: application/json" -X POST -d@in01.json $URL
in01.json
{"name": "夏目漱石",
"key1": "aaaa",
"key2": "bbbb",
"key3": "cccc"}
  1. HTTPie
http_test.sh
URL="https://oa7ucmnfhjs.execute-api.ap-northeast-1.amazonaws.com/test01"
#
http $URL < in01.json
  1. python
restapi_test.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
#	restapi_test.py
#
#					Oct/18/2017
# ----------------------------------------------------------------
import sys
import json
import requests
# ----------------------------------------------------------------
#
rest_api_id="oa7ucmnfhj"
#
url="https://" + rest_api_id + ".execute-api.ap-northeast-1.amazonaws.com/test01"
#
print(url)
#
name = '夏目漱石'
key1 = 'abcdef'
key2 = 'test2'
key3 = 'test3'
#
payload = {'Content-Type': 'application/json',
		'name': name,
		'key1': key1,
		'key2': key2,
		'key3': key3}
#
rr = requests.post(url, data=json.dumps(payload))
#
print(rr.text)
#
# ----------------------------------------------------------------
  1. Node.js
restapi_test.js
#! /usr/bin/node
// ---------------------------------------------------------------
//
//	restapi_test.js
//
//					Oct/18/2017
//	
// ---------------------------------------------------------------
var Client = require('node-rest-client').Client
 
var client = new Client()
const args = {
    data: { "name": "夏目漱石" },
    headers: { "Content-Type": "application/json" }
}

const rest_api_id="oa7ucmnfhj"


const url="https://" + rest_api_id + ".execute-api.ap-northeast-1.amazonaws.com/test01"

client.post(url, args, function (data, response) {
    // parsed response body as js object 
	console.log(data)
    // raw response 
//    console.log(response);
})

//
// ---------------------------------------------------------------

次の aws-cli バージョンで確認しました。

$ aws --version
aws-cli/2.2.41 Python/3.8.8 Linux/5.14.7-arch1-1 exe/x86_64.arch prompt/off
2
4
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
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?