【发布时间】: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