-1

I am new to AWS. I'm trying to get data from DynamoDB table and display the item details in a webpage using a lambda function. Lambda function uses either nodejs or python.

It's a MQTT related project, I have created things and subscribe to the topic in AWS. Using MQTT BOX tool, I have published the data. It will be stored in base64 encryption in a DynamoDB table.

Here is my python code in the lambda function,

import boto3
import json
import os

s3 = boto3.client('s3')
ddb = boto3.response('dynamodb')
table = ddb.Table('ioTDaxxx')

def lambda_handler(event, context):
response = table.scan()
body = json.dumbs(response['Items'])
response = s3.put_object(Bucket='mqttxxx',
Key='hello.json',
Body=body,
ContentType='application/json')

It gives an error "module initialization error: module 'boto3' has no attribute 'response'". I don't know whether its correct code.

Kindly share me any sample code to fetch the data from DynamoDB table.

Ashan
  • 18,898
  • 4
  • 47
  • 67
abdul kadhar
  • 337
  • 4
  • 20

1 Answers1

0

The documentation for boto3 can be found here:

http://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html

Not sure why you are initializing it with boto3.response - but based on the docs you should be doing it the same way as how you are init'ing the s3 module:

ddb = boto3.client('dynamodb')

You can then call ddb.get_item(your_params) with the appropriate params. A full list of params for get_item() can be found here:

http://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html#DynamoDB.Client.get_item

Seanvm
  • 394
  • 1
  • 9
  • def lambda_handler(event, context): response = table.scan() for i in response['Items']: print(i['data'], ":", i['timestamp']) #body = json.dumbs(response['Items']) body = '{ "name":"John", "age":31, "city":"New York" }' response = s3.put_object(Bucket='mqttxxxx', Key='hello1.json', Body=body, ContentType='application/json') When we try to dumbs the value to json and it store in my store bucket. But when i run above code, it gives **"An error occurred (AccessDenied) when calling the PutObject operation: Access Denied"** – abdul kadhar Apr 17 '18 at 11:23
  • 1
    @abdulkadhar - that type of error occurs when your lambda function doesn't have the correct roles to access the resource (e.g. s3, dynamoDB). If you don't have it already, you will need to add an `iamRoleStatements` section to your `serverless.yml` file to grant permission to s3 and dynamoDB. You can read more about IAM roles here: [https://serverless.com/framework/docs/providers/aws/guide/iam/](https://serverless.com/framework/docs/providers/aws/guide/iam/) – Seanvm Apr 17 '18 at 15:12