Search code examples
javascriptnode.jsamazon-web-servicesamazon-simple-email-service

SignatureDoesNotMatch error when sending an email from Node AWS SDK (Simple Email Service)


I try to send an email with the @aws-sdk/client-ses SDK in Node but I get:

SignatureDoesNotMatch: The request signature we calculated does not match the signature you provided. Check your AWS Secret Access Key and signing method. Consult the service documentation for details.

Here is my code:

const aws = require("@aws-sdk/client-ses");

async function main() {
  const ses = new aws.SES({
    region: "eu-west-3",
    credentials: {
      accessKeyId: "REDACTED",
      secretAccessKey: "REDACTED"
    }
  });
  await ses.sendEmail({
    Destination: {
      ToAddresses: ["[email protected]"]
    },
    Message: {
      Subject: {
        Charset: "UTF-8",
        Data: "Test email"
      },
      Body: {
        Text: {
          Charset: "UTF-8",
          Data: "This is the message body in text format."
        }
      }
    },
    Source: "[email protected]"
  });
}

main().catch(console.error);

Is there something wrong with this code?

Thank you


Solution

  • OK I found my mistake: I was using the access key generated with the user, that is in fact a SMTP login/password, not an access key that you can use in the SDK (even if it appears under access keys in IAM).

    The solution is to create a new access key in IAM, then I used nodemailer (because otherwise my code used the action ses:SendEmail instead of ses:SendRawEmail)

    const nodemailer = require("nodemailer");
    const aws = require("@aws-sdk/client-ses");
    
    async function main() {
      let transporter = nodemailer.createTransport({
        SES: {
          ses: new aws.SES({
            region: "eu-west-3",
            credentials: {
              accessKeyId: "REDACTED",
              secretAccessKey: "REDACTED"
            }
          }),
          aws
        }
      });
      await transporter.sendMail({
        from: "[email protected]",
        to: "[email protected]",
        subject: "Test email",
        text: "This is the message body in text format."
      });
    }
    
    main().catch(console.error);