用户无权执行:dynamodb:Query 从本地 lambda 函数访问数据库时发生资源错误
User is not authorized to perform: dynamodb:Query on resource error when accessing db from local lambda function
我正在尝试 运行 使用 SAM 模板在本地 运行 时间 node.js 新创建的 lambda 函数。
我有以下内容:
a) 具有区域、accessKeyId 和 secretAccessKey 的 aws 帐户
b) aws-cli, aws-sam, docker
运行 在本地使用 sam local invoke 的 lambda 函数很好,但问题是当我在我的函数中连接到 dynamodb 时,出现以下错误。
{"message":"User: arn:aws:iam::*********:user/test.user is not authorized to perform: dynamodb:Query on resource: arn:aws:dynamodb:us-west-2:********:table/my_table with an explicit deny","code":"AccessDeniedException","time":"2020-08-30T07:45:51.678Z","requestId":"7SIBTHTKDSSDJNSTLNUSSBHDNDHBSMVJF66Q9ASUAAJG","statusCode":400,"retryable":false,"retryDelay":40.84164538820391}
我可以访问 aws 帐户并具有 accessKeyId 和 secretAccessKey 并且能够在从 aws 控制台尝试时进行查询。在 vscode 编辑器中,安装了 aws-toolkit 并添加了凭据,但我仍然在本地遇到相同的错误。
有人可以帮助我解决这个问题,因为我正在经历寻找解决方案的困难局面。
这是我的代码片段。
let response;
const AWS = require('aws-sdk');
AWS.config.update({
region: "us-west-2",
accessKeyId: '************',
secretAccessKey: '********************'
});
const dbClient = new AWS.DynamoDB.DocumentClient();
exports.lambdaHandler = async (event, context) => {
try {
let myTable = await getData(event.myId);
response = {
'statusCode': 200,
'body': JSON.stringify({
message: myTable
})
}
} catch (err) {
console.log(err);
return err;
}
return response
};
const MY_TABLE_NAME = "my_table";
const getData = async (myId) => {
const params = {
TableName: MY_TABLE_NAME ,
KeyConditionExpression: "#uid = :id",
ExpressionAttributeValues: {
':id': myId
},
ExpressionAttributeNames: {
'#uid': 'userID'
}
};
let { Count, Items } = await dbClient.query(params).promise();
if (Items.length == 0) {
return false;
} else {
var obj = {
Name: (Count > 0) ? Items[0].name : null,
MY_TABLE: MY_TABLE
};
return obj;
}
};
template.yaml
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs12.x
Policies: AmazonDynamoDBFullAccess
Events:
HelloWorld:
Type: Api # More info
Properties:
Path: /hello
Method: get
任何帮助将不胜感激。提前致谢。
我收到用户未授权错误的原因是因为我没有编程访问权限,只被授权访问 aws 控制台。
谢谢
我正在尝试 运行 使用 SAM 模板在本地 运行 时间 node.js 新创建的 lambda 函数。 我有以下内容:
a) 具有区域、accessKeyId 和 secretAccessKey 的 aws 帐户 b) aws-cli, aws-sam, docker
运行 在本地使用 sam local invoke 的 lambda 函数很好,但问题是当我在我的函数中连接到 dynamodb 时,出现以下错误。
{"message":"User: arn:aws:iam::*********:user/test.user is not authorized to perform: dynamodb:Query on resource: arn:aws:dynamodb:us-west-2:********:table/my_table with an explicit deny","code":"AccessDeniedException","time":"2020-08-30T07:45:51.678Z","requestId":"7SIBTHTKDSSDJNSTLNUSSBHDNDHBSMVJF66Q9ASUAAJG","statusCode":400,"retryable":false,"retryDelay":40.84164538820391}
我可以访问 aws 帐户并具有 accessKeyId 和 secretAccessKey 并且能够在从 aws 控制台尝试时进行查询。在 vscode 编辑器中,安装了 aws-toolkit 并添加了凭据,但我仍然在本地遇到相同的错误。
有人可以帮助我解决这个问题,因为我正在经历寻找解决方案的困难局面。 这是我的代码片段。
let response;
const AWS = require('aws-sdk');
AWS.config.update({
region: "us-west-2",
accessKeyId: '************',
secretAccessKey: '********************'
});
const dbClient = new AWS.DynamoDB.DocumentClient();
exports.lambdaHandler = async (event, context) => {
try {
let myTable = await getData(event.myId);
response = {
'statusCode': 200,
'body': JSON.stringify({
message: myTable
})
}
} catch (err) {
console.log(err);
return err;
}
return response
};
const MY_TABLE_NAME = "my_table";
const getData = async (myId) => {
const params = {
TableName: MY_TABLE_NAME ,
KeyConditionExpression: "#uid = :id",
ExpressionAttributeValues: {
':id': myId
},
ExpressionAttributeNames: {
'#uid': 'userID'
}
};
let { Count, Items } = await dbClient.query(params).promise();
if (Items.length == 0) {
return false;
} else {
var obj = {
Name: (Count > 0) ? Items[0].name : null,
MY_TABLE: MY_TABLE
};
return obj;
}
};
template.yaml
Resources:
HelloWorldFunction:
Type: AWS::Serverless::Function # More info
Properties:
CodeUri: hello-world/
Handler: app.lambdaHandler
Runtime: nodejs12.x
Policies: AmazonDynamoDBFullAccess
Events:
HelloWorld:
Type: Api # More info
Properties:
Path: /hello
Method: get
任何帮助将不胜感激。提前致谢。
我收到用户未授权错误的原因是因为我没有编程访问权限,只被授权访问 aws 控制台。
谢谢