Aws/APIGateway/Lambdaプロキシ統合の使用のついでに、DyanmoDBまで書き込む
DynamoDBのリファレンスは下記になります。
https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html
https://docs.aws.amazon.com/cli/latest/reference/dynamodb/
APIGatewayのURLの末尾(クエリパラメータ)に[?id=0003&value=kyoto]をつけてリクエストしてみる。
eventの['queryStringParameters']からidとvalueを受け取って、dynamodbの指定したTableにputitemしています。
※DynamoDBへの登録がしたかったので、ブラウザには{“message”: “Internal server error”}が返ります。
import json
import boto3
def lambda_handler(event, context):
id = event['queryStringParameters']['id']
value = event['queryStringParameters']['value']
item = {
"DeviceID": id,
"SensorID": value
}
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('{テーブル名}')
response = table.put_item(Item=item)
return response