Services Covered

  • dynamodb DynamoDB
  • lambda AWS Lambda

Lab description

The company you work for are developing a system that collects data events from client applications on different devices and platforms periodically. The data event is processed by an AWS Lambda function which stores the event data in an Amazon DynamoDB table. The DynamoDB items will be later processed asynchronously by a different part of the system.

You have been tasked with implementing the required change to the AWS Lambda function in a development AWS account so that the change can be tested and verified by another team before being accepted.

In this challenge lab, an AWS Lambda function named dynamodb-lambda with the default new function implementation exists.


Learning Objectives

  • Creating a DynamoDB table
  • Updating the AWS Lambda function using the given implementation
  • Testing the function’s implementation using the AWS Lambda Console
  • Modifying the implementation of the AWS Lambda function

Lab date

31-10-2021


Prerequisites

  • AWS account

Lab steps

  1. Create a DynamoDB Table. Call it items, partition key id of type String and sort key type of type String.
  2. Upload the Lambda function code:
    import json
    import boto3
    import uuid
    def lambda_handler(event, context):
       print(event)
       TABLE_NAME = '<REPLACE-ME>'
       item_id = event['id'] or str(uuid.uuid4())
       item = {
           'id': {
               'S': item_id
           },
           'type': {
               'S': event['type'].lower()
           },
           'forward': {
               'BOOL': False
           }
       }
       if item['type']['S'] == 'mobile':
           item['forward']['BOOL'] = True
       dynamodb = boto3.client('dynamodb')
       response = dynamodb.put_item(TableName=TABLE_NAME, Item=item)
       status = response['ResponseMetadata']['HTTPStatusCode']
       return {'statusCode': status, 'body': json.dumps(item)}
  3. Invoke the Lambda Function with a Test Payload. Using the Test functionality of the AWS Lambda Console, test your Lambda implementation with the following JSON payload:
    { 
       "id": "1", 
       "type": "mobile" 
    }
  4. Modify the Lambda Implementation. Modify the Lambda function implementation so that events where the type is desktop have the forward attribute set to true.
    elif item['type']['S'] == 'desktop':
           item['forward']['BOOL'] = True

Lab files


Acknowledgements

Tags:

Leave a Reply

Your email address will not be published. Required fields are marked *