【发布时间】:2023-03-28 04:44:01
【问题描述】:
让我从我想要实现的目标的总体描述开始。我正在使用 Lambda、Cognito(联合身份)、API Gateway 等构建无服务器 API。我使用 aws_iam 作为 API Gateway 中的授权方。在某些端点中,我需要访问例如用户电子邮件或用户名或其他任何内容,以便我可以在响应中将其发回(还有未提出请求的用户的数据)。我想我正在寻找对身份池的某种“管理员”访问权限,以便我可以根据 cognitoIdentityId 检索数据。
现在,就我而言,这些数据存储在 Cognito 的数据集中。问题是,如何从我的 Lambda 函数 (node.js) 访问这些数据?这是一个好方法吗?我应该使用其他东西而不是数据集吗?有没有可行的例子?
如有必要,我很乐意提供更多详细信息。
谢谢
编辑#1:
这是我的 lambda 函数的代码:
module.exports.getDataSet = (event, context, callback) => {
console.log("event: " + JSON.stringify(event));
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: IDENTITY_POOL_ID
});
try {
AWS.config.credentials.get(function() {
var client = new AWS.CognitoSync();
var params = {
DatasetName: 'userinfo',
IdentityId: event.requestContext.identity.cognitoIdentityId,
IdentityPoolId: IDENTITY_POOL_ID
};
client.listRecords(params, function (err, data) {
if (err) {
console.log(JSON.stringify(err));
} else {
console.log(data);
}
});
});
} catch (ex) {
callback(ex);
}
};
这就是我在调用listRecords 时得到的err:
{
"message": "Missing credentials in config",
"code": "CredentialsError",
"time": "2017-05-26T08:42:39.298Z",
"requestId": "46712a9b-41ef-11e7-9e3c-074afafb3349",
"statusCode": 400,
"retryable": false,
"retryDelay": 21.688148977111666,
"originalError": {
"message": "Could not load credentials from CognitoIdentityCredentials",
"code": "CredentialsError",
"time": "2017-05-26T08:42:39.298Z",
"requestId": "46712a9b-41ef-11e7-9e3c-074afafb3349",
"statusCode": 400,
"retryable": false,
"retryDelay": 21.688148977111666,
"originalError": {
"message": "Unauthenticated access is not supported for this identity pool.",
"code": "NotAuthorizedException",
"time": "2017-05-26T08:42:39.298Z",
"requestId": "46712a9b-41ef-11e7-9e3c-074afafb3349",
"statusCode": 400,
"retryable": false,
"retryDelay": 21.688148977111666
}
}
}
编辑 #2:
通过删除解决
AWS.config.credentials = new AWS.CognitoIdentityCredentials({
IdentityPoolId: IDENTITY_POOL_ID
});
从代码中并将 AmazonCognitoReadOnly 策略添加到调用 lambda 的角色中。
【问题讨论】:
标签: node.js amazon-web-services aws-lambda serverless-framework