python在AWS lamda中运行的“ XXXX未定义”错误
我有一个简单的脚本来更新DynamoDB中的值,然后还给我回馈更新的值。如果我在vscode中运行脚本,与AWS交谈,则可以正常工作 - 没有错误。但是,当我为lambda格式化时,我会遇到错误。
vscode
import boto3
from boto3.dynamodb.conditions import Key, Attr
dynamodb = boto3.resource('dynamodb',region_name='us-east-1')
table = dynamodb.Table('Visitors')
response = table.update_item(
Key={'ID': 0},
UpdateExpression="ADD #ct :increment",
ExpressionAttributeNames={'#ct': 'visitorcount'},
ExpressionAttributeValues={':increment': 1},
ReturnValues = "UPDATED_NEW"
)
visitorCount = int(response["Attributes"]["visitorcount"])
print('visitorCount obtained from table = ' + str(visitorCount))
lambda
import json
import boto3
from boto3.dynamodb.conditions import Key, Attr
dynamodb = boto3.resource('dynamodb',region_name='us-east-1')
table = dynamodb.Table('Visitors')
def lambda_handler(event, context):
dbresponse = table.update_item(
Key={'ID': 0},
UpdateExpression="ADD #ct :increment",
ExpressionAttributeNames={'#ct': 'visitorcount'},
ExpressionAttributeValues={':increment': 1},
ReturnValues = "UPDATED_NEW"
)
visitorCount = int(dbresponse["Attributes"]["visitorcount"])
print('visitorCount obtained from table = ' + str(visitorCount))
测试时lambda错误
测试事件名称 MyCodetest
Response
{
*"errorMessage": "name 'dbresponse' is not defined",
"errorType": "NameError",*
"requestId": "",
"stackTrace": [
" File \"/var/lang/lib/python3.9/importlib/__init__.py\", line 127, in import_module\n return _bootstrap._gcd_import(name[level:], package, level)\n",
" File \"<frozen importlib._bootstrap>\", line 1030, in _gcd_import\n",
" File \"<frozen importlib._bootstrap>\", line 1007, in _find_and_load\n",
" File \"<frozen importlib._bootstrap>\", line 986, in _find_and_load_unlocked\n",
" File \"<frozen importlib._bootstrap>\", line 680, in _load_unlocked\n",
" File \"<frozen importlib._bootstrap_external>\", line 850, in exec_module\n",
" File \"<frozen importlib._bootstrap>\", line 228, in _call_with_frames_removed\n",
" *File \"/var/task/lambda_function.py\", line 18, in <module>\n visitorCount = int(dbresponse[\"Attributes\"][\"visitorcount\"])\n"*
我谷歌搜索了无济于事,任何帮助将不胜感激。我敢肯定,这是很小的,但不是程序员,发现这个障碍很难克服。
I have a simple script to update a value in DynamoDB, and then give me back the updated value. If I run the script in VSCode, talking to AWS, it works fine - no errors. But when I format it for Lambda, I get an error.
VSCode
import boto3
from boto3.dynamodb.conditions import Key, Attr
dynamodb = boto3.resource('dynamodb',region_name='us-east-1')
table = dynamodb.Table('Visitors')
response = table.update_item(
Key={'ID': 0},
UpdateExpression="ADD #ct :increment",
ExpressionAttributeNames={'#ct': 'visitorcount'},
ExpressionAttributeValues={':increment': 1},
ReturnValues = "UPDATED_NEW"
)
visitorCount = int(response["Attributes"]["visitorcount"])
print('visitorCount obtained from table = ' + str(visitorCount))
Lambda
import json
import boto3
from boto3.dynamodb.conditions import Key, Attr
dynamodb = boto3.resource('dynamodb',region_name='us-east-1')
table = dynamodb.Table('Visitors')
def lambda_handler(event, context):
dbresponse = table.update_item(
Key={'ID': 0},
UpdateExpression="ADD #ct :increment",
ExpressionAttributeNames={'#ct': 'visitorcount'},
ExpressionAttributeValues={':increment': 1},
ReturnValues = "UPDATED_NEW"
)
visitorCount = int(dbresponse["Attributes"]["visitorcount"])
print('visitorCount obtained from table = ' + str(visitorCount))
Lambda error when testing
Test Event Name
mycodetest
Response
{
*"errorMessage": "name 'dbresponse' is not defined",
"errorType": "NameError",*
"requestId": "",
"stackTrace": [
" File \"/var/lang/lib/python3.9/importlib/__init__.py\", line 127, in import_module\n return _bootstrap._gcd_import(name[level:], package, level)\n",
" File \"<frozen importlib._bootstrap>\", line 1030, in _gcd_import\n",
" File \"<frozen importlib._bootstrap>\", line 1007, in _find_and_load\n",
" File \"<frozen importlib._bootstrap>\", line 986, in _find_and_load_unlocked\n",
" File \"<frozen importlib._bootstrap>\", line 680, in _load_unlocked\n",
" File \"<frozen importlib._bootstrap_external>\", line 850, in exec_module\n",
" File \"<frozen importlib._bootstrap>\", line 228, in _call_with_frames_removed\n",
" *File \"/var/task/lambda_function.py\", line 18, in <module>\n visitorCount = int(dbresponse[\"Attributes\"][\"visitorcount\"])\n"*
I've Googled to no avail, any help would be greatly appreciated. I'm sure it's something small, but not being a programmer, finding this hurdle very difficult to overcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论