Lambda with S3 Example
1. Configure security - Role
Create a DynomdbFullAccess Role and Assign to Lambda (NOT EC2!!).
AmazonDynamoDBFullAccess
2. Configure a Lambda Function
Step 1: Create a Lambda function Func2 with Runtime as Python 3.6
Step 2: Write below code in the code window. (This code is written in python using Boto Library which has the functions to insert S3 object information into DynamoDB table).
import boto3
from uuid import uuid4
def lambda_handler(event, context):
s3 = boto3.client("s3")
dynamodb = boto3.resource('dynamodb')
for record in event['Records']:
bucket_name = record['s3']['bucket']['name']
object_key = record['s3']['object']['key']
size = record['s3']['object'].get('size', -1)
event_name = record ['eventName']
event_time = record['eventTime']
dynamoTable = dynamodb.Table('newtable-irfan')
dynamoTable.put_item(
Item={'unique': str(uuid4()), 'Bucket': bucket_name, 'Object': object_key,'Size': size, 'Event': event_name, 'EventTime': event_time})
Step 3: File---> Save and Click on the Deploy button.
Step 4: Click on Configuration Tab and select Permission and click on Edit Role and attach the role which is created on Step1.
Step 5: Add Trigger by clicking on Add trigger button.
Select S3 trigger
Select bucket name (if you don't have a bucket, create a bucket first)
Event Type: All objects create events
Select acknowledgment for recursive invocation.
Click on Add button
3. Initiate a DynamoDB Table
Step 1: Create a DynamoDB table newtable-irfan and partition key unique.
Step 2: Explore items in newtable-irfan and you will find no records for this table.
4. Monitor changes in DynamoDB when you upload something in S3 bucket
Step 1: Upload and file in S3 bucket.
Step 2: Explore items in the DynamoDB table. and you will find the metadata information of the uploaded file in the S3 bucket.
Experiment:
Notice that if you delete the file in S3 bucket, the data in dynamoDB will not be erased.
Why?
because the lambda function we provide is only doing the PUT method. if we want to delete from the dynamoDB, we need to make another method. BUT that will require the API GATEWAY!.
