Posts

Showing posts with the label dynamodb

REST API with Chalice + Pynamo

Image
In this post, I am going to deploy a sample REST API on AWS API Gateway, Lambda, and DynamoDB using Chalice and PynamoDB. Chalice: https://web-quickstart.blogspot.com/2021/03/aws-chalice.html Pynamo: https://web-quickstart.blogspot.com/2021/05/crud-dynamodb-from-python-pynamodb.html API spec The following methods will be served for CRUD operations: /users/  POST:   param: id name /users/{id}  GET:   response: id name    PUT:   param: name    DELETE create project % chalice new-project user_api % cd ./user_api prepare files % tree . ├── app.py ├── chalicelib │   ├── __init__.py │   └── user.py ├── docker-compose.yml └── requirements.txt # app.py from chalice import Chalice from chalice import ForbiddenError, NotFoundError from chalicelib.user import User app = Chalice(app_name= 'user_api' ) @ app.route ( '/users' , methods=[ 'POST' ]) def create_user ():     user_as_json = app.current_request.json_body ...

CRUD DynamoDB from Python (PynamoDB)

In this post, I am goint to use DynamoDB from PynamoDB. You may compare the code with  CRUD DynamoDB from Python (boto3) what is PynamoDB? PynamoDB is "a Pythonic interface for Amazon's DynamoDB" https://github.com/pynamodb/PynamoDB install % pip install pynamodb % pip list | grep pynamo pynamodb                           5.0.3 student.py You can define the entity like: from pynamodb.attributes import UnicodeAttribute from pynamodb.models import Model class Student (Model):   class Meta :     table_name = 'student'     host = 'http://localhost:8000' # region = 'us-east-1'     write_capacity_units = 5       read_capacity_units = 5     id = UnicodeAttribute(hash_key= True )   grade = UnicodeAttribute(null= True ) You may remove the host = localhost (and optionally specify the region) to run it on the cloud. test.py from student import Studen...

CRUD DynamoDB from Python (boto3)

This post shows examples of DynamoDB usage from Python boto3. install % pip install boto3 % pip list | grep boto3 boto3                              1.17.61 test.py import boto3 # connect to dynamodb (local) # remove endpoint_url to use the cloud dynamodb = boto3.resource( 'dynamodb' , endpoint_url= 'http://localhost:8000' ) # list table table_list = dynamodb.tables.all() for table in table_list:   print (table.table_name) # create table table = dynamodb.create_table(   TableName= 'student' ,   AttributeDefinitions=[     {       'AttributeName' : 'id' ,       'AttributeType' : 'S'     },   ],   KeySchema=[     {       'AttributeName' : 'id' ,       'KeyType' : 'HASH'     }   ],   ProvisionedThroughput={     'ReadCapacityUnits' : 5 ,     'WriteCapacityUnits' ...