Episode 1: AWS Lambda and DynamoDB

·

3 min read

This is the first chapter of the series . In this chapter, we will look at the most common server-less components of AWS, namely Lambda and DynamoDB. We will not be looking at the server-less components at deep-dive manner, for those AWS have very amazing resources online.

Let's log into the AWS console, we will begin with creating a DynamoDB table name Messages with a single column (partition key) named message.

DynamoDB Creation

Lets go DynamoDB and create a db with table name Messages...

Screen Shot 36.png

Lets create a column called partition key. As DynamoDB is a NoSQL, it basically can grow in columns, and the data it holds over time. As we will see in the later episodes by adding more data to the db. However the partition key cannot be changed once created as such, designing and identifying a correct partition key is important.

Screen Shot 37.png

Screen Shot 38.png

Once created, you will see the table in creation in-progress just like below:

Screen Shot 39.png

Lets now go to the items tab from the left menu, and select the table we just created, Messages and then you can click on Create item on the right hand side:

Screen Shot 40.png

Lets add a message, this is first message manually key-ed in from dynamoDB

Screen Shot 41.png

After adding, you should see the message in the list:

Screen Shot 42.png

Lambda Function Creation

Lets create a Lambda function that will get all the data from the dynamoDB and return.

Lets go to Lambda and Creation Function:

Screen Shot 43.png

Let us name the function, MyReadLambda and select the language Python 3.8 and create the function

Screen Shot 44.png

Paste the below code, and select Deploy

import json
import boto3
import os

def lambda_handler(event, context):
    dynamodb = boto3.resource('dynamodb')
    messages_table = "Messages"
    table = dynamodb.Table(messages_table)
    data = table.scan()

    return {
        'statusCode': 200,
        'body': json.dumps(data)
    }

Screen Shot 47.png

One more step before we could test, lets update the permission to allow the Lambda to scan DynamoDB. You need to go to Configuration tab and select the Role name that will redirect you to IAM

Screen Shot.png

You should be able to see similar to the below code:

Screen Shot 51.png

Select attach policies, and search for DynamoDB

Screen Shot 50.png

Select ReadOnlyAccess and click Attach Policy

Once done, lets route back to Lambda and go to test,

Please note, IAM may take time to reflect, to if the below steps does not work, lets try waiting for sometime and try again

Screen Shot.png

In the test tab, you should similiar to the above view, in that give a name to to the test event. And click Test

Screen Shot 52.png

Once done, you should see below:

Screen Shot 54.png

We have successfully deployed and tested the flow. This marks the end of this episodes, we will manually delete the resources we have created.

Cleanup

Let us delete the Lambda function by simply selecting the function and select the Delete from Actions dropdown:

Screen Shot 55.png

Let us delete the DynamoDB table:

Screen Shot 57.png

you will have to type delete to confirm the deletion

Screen Shot 58.png