python在AWS lamda中运行的“ XXXX未定义”错误

发布于 2025-02-10 03:25:20 字数 2458 浏览 1 评论 0原文

我有一个简单的脚本来更新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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文