Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Lambda w/ Python UUID on Dynamo DB (Concept)

This is a concept question:

We would like to create a unique primary key for a Dynamo DB table, while running our code on AWS Lambda.

If we use the python built in function uuid on AWS Lambda to create a unique key for a dynamo DB database is there any possibility that it could create a double of the key, if we had for example 5-20 billion items in our dynamodb database.

I know for example that the possibilities in a normal application for a double uuid key is extremely low nearly impossible.

As I understand every time uuid runs it makes sure it can't create a double by saving some previous value in memory.

However I am unsure if Lambda is just running the function below over and over with the same python console (and saving the uuid to make sure its unique) or if it is creating multiple separate console instances and running them through separately (not saving the memory of uuid).

Although this is a concept question here is some sample code:

from __future__ import print_function
from decimal import *
import boto3
import json
from locale import str
import uuid

def my_handler(event, context):
    description = event['description'] 
    spot_id = uuid.uuid4() #Unique identifier for spot 
    dynamodb = boto3.client('dynamodb')
    tablesinfo = "sinfo"
    dynamodb.put_item(
    TableName = tablesinfo, Item = {
      'spot_id':{'S' : str(spot_id)},
      'description': {'S' : description}
      }
    )
    return {'spot_id' : spot_id}
like image 398
Y Anderson Avatar asked Feb 25 '16 11:02

Y Anderson


People also ask

Can AWS Lambda connect to DynamoDB?

You can use an AWS Lambda function to process records in an Amazon DynamoDB stream. With DynamoDB Streams, you can trigger a Lambda function to perform additional work each time a DynamoDB table is updated.

What is DynamoDB UUID?

For example, if you have a User table, you might have a UUID as a primary key that uniquely identifies each user, but you might also have user name and email fields (“attributes” in DynamoDB terminology), which also must be unique for that user.

Can Lambda write to DynamoDB?

AWS Lambda: Allows a Lambda function to access an Amazon DynamoDB table. This example shows how you might create an identity-based policy that allows read and write access to a specific Amazon DynamoDB table. The policy also allows writing log files to CloudWatch Logs.


1 Answers

Amazon AWS have their own example for creating a UUID using Python in Lambda and storing this in elasticache. Amazon don't explicitly say this will definitively create a unique entry every time, but you can combine this with a check to see if the generated UUID is already in DynamoDB before insertion. The downside to checking if the UUID already exists is that this will use some read capacity on the DynamoDB table and therefore conceptually this is a cost to you.

Here's the code from AWS, if you adjust this to suit for DynamoDB with the check to see if the UUID has already been created in the table then this would work:

From: Amazon Lambda Documentation - Create a Lambda Function example

from __future__ import print_function
import time
import uuid
import sys
import socket
import elasticache_auto_discovery
from pymemcache.client.hash import HashClient

#elasticache settings
elasticache_config_endpoint = "your-elasticache-cluster-endpoint:port"
nodes = elasticache_auto_discovery.discover(elasticache_config_endpoint)
nodes = map(lambda x: (x[1], int(x[2])), nodes)
memcache_client = HashClient(nodes)

def handler(event, context):
    """
    This function puts into memcache and get from it.
    Memcache is hosted using elasticache
    """

    #Create a random UUID... this will the sample element we add to the cache.
    uuid_inserted = uuid.uuid4().hex
    #Put the UUID to the cache.
    memcache_client.set('uuid', uuid_inserted)
    #Get item (UUID) from the cache.
    uuid_obtained = memcache_client.get('uuid')
    if uuid_obtained == uuid_inserted:
        # this print should go to the CloudWatch Logs and Lambda console.
        print ("Success: Fetched value %s from memcache" %(uuid_inserted))
    else:
        raise Exception("Value is not the same as we put :(. Expected %s got %s" %(uuid_inserted, uuid_obtained))

    return "Fetched value from memcache: " + uuid_obtained
like image 65
3G Telecoms Avatar answered Sep 17 '22 15:09

3G Telecoms