AWS Lambda with Python for CRUD Operation

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"dynamodb:DeleteItem",
"dynamodb:GetItem",
"dynamodb:PutItem",
"dynamodb:Scan",
"dynamodb:UpdateItem"
],
"Resource": "arn:aws:dynamodb:ap-south-1:167771397877:table/tutorials"
}
]
}
import json
import boto3
from boto3.dynamodb.conditions import Key
from boto3.dynamodb.conditions import Attr
def lambda_handler(event, context):
print("Event json %s" % json.dumps(event))
print("Context %s" % context)

client = boto3.resource('dynamodb')
table = client.Table('tutorials')

title = event['title']

print("Getting Title Filter %s" % title)

if not title:
print("Title is empty")
response = table.scan()
else:
print("Title is NOT empty")
response = table.scan(
FilterExpression = Attr('title').begins_with(title)
)
return response['Items']
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"logs:CreateLogGroup",
"logs:CreateLogStream",
"logs:PutLogEvents"
],
"Resource": "arn:aws:logs:*:*:*"
},
{
"Effect": "Allow",
"Action": "dynamodb:PutItem",
"Resource": "arn:aws:dynamodb:ap-south-1:167771397877:table/courses"
}
]
}
import json
import boto3
def lambda_handler(event, context):
client = boto3.resource('dynamodb')
table = client.Table('tutorials')
response = table.get_item(
Key={
'id': event['id']
}
)
if 'Item' in response:
return response['Item']
else:
return {
'statusCode': '404',
'body': 'Not found'
}
{
“id”: “02db0cdf-2006–4fc3-b133-d0d3ef8487b3”
}
import json
import boto3
import datetime
def lambda_handler(event, context):
print(type(event))
print(event)
print("Event json %s" % json.dumps(event))
print("Context %s" % context)
client = boto3.resource('dynamodb')
table = client.Table('tutorials')
eventDateTime = (datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S")
published = False
response = table.put_item(
Item = {
'id': context.aws_request_id,
'title': event['title'],
'description': event['description'],
'published': published,
'createdAt': eventDateTime,
'updatedAt': eventDateTime
}
)
return {
'statusCode': response['ResponseMetadata']['HTTPStatusCode'],
'body': 'Record ' + context.aws_request_id + ' added'
}
{
"id": "d290f1ee-6c54-4b01-90e6-d701748f0851",
"title": "Hansie",
"description": "Dachshund",
"published": "Male"
}
import json
import boto3
from datetime import datetime
def lambda_handler(event, context):print("Event json %s" % json.dumps(event))
print("Context %s" % context)

client = boto3.resource('dynamodb')
table = client.Table('tutorials')eventDateTime = (datetime.now()).strftime("%Y-%m-%d %H:%M:%S")
published = False

response = table.put_item(
Item={
'id': event['id'],
'title': event['title'],'description': event['description'],

'published': event['published'],
'updatedAt': eventDateTime,
'createdAt': eventDateTime
}
)
return {'statusCode': response['ResponseMetadata']['HTTPStatusCode'],'body': 'Record ' + context.aws_request_id + ' added'}
{“id”: “d290f1ee-6c54–4b01–90e6-d701748f0851”,“title”: “Hansie”,“description”: “Dachshund”,“published”: true}
import json
import boto3
def lambda_handler(event, context):client = boto3.resource('dynamodb')table = client.Table('tutorials')response = table.delete_item(Key={'id': event['id']})return {
'statusCode': '204',
'body': 'Tutorial Deleted successfully'
}
{
"id": "87884945-3bb3-4560-8976-8dc8499f3174"
}
import boto3dynamodb = boto3.resource('dynamodb')
table = dynamodb.Table('my-table')
scan = Nonewith table.batch_writer() as batch:
# Iterate through table until it's fully scanned
while scan is None or 'LastEvaluatedKey' in scan:
if scan is not None and 'LastEvaluatedKey' in scan:
scan = table.scan(
ProjectionExpression='id', # Replace with your actual Primary Key
ExclusiveStartKey=scan['LastEvaluatedKey'],
)
else:
scan = table.scan(ProjectionExpression='id')
for item in scan['Items']:
batch.delete_item(Key={'id': item['id']})

--

--

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