节点代码
// send message to aws sqs
const AWS = require('aws-sdk');
const sqs = new AWS.SQS({
apiVersion: '2012-11-05',
region: process.env.AWS_REGION,
credentials: new AWS.SharedIniFileCredentials(
{
disableAssumeRole: false,
},
),
});
const sendMessage = async (queueUrl, message) => {
console.log('region:', process.env.AWS_REGION, 'accessKeyId:',
process.env.AWS_ACCESS_KEY_ID, 'secretAccessKey:',
process.env.AWS_SECRET_ACCESS_KEY, 'AWS_SHARED_CREDENTIALS_FILE',
process.env.AWS_SHARED_CREDENTIALS_FILE, 'AWS_SDK_LOAD_CONFIG',
process.env.AWS_SDK_LOAD_CONFIG, 'AWS_PROFILE', process.env.AWS_PROFILE,
'AWS_CONFIG_FILE', process.env.AWS_CONFIG_FILE);
console.log(`sendMessage: ${queueUrl}, message: ${message}`);
const params = {
MessageBody: message,
QueueUrl: queueUrl,
};
await sqs.sendMessage(params).promise().then(
(data) => {
console.log(`sendMessage: ${data}`);
},
(err) => {
console.log(`sendMessage: ${err}`);
},
);
};
const deleteMessage = async (queueUrl, receiptHandle) => {
const params = {
QueueUrl: queueUrl,
ReceiptHandle: receiptHandle,
};
await sqs.deleteMessage(params).promise().then(
(data) => {
console.log(`deleteMessage: ${data}`);
},
(err) => {
console.log(`deleteMessage: ${err}`);
},
);
};
module.exports = {
sendMessage,
deleteMessage,
};
同样的代码在一个lambda中运行良好,但在另一个lambdasendMessage: CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1
我尝试过的东西:
- 尝试设置AWS_SDK_LOAD_CONFIG=1无效
- 尝试取消上面的设置:不起作用
- 尝试打印正在打印的评估密钥和密钥,但不起作用
- 我已经尝试了这个链接中的所有内容https://github.com/aws/aws-sdk-js/issues/1955
- 检查了所有权限,但这与权限无关,否则我会出现访问错误
- 尝试升级和降级aws-sdk版本
使用AWS Lambda,您不必担心访问密钥和机密。只需向Lambda的IAM角色授予必要的权限,并使用实例化您的SDK
const sqs = new AWS.SQS();
SDK将自动获取凭据。
如果您想确保SDK使用特定的API版本,请保留apiVersion
prop:
const sqs = new AWS.SQS({
apiVersion: '2012-11-05',
});
不要记录访问密钥和机密,这会带来安全风险。
要了解更多关于Lambda所扮演角色的信息,请查看文档。
使用内联凭据不是一个好的做法。作为一种更好的做法,删除所有凭据(access_key、secret_key、凭据配置文件等(可能会出现访问异常。授予lambda SendMessage(sqs:SendMessage(权限就可以了。
您必须尝试这种方式。如果你想发送消息,那么添加一个密钥,即参数中的消息
const params = {
AttributeNames: [
'SentTimestamp',
],
MaxNumberOfMessages: 5,
MessageAttributeNames: [
'All',
],
VisibilityTimeout: 15,
WaitTimeSeconds: 10,
QueueUrl":"your URL"
};
const sqsconfig = new AWS.Config({
accessKeyId: accessKeyId,
secretAccessKey: secretAccessKey,
region: s3AccessRegion,
sessionToken: sessionToken,
});
sqs.config = sqsconfig;
//here write a function to send-receive or delete message