繁体   English   中英

从AWS Lambda访问AWS DynamoDB时出错

[英]Getting error while accessing AWS DynamoDB from AWS Lambda

我在lambda中遵循boto3代码以通过STS凭据访问dynamo db表时收到错误。 代码如下

import boto3
def lambda_handler(event, context):
sts_client = boto3.client('sts')

# Call the assume_role method of the STSConnection object and pass the role
# ARN and a role session name.
assumedRoleObject = sts_client.assume_role(
    RoleArn="arn:aws:iam::012345678910:role/test_role",
    RoleSessionName="AssumeRoleSession1"
)

# From the response that contains the assumed role, get the temporary
# credentials that can be used to make subsequent API calls
credentials = assumedRoleObject['Credentials'] 


dynamoDB = boto3.resource('dynamodb',aws_access_key_id=credentials['AccessKeyId'],aws_secret_access_key=credentials['SecretAccessKey'],aws_session_token=credentials['SessionToken'],)
test1=dynamoDB.get_available_subresources

table = dynamoDB.Table('Test1')

response = table.get_item(
    Key={
            'Name': 'ABC'
        }
    )

错误堆栈跟踪如下:

ResourceNotFoundException: An error occurred (ResourceNotFoundException) when calling the GetItem operation: Requested resource not found

首先检查表“ Test1”是否存在

这个文档:

ResourceNotFoundException

消息:找不到请求的资源。

示例:正在请求的表不存在,或者处于CREATING状态为时过早。

使用list-tables命令检查该表是否存在:

aws dynamodb list-tables

验证您的CLI默认区域是否与表的区域相同

如果该表确实存在,请检查您的cli配置,以验证您要查询的表所在的区域是否相同。 您可以像这样检查默认区域:

aws configure get region

您可以使用aws configure更改默认设置,或直接在任何CLI命令上指定--region来覆盖默认区域。

由于表不存在,我通常会收到该错误。

校验:

  1. 表格存在且拼写正确
  2. 您正在访问表所在的正确DynamoDB实例
  3. 您正在使用的角色有权访问表

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM