0
votes

I am trying to connect to DynamoDB from my SAM application locally. I was able to start dynamodb server and was able to connect it through my python file referring this https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Python.01.html

import json import boto3 from boto3.dynamodb.conditions import Key from pprint import pprint

import requests

from botocore.exceptions import ClientError

def put_movie(title, year, plot, rating, dynamodb=None): if not dynamodb: dynamodb = boto3.resource('dynamodb', endpoint_url="http://localhost:8000")

table = dynamodb.Table('Movies')
response = table.put_item(
   Item={
        'year': year,
        'title': title,
        'info': {
            'plot': plot,
            'rating': rating
        }
    }
)
return response

def fun1(): movie_resp = put_movie("The Big New Movie", 2015, "Nothing happens at all.", 0) print("Put movie succeeded:") pprint(movie_resp)

def lambda_handler(event, context): """Sample pure Lambda function

Parameters
----------
event: dict, required
    API Gateway Lambda Proxy Input Format

    Event doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html#api-gateway-simple-proxy-for-lambda-input-format

context: object, required
    Lambda Context runtime methods and attributes

    Context doc: https://docs.aws.amazon.com/lambda/latest/dg/python-context-object.html

Returns
------
API Gateway Lambda Proxy Output Format: dict

    Return doc: https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-lambda-proxy-integrations.html
"""

# try:
#     ip = requests.get("http://checkip.amazonaws.com/")
# except requests.RequestException as e:
#     # Send some context about this error to Lambda Logs
#     print(e)

#     raise e
fun1()

s1 = "Hello there"
return {
    "statusCode": 200,
    "body": json.dumps({
        #"message": "hello world",
        "message" : s1
        # "location": ip.text.replace("\n", "")
    }),
}

    }

I get this error : enter image description here

here is my YAML file : enter image description here enter image description here

2

2 Answers

0
votes

If you're working with local DynamoDB, you need to set the endpoint URL when creating resource.

dynamodb = boto3.resource('dynamodb', endpoint_url="http://localhost:8000")
0
votes

If you already created a DynamoDB Table and have it running you do not need to use the endpoint parameter in line 3 of the put_movie function. So that function should look like the below:

import boto3

def put_movie(title, year, plot, rating, dynamodb = None):
    if not dynamodb:
        dynamodb = boto3.resource('dynamodb')
    
    table = dynamodb.Table('Movies')

    response = table.put_item(
        Item = {
            'year': year,
            'title': title,
            'info': {
                'plot': plot,
                'rating': rating
                }
            }
        )
    return response

Reference documentation: https://boto3.amazonaws.com/v1/documentation/api/latest/guide/dynamodb.html#using-an-existing-table