简体   繁体   English

从AWS Lambda访问AWS DynamoDB时出错

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

I am getting the error for following boto3 code in lambda to access dynamo db table through STS credentials. 我在lambda中遵循boto3代码以通过STS凭据访问dynamo db表时收到错误。 The code is below 代码如下

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'
        }
    )

The error stacktrace is below: 错误堆栈跟踪如下:

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

First check whether Table "Test1" exists 首先检查表“ Test1”是否存在

From this documentation: 这个文档:

ResourceNotFoundException ResourceNotFoundException

Message: Requested resource not found. 消息:找不到请求的资源。

Example: Table which is being requested does not exist, or is too early in the CREATING state. 示例:正在请求的表不存在,或者处于CREATING状态为时过早。

Check whether this table exists with the list-tables command: 使用list-tables命令检查该表是否存在:

aws dynamodb list-tables

Verify whether your CLI default region is the same as your table's region 验证您的CLI默认区域是否与表的区域相同

If this table does exist, check your cli configuration to verify that you're querying in the same region that the table exists. 如果该表确实存在,请检查您的cli配置,以验证您要查询的表所在的区域是否相同。 You can check your default region like this: 您可以像这样检查默认区域:

aws configure get region

You can use aws configure to change your default settings, or specify --region directly on any CLI command to override your default region. 您可以使用aws configure更改默认设置,或直接在任何CLI命令上指定--region来覆盖默认区域。

I have gotten that error often usually due to the table not existing. 由于表不存在,我通常会收到该错误。

Check: 校验:

  1. Table exists and spelling is correct 表格存在且拼写正确
  2. You are accessing the right DynamoDB instance where your table exists 您正在访问表所在的正确DynamoDB实例
  3. The role you are using has access to the table 您正在使用的角色有权访问表

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

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