A Journey in Learning

Python Boto3 DynamoDB update

When dealing with any type of troubleshooting, there is always a moment of sheer horror in which you doubt your ability to do anything remotely difficult. Usually this is when people start flipping out however after you realize the small mistake that caused you so much pain you gain some sort of dopamine dump in which your ape body feels like it outran some sort of pre-historic predator.

I have been flirting with coding and wanted to see if I could create a simple function to update DynamoDB using Python, my plans are to use this code to update my website counter using an API Gateway. The problem is that BOTO3 has very little examples online to follow and that I need to learn more about the Syntax of Python.

import boto3

dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('WebsiteVisits')

def Website_CounterUpdater(event, context):
  
  table.update_item(
    Key={
      'URL': "Alkisnar.IO",
      'Page': "ResumePage"
    },
    UpdateExpression='set VisitCount = VisitCount + :increment',
        ExpressionAttributeValues={
            ':increment': 1
        }    
    )

What I did and What I learned:

  1. I want to make sure that this code has the least amount of permissions as possible, I don’t want my functions to have the golden key to unlock all my services so this specific piece of code can ONLY access my single DynamoDB table.
  2. BOTO3 Python is not the goto code that other people use to access dyanmoDB, most other people use NodeJs
  3. I need to learn Python Syntax, For example a Handler and function name in lambda need to be defined: [file_name:function_name]. In the function itself it needs to written as: def function_name(event, context):
  4. In coding, small things like a colon before :increment, indentation of parenthesis, placement of "=" signs need to be carefully examined before continuing to code, lest your going to make a small mistake that will take ages to find