Email

Hey man! So the stuff connected to the API/Lambda has been for the Cloud Challenge, which I’ve been working on and having a ton of fun with. I’ve been fighting procrastination by only working on the project that seems the most fun that moment, and the Cloud Challenge has been the consistent winner. I’ve made great progress, too! I’ve got an S3 bucket, running on CloudFront with SSL, serving a tiny website that will eventually have my resume on it. I’ve made a REST API with API Gateway that accepts GET and POST (CORS set up without a hitch but let’s not talk about it because I don’t want to jinx it), tested with Postman, connected to a Lambda that is set up to do basic CRUD operations on a DynamoDB table. I’m going to use all that just to do a pageload counter lmao, and my first plan was to do a hard-coded JSON payload to the Lambda but I’ve just been stymied. Without an example I’m really struggling with the JSON. It occurs to me just now that a really useful thing to put up for potential readers along with the resume would be a few basic examples of read/write/delete operations in functioning JSON, and it would give me good practice if I can stand to do it. Actually this would be best in a blog post, which is part of the challenge anyway. Sorry, thinking out loud.

So anyway, for our first trick, I’d like to call the API with the AWS SDK. I’m optimistic that these two pages have what we need:
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Js.03.html#GettingStarted.Js.03.03
and
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/WorkingWithItems.html#WorkingWithItems.AtomicCounters

I’ll put the Lambda code at the end of the email; when I pasted it, it pasted the VS Code formatting which is sweet but I can’t turn off the formatting after the code without messing it up within the code.

Second: I’d like to clean up my Gatsby/Sanity ecommerce template. For that I think we can just look at it in real time.

Third: (maybe second?) I’m noticing that part of what works well for me about the Cloud Challenge is the detailed specs. I liked this about your “5 Projects” piece, too. I think my next most likely project will be the one that builds map instances for AA websites and others that have lists of addresses and could really use a pinned map. It would be great if we could work up a detailed spec for that.

Back to first. Specifics of the API call: for the payload, we’re going to need something like this, but in JSON instead of CLI-speak:

aws dynamodb update-item \  
    --table-name ProductCatalog \  
    --key '{"Id": { "N": "601" }}' \  
    --update-expression "SET Price = Price + :incr" \  
    --expression-attribute-values '{":incr":{"N":"5"}}' \  
    --return-values UPDATED_NEW  

Here’s the Lambda code I’m using to control the database, I pulled it straight off an AWS tutorial. See you soon, I’m excited!

import boto3  
import json  
 
def handler(event, context):  
'''Provide an event that contains the following keys:  
 
- operation: one of the operations in the operations dict below  
- tableName: required for operations that interact with DynamoDB  
- payload: a parameter to pass to the operation being performed  
'''  
 
operation = event['operation']  
 
if 'tableName' in event:  
dynamo = boto3.resource('dynamodb').Table(event['tableName'])  
 
operationsDict = {  
'create': lambda x: dynamo.put_item(**x),  
'read': lambda x: dynamo.get_item(**x),  
'update': lambda x: dynamo.update_item(**x),  
'list': lambda x: dynamo.scan(**x),  
'echo': lambda x: x,  
'ping': lambda x: 'pong'  
}  
 
if operation in operationsDict:  
return operationsDict[operation](event.get('payload'))  
else:  
raise ValueError('Unrecognized operation "{}"'.format(operation))

Notes

"operation": "update", "tableName": "CCDynamoDB", "payload": { "ExpressionAttributeValues": { ":q":{"N": 1} }, "Key": { "id": "visitorCount" }, "UpdateExpression": "SET currentCount = currentCount + 1" } }

Todo

  • [ ]