Api stop working using nodejs and dynamodb

Viewed 125

I am working on a nodejs based api using aws dynamodb. API was working fine but today i am getting this error:

{
    "success": false,
    "message": {
        "message": "Missing region in config",
        "code": "ConfigError",
        "time": "2020-08-17T19:17:17.462Z"
    }
}

Below is my user api route:

const AWS = require('aws-sdk');
const config = require('config');
const { v4: uuidv4 } = require('uuid');
// Set the region
AWS.config.update({ region: 'us-east-2' });

router.post(`/api/user`, (req, res) => {
    console.log('User Add Services Called');

    try {
        //Constants
        AWS.config.update(config.aws_remote_config);

        var params = {
            TableName: config.aws_table_name,
            Item: {
                PK: `${uuidv4()}`,
                SK: `User`,
                name: 'Test'
            }
        };

        //Calling DynamoDB to add the item to the table
        docClient.put(params, function (err) {
            if (err) {
                res.send({
                    success: false,
                    message: err
                });
            } else {
                res.send({
                    success: true,
                    message: 'User Added'
                });
            }
        });
    } catch (error) {
        return res.status(500).json({
            status: 'error',
            message: 'An error occurred trying to process your request'
        });
    }
});

Some time these error shows too:

  • connect ENETUNREACH 169.254.169.254:80
  • Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

Can anyone tell why its happening, API was working fine & I also tried regenerating access keys too but the same issue is coming

1 Answers

I think you are re-configuring the AWS.config which is overwriting your initial config update that contains the region initialization. Try this:

const AWS = require('aws-sdk');
const config = require('config');
const { v4: uuidv4 } = require('uuid');

// Set the region
AWS.config.update({ region: 'us-east-2', ...config.aws_remote_config });

router.post(`/api/user`, (req, res) => {
    console.log('User Add Services Called');

    try {
        var params = {
            TableName: config.aws_table_name,
            Item: {
                PK: `${uuidv4()}`,
                SK: `User`,
                name: 'Test'
            }
        };

        //Calling DynamoDB to add the item to the table
        docClient.put(params, function (err) {
            if (err) {
                res.send({
                    success: false,
                    message: err
                });
            } else {
                res.send({
                    success: true,
                    message: 'User Added'
                });
            }
        });
    } catch (error) {
        return res.status(500).json({
            status: 'error',
            message: 'An error occurred trying to process your request'
        });
    }
});

EDIT

Then that means your credentials are missing simply do this:


// ... Other code

// Set the region
AWS.config.update({ 
    ...config.aws_remote_config,
    region: 'us-east-2',
    credentials: {
        accessKeyId: 'your-access-key',
        secretAccessKey: 'your-secret-key'
    } });

// ... Other code

Related