Entoverse

Reactのこと

Python 3.7 AWS Lambda DynamoDB 接続

AWS Lambda Python 3.7を使ってDynamoDBテーブル接続

import boto3
import json
from boto3.dynamodb.conditions import Key

def lambda_handler(event, context):
    dynamodb = boto3.resource('dynamodb')
    table    = dynamodb.Table('テーブル名')
    try:
        res = table.query(
            KeyConditionExpression = Key('プライマーキー').eq(int(event["value1"])) & Key('ソートキー').eq(int(event["value2"]))
            #ScanIndexForward = false
        )
        cnt = 0
        out = list(range(0))
        for song_num in res["Items"][0]["なにか"]:
            if (start_ <= cnt) and (end_ > cnt):
                out.append(one_song(song_num))
            cnt += 1
            
    except Exception as e:
        res = e;
    return {
        'statusCode': 200,
        'body': out
    }

f:id:mojeld:20181030113145p:plain