AmazonApiGateway+Lambda+DynamoDBでREST API作った。
結構時間かかったのでメモ
#環境
- DynamoDB
Region:us-west-2
{ "Table": { "AttributeDefinitions": [ { "AttributeName": "category", "AttributeType": "S" }, { "AttributeName": "id", "AttributeType": "S" }, { "AttributeName": "user_id", "AttributeType": "S" } ], "CreationDateTime": "2015-07-20T00:37:39.302Z", "GlobalSecondaryIndexes": [ { "IndexName": "id-index", "IndexSizeBytes": 0, "IndexStatus": "ACTIVE", "ItemCount": 0, "KeySchema": [ { "AttributeName": "id", "KeyType": "HASH" } ], "Projection": { "ProjectionType": "ALL" }, "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 1, "WriteCapacityUnits": 1 } } ], "ItemCount": 0, "KeySchema": [ { "AttributeName": "user_id", "KeyType": "HASH" }, { "AttributeName": "category", "KeyType": "RANGE" } ], "ProvisionedThroughput": { "NumberOfDecreasesToday": 0, "ReadCapacityUnits": 1, "WriteCapacityUnits": 1 }, "TableName": "Test", "TableSizeBytes": 0, "TableStatus": "ACTIVE" } }
descriptionだと長々出てくるが
user_id String hash_key category String range_key id String id-index
と言うだけ。
- Lambda
Region:us-west-2
#流れ
(1)
Lambdaのトップページに行き"Create a Lambda function"を押す
Select blueprintで"microservice-http-endpoint"を選択する
(2)
必須要素を埋める
{ "Name":"TestAPI", "Description":"This is test", "Runtime":"node.js" }
(3)
(4)
RoleにDynamoDBが操作できる権限を持つユーザーを設定する。
(5)
Nextを押す
(6)
必須要素を埋める
{ "API name":"Test", "Resource name":"test", "Method":"GET", "Deployment stage":"prod", "Security":"Open" }
(7)
終わり
(8)
DynamoDBに以下のようなデータを詰める
Item{5} user_id String: mizuki category String: admin id String: u0001 shield String: 世界樹の盾 weapon String: 水の剣
(9)
"Actions"ボタンを押して"configure sample event"を選ぶとtest caseを編集できる
{ "id": "u0001" }
とする
(A)
Testボタンを押す
Execution resultに
{ "Count": 1, "Items": [ { "id": "u0001", "category": "admin", "weapon": "/**文字化け**/", "user_id": "mizuki", "shield": "/**文字化け**/" } ], "ScannedCount": 1 }
と出たら成功
(B)
API endpointsタブを開き、Method->GETのボタンを押す。
Amazon API Gatewayのページに飛ぶ。
"Integration Request"を押し設定する。
{ "id": "$input.params('id')" }
(C)
"Method Request"を押し設定する。
URL Query String Parameters -> Add query string
name: id
(D)
Clientの上のTestを押し、Testする。
Query Strings
id:u0001
結果
{ "Count": 1, "Items": [ { "id": "u0001", "category": "admin", "weapon": "水の剣", "user_id": "mizuki", "shield": "世界樹の盾" } ], "ScannedCount": 1 }
(E)
Deployボタンを押し
Deployment stage: prod
にしてDeployする。
はまったところ
{ "id": "$input.params('id')" }
を設定する時設定を間違えてidに値が入らなかった。