sendMessage:CredentialsError:配置中缺少凭证,如果使用 AWS_CONFIG_FILE,请在 AWS Lambda 上设置 AWS_SDK_LOAD_CONFIG=1

发布于 2025-01-17 01:56:15 字数 2106 浏览 1 评论 0原文

节点代码

// 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 中工作正常,但在另一个 lambda 中我得到 sendMessage: CredentialsError: 配置中缺少凭证,如果使用 AWS_CONFIG_FILE,请设置 AWS_SDK_LOAD_CONFIG=1

我尝试过的事情:

  1. 尝试设置 AWS_SDK_LOAD_CONFIG=1 不起作用
  2. 尝试取消上面的设置:不起作用
  3. 尝试打印评估密钥和秘密密钥正在打印,但不起作用
  4. 我已经尝试了此链接中的所有操作 https://github.com/aws/aws-sdk-js/issues/1955< /a>
  5. 检查了所有权限,但这不是权限相关的问题,否则我会遇到访问错误,
  6. 也尝试升级和降级 aws-sdk 版本。

Node Code

// 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,
};

The same code is working fine in one lambda, but in another I am getting
sendMessage: CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

Things I have tried:

  1. tried setting AWS_SDK_LOAD_CONFIG=1 didn't work
  2. tried unsetting above: didn't work
  3. tried printing the assess key and secret key they are getting printed, but not working
  4. I have tried all things in this link https://github.com/aws/aws-sdk-js/issues/1955
  5. Checked all permissions but it's not permission related issue otherwise I would have got access error,
  6. tried upgrading and downgrading aws-sdk version as well.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

勿挽旧人 2025-01-24 01:56:15

使用 AWS Lambda,您不必担心访问密钥和秘密。只需向 Lambda 的 IAM 角色授予必要的权限,然后使用

const sqs = new AWS.SQS();

SDK 实例化您的 SDK,SDK 将自动获取凭证。

如果您想确保 SDK 使用特定的 API 版本,请保留 apiVersion 属性:

const sqs = new AWS.SQS({
    apiVersion: '2012-11-05',
});

不要记录访问密钥和机密,这存在安全风险。

要了解有关 Lambda 所承担角色的更多信息,请查看

With AWS Lambda, you don't have to worry about the access key and secret. Simply grant the necessary rights to the Lambda's IAM role, and instantiate your SDK with

const sqs = new AWS.SQS();

The SDK will pick up the credentials automatically.

If you want to make sure the SDK uses a specific API version, keep the apiVersion prop:

const sqs = new AWS.SQS({
    apiVersion: '2012-11-05',
});

Don't log the access key and secret, that's a security risk.

To read more about the role that Lambda assumes, check the documentation.

我纯我任性 2025-01-24 01:56:15

使用内联凭据不是一个好习惯。作为更好的做法,删除所有凭据(access_key、secret_key、凭据配置文件等)可能会出现访问异常。
授予 lambda SendMessage (sqs:SendMessage) 权限,然后就可以开始了。

Its not a good practice to use inline credentials. As a better practice remove all credentials (access_key, secret_key, credentials profile etc) probably you will get access exception.
Grant the lambda SendMessage (sqs:SendMessage) permissions and you are good to go.

脱离于你 2025-01-24 01:56:15

你必须尝试这种方式。如果你想发送消息,那么在 params 中添加一个 key,即 message

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

you have to try this way. if you want to send message then add one key which is message in params

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文