【问题标题】:Not able to access DynamoDB with my SAM app无法使用我的 SAM 应用程序访问 DynamoDB
【发布时间】:2020-11-10 14:20:50
【问题描述】:

我正在尝试从本地的 SAM 应用程序连接到 DynamoDB。我能够启动 dynamodb 服务器,并且能够通过我的 python 文件连接它,引用这个 https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Python.01.html

导入 json 导入 boto3 从 boto3.dynamodb.conditions 导入密钥 从 pprint 导入 pprint

导入请求

从 botocore.exceptions 导入 ClientError

def put_movie(title, year, plot, rating, dynamodb=None): 如果不是 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("大新电影", 2015, “什么都没有发生。”, 0) print("放电影成功:") pprint(movie_resp)

def lambda_handler(事件,上下文): """示例纯 Lambda 函数

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", "")
    }),
}

    }

我收到此错误: enter image description here

这是我的 YAML 文件: enter image description here 在此处输入图片说明

【问题讨论】:

    标签: python amazon-web-services aws-lambda amazon-dynamodb


    【解决方案1】:

    如果您使用的是本地 DynamoDB,则需要在创建资源时设置端点 URL。

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

    【讨论】:

      【解决方案2】:

      如果您已经创建了一个 DynamoDB 表并让它运行,则不需要在 put_movie 函数的第 3 行中使用 endpoint 参数。因此该函数应如下所示:

      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
      

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-09-27
        • 2022-08-23
        • 2020-02-01
        • 1970-01-01
        • 1970-01-01
        • 2014-01-12
        • 2018-07-05
        相关资源
        最近更新 更多