Carlos Aguni

Highly motivated self-taught IT analyst. Always learning and ready to explore new skills. An eternal apprentice.


Lambda DynamoDB Auto Increment

25 May 2020 »

https://medium.com/lego-engineering/sequence-numbering-in-serverless-via-api-gateway-40e5f6c83e93

import json
import boto3

def lambda_handler(event, context):
    # TODO implement
    dynamodb = boto3.client('dynamodb')
    rs = dynamodb.update_item(
        TableName= "serial_gen",
        Key= {
            "id": {
                "S": "100"
            }
        },
        ExpressionAttributeValues= {
            ":val": {
                "N": "1"
            }
        },
        UpdateExpression= "SET seq = seq + :val",
        ReturnValues= "UPDATED_NEW"
    )
    print(rs)
    return "ok"