CRUD operations using API Gateway, Lambda and DynamoDB

ABHISHEK KUMAR
4 min readMay 13, 2021

--

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.

--

--

ABHISHEK KUMAR
ABHISHEK KUMAR

Written by ABHISHEK KUMAR

DevOps/Cloud | 2x AWS Certified | 1x Terraform Certified | 1x CKAD Certified | Gitlab

No responses yet