CRUD operations using API Gateway, Lambda and DynamoDB

Creating Table in DynamoDB

This is an asynchronous operation. Upon receiving a create table request, DynamoDB immediately returns a response with a Table status of CREATING. After the table is created, DynamoDB sets the Table status to ACTIVE.

You can perform read and write operations only on an ACTIVE table.

import boto3
import json
def lambda_handler(event, context):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.create_table(
TableName='UserTable',
KeySchema=
{
'AttributeName': 'username',
'KeyType': 'HASH'
},
AttributeDefinitions=
{
'AttributeName': 'password',
'AttributeType': 'RANGE'
},
ProvisionedThroughput={
'ReadCapacityUnits': 1,
'WriteCapacityUnits': 1
}
)
return table.table_status

AttributeDefinitions — An attributes which describe the key schema for the table. it is a required object.

KeySchema — Specifies the attributes that makes the primary key for a table. attributes in KeySchemamust be defined in AttributeDefinitions .

Writing to Table in DynamoDB

import json
import boto3
def put_user_data(username, password, dynamodb=None):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('UserTable')
response = table.put_item(
Item={
'username': username,
'password': password
}
)
return response
def lambda_handler(event, context):

username=event['username']
password=event['password']
dynamo_resp = put_user_data(username,password)
return {
'statusCode': 200,
'body': json.dumps(dynamo_resp)
}

Note: Pass JSON Value for event in the test because we are integrating with API Gateway.

{
"username": "user1",
"password": "user1"
}

Reading from Table in DynamoDB

import json
import boto3
def get_user(username, dynamodb=None):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('UserTable')
response = table.get_item(Key={'username': username})
return response
def lambda_handler(event, context):
# TODO implement
username=event['username']
get_data = get_user(username)
return {
'statusCode': 200,
'body': json.dumps(get_data)
}

Note: Pass JSON Value for event in the test because we are integrating with API Gateway.

{
"username": "user1"
}

Also, here it is required that we mention both the Attributes in Item which are defined in KeySchema.

Deletion from Table in DynamoDB

import json
import boto3
def delete_user_data(username):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('UserTable')
response = table.delete_item(
Key={
'username': username
}
)
return response
def lambda_handler(event, context):
# TODO implement
username=event['username']
resp=delete_user_data(username)
return {
'statusCode': 200,
'body': json.dumps(resp)
}

Note: Pass JSON Value for event in the test because we are integrating with API Gateway.

{
"username": "user1"
}

Also, here it is required that we mention both the Attributes in Item which are defined in KeySchema.

Updating the Table in dynamodb

import json
import boto3
def update_password(username, password):
dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('UserTable')
response = table.update_item(
Key={
'username': username,
},
UpdateExpression = 'SET password = :val1',
ExpressionAttributeValues={':val1': password }
)
return response
def lambda_handler(event, context):
username=event['username']
password=event['password']
resp=update_password(username,password)
return {
'statusCode': 200,
'body': json.dumps(resp)
}

Note: Pass JSON Value for event in the test because we are integrating with API Gateway.

{
"username": "user1",
"password": "user1"
}

DynamoDB Table

API Gateway Creation

Step: 1

Step: 2 Give Your API Gateway name with endpoint type regional

Step: 3

create resource type

Step:4

Step:5

Step:6

Step:7

Step:8

Step:9

Step:10

Step:11 -> Hit API Gateway URL with Post Method and pass the required values

Step: 12 Similarly you can create GET, PUT, DELETE,POST Method in postman.

--

--

--

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

The 3G Tale

An Apple TV case study: How we stayed ahead of the curve, and what we learnt along the way

‘Sorting’ out your problems!

Saas! Software as a service on Linux OS.

Google Home + OpenFaaS — Write your own voice-controlled functions

Applying Legal Design Thinking to the Software Development Process — The Distributed SQL Blog

Code that tries to be too clever

Basic Python Commands

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
ABHISHEK KUMAR

ABHISHEK KUMAR

More from Medium

AWS Amplify: Cognito Authorization for Lambda REST API (Part 3)

Asynchronous workflows powered by AWS-Lambda

Frictionless Serverless Development: Part 3— Authentication

Node-Canvas in AWS Lambda