AWS Cloud Quest: Triggers – Aggregating Data

This is the DIY challenge of the Triggers – Aggregating Data in AWS Cloud Quest.

DIY Steps:

Create a new DynamoDB table total_votes with partition and sort keys.

Update Lambda dynamodbstreams-newvotes

# Runtime: Python 3.7
# This…


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by architec

This is the DIY challenge of the Triggers - Aggregating Data in AWS Cloud Quest.

Image description

DIY Steps:

  1. Create a new DynamoDB table total_votes with partition and sort keys.

    Image description

  2. Update Lambda dynamodbstreams-newvotes

# Runtime: Python 3.7
# This Lambda function ilustrates how to handle DynamoDB Streams.
# Please review the comments for each code block to help you understand the execution of this Lambda function.
import boto3

# This will instantiate DynamoDB client for later use.
dynamodb = boto3.resource('dynamodb')

def check_record_pattern(record):
    if 'dynamodb' in record and 'NewImage' in record['dynamodb']:
        new_image = record['dynamodb']['NewImage']
        return 'improvement' in new_image and 'region' in new_image and 'S' in new_image['improvement'] and 'S' in new_image['region']
    return False

def lambda_handler(event, context):
    # Here you can find the documentation about the event and a sample of the event:
    # https://docs.aws.amazon.com/lambda/latest/dg/with-ddb-example.html
    print("Received Stream Event: " + str(event))
    for record in event['Records']:
        print(record['eventID'])
        print(record['eventName'])

        if check_record_pattern(record):
            improvement = record['dynamodb']['NewImage']['improvement']['S']
            region = record['dynamodb']['NewImage']['region']['S']
            print("Vote for " + improvement + " from region " +region)

            # Below code will enable you to use update_item API to increment 1 vote
            # for a given improvement and region to total_votes table.

            # For UpdateItem expressions check
            # https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html
            # For boto3 update_item check
            # https://boto3.amazonaws.com/v1/documentation/api/latest/reference/services/dynamodb.html#DynamoDB.Table.update_item

            table = dynamodb.Table('total_votes')
            table.update_item(
                Key={
                    'improvement': improvement,
                    'region': region
                },
                UpdateExpression="ADD total_votes :votevalue",
                ExpressionAttributeValues={
                    ':votevalue': 1
                }
            )

    return 'Successfully processed {} records.'.format(len(event['Records']))


This content originally appeared on DEV Community 👩‍💻👨‍💻 and was authored by architec


Print Share Comment Cite Upload Translate Updates
APA

architec | Sciencx (2023-02-13T23:14:58+00:00) AWS Cloud Quest: Triggers – Aggregating Data. Retrieved from https://www.scien.cx/2023/02/13/aws-cloud-quest-triggers-aggregating-data/

MLA
" » AWS Cloud Quest: Triggers – Aggregating Data." architec | Sciencx - Monday February 13, 2023, https://www.scien.cx/2023/02/13/aws-cloud-quest-triggers-aggregating-data/
HARVARD
architec | Sciencx Monday February 13, 2023 » AWS Cloud Quest: Triggers – Aggregating Data., viewed ,<https://www.scien.cx/2023/02/13/aws-cloud-quest-triggers-aggregating-data/>
VANCOUVER
architec | Sciencx - » AWS Cloud Quest: Triggers – Aggregating Data. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2023/02/13/aws-cloud-quest-triggers-aggregating-data/
CHICAGO
" » AWS Cloud Quest: Triggers – Aggregating Data." architec | Sciencx - Accessed . https://www.scien.cx/2023/02/13/aws-cloud-quest-triggers-aggregating-data/
IEEE
" » AWS Cloud Quest: Triggers – Aggregating Data." architec | Sciencx [Online]. Available: https://www.scien.cx/2023/02/13/aws-cloud-quest-triggers-aggregating-data/. [Accessed: ]
rf:citation
» AWS Cloud Quest: Triggers – Aggregating Data | architec | Sciencx | https://www.scien.cx/2023/02/13/aws-cloud-quest-triggers-aggregating-data/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.