Search code examples
javascriptnode.jsamazon-ses

ConfigError: Missing region in config when i am using amazon ses with Node.js


I have used an env file and i used the structure like this and getting this error how to resolve that

"AWS_SES_REGION":"us-east-1" and i have put us-west-2 also but still getting the same error
"AWS_ACCESS_KEY_ID":"value"
"AWS_SECRET_KEY":"value"

and here is the code which i am using to send the email can anyone suggest me how to solve this problem

require('dotenv').config();
const AWS = require('aws-sdk');


const SESConfig = {
    apiVersion:"2010-12-01",
    accessKeyId:process.env.AWS_SECRET_KEY,
    accessSecretKey:process.env.AWS_SECRET_KEY,
    region:process.env.AWS_SES_REGION   
}

// AWS.SESConfig.update({region: 'eu-central-1'});

var params = {
    Source: 'xyz045@gmail.com',
    Destination: {
      ToAddresses: [
        'yyy45@gmail.com'
      ]
    },
    ReplyToAddresses: [
      'xyz05@gmail.com',
    ],
    Message: {
      Body: {
        Html: {
          Charset: "UTF-8",
          Data: 'IT IS <strong>WORKING</strong>!'
        }
      },
      Subject: {
        Charset: 'UTF-8',
        Data: 'Node + SES Example'
      }
    }
  };

  new AWS.SES(SESConfig).sendEmail(params).promise().then((res) => {
    console.log(res);
  }).catch(error => {
      console.log(error)
  });

Solution

  • Try to load the config using the AWS.Config class.

    Example:

    require('dotenv').config();
    const AWS = require('aws-sdk');
    
    
    
    const SESConfig = {
        apiVersion: "2010-12-01",
        accessKeyId: process.env.AWS_SECRET_KEY,
        accessSecretKey: process.env.AWS_SECRET_KEY,
        region: process.env.AWS_SES_REGION
    }
    
    let config = new AWS.Config(SESConfig); // Load the configuration like this. 
    /*
    Or you could update the config like this.
    AWS.config.update(SESConfig);
    */
    
    
    var params = {
        Source: 'xyz045@gmail.com',
        Destination: {
            ToAddresses: [
                'yyy45@gmail.com'
            ]
        },
        ReplyToAddresses: [
            'xyz05@gmail.com',
        ],
        Message: {
            Body: {
                Html: {
                    Charset: "UTF-8",
                    Data: 'IT IS <strong>WORKING</strong>!'
                }
            },
            Subject: {
                Charset: 'UTF-8',
                Data: 'Node + SES Example'
            }
        }
    };
    new AWS.SES().sendEmail(params).promise().then((res) => {
        console.log(res);
    }).catch(error => {
        console.log(error)
    });