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

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

```js: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 でラムダを使う](https://qiita.com/ekzemplaro/items/7dc187885dffe0be6341)

次との違いは、uri が、http か、lambda の違いです。
[aws cli で API Gateway の API を作成する](https://qiita.com/ekzemplaro/items/8034bccff62bc1a0733e)

1) RestApi を作成。出力にrest-api-id　が表示される

```bash:restapi_create.sh
#
aws apigateway create-rest-api --name 'Afternoon AAA' \
	--region ap-northeast-1
#
```

確認方法

>```bash
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
#
```

3) Method を作成します。

![gateway_oct1801.png](https://qiita-image-store.s3.amazonaws.com/0/179446/2db8ea01-4dcf-e306-57ad-20a811dd2867.png)

![gateway_oct1802.png](https://qiita-image-store.s3.amazonaws.com/0/179446/47272de5-c4ad-9964-71c9-e57bd76b9797.png)

4) テストをします。

![gateway_oct1804.png](https://qiita-image-store.s3.amazonaws.com/0/179446/b8884b67-6100-cb95-950b-d2770308a687.png)

![gateway_oct1805.png](https://qiita-image-store.s3.amazonaws.com/0/179446/9527de5c-3625-2f0c-1013-f208cf67e077.png)

5) デプロイします。

![deploy_aa.png](https://qiita-image-store.s3.ap-northeast-1.amazonaws.com/0/179446/9506957c-f7ee-e6a2-24e3-e251141011b8.png)

![gateway_oct1807.png](https://qiita-image-store.s3.amazonaws.com/0/179446/31378e18-0078-ca48-64d5-99b31897619b.png)

##テスト##

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

1) curl

```bash: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
```

```js:in01.json
{"name": "夏目漱石",
"key1": "aaaa",
"key2": "bbbb",
"key3": "cccc"}
```

2) HTTPie

```bash:http_test.sh
URL="https://oa7ucmnfhjs.execute-api.ap-northeast-1.amazonaws.com/test01"
#
http $URL < in01.json
```

3) python


```py: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)
#
# ----------------------------------------------------------------
```
4) Node.js

```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 バージョンで確認しました。

```text
$ aws --version
aws-cli/2.2.41 Python/3.8.8 Linux/5.14.7-arch1-1 exe/x86_64.arch prompt/off
```