DonJuanEco
DonJuanEco

Reputation: 143

AWS creds error when making calls from local react app "Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1"

I'm getting the following error when running my react app locally which make an api request to AWS via the AWS-SDK:

CredentialsError: Missing credentials in config, if using AWS_CONFIG_FILE, set AWS_SDK_LOAD_CONFIG=1

I've tried:

This is how I'm making the request:

import AWS from 'aws-sdk';
import { useState, useEffect } from 'react';

const ssm = new AWS.SSM({ region: 'eu-west-1' });

export const useFetchParams = (initialValue) => {
    const [result, setResult] = useState(initialValue);

    useEffect(() => {
        const params = {
            Path: '/',
            MaxResults: '2',
            Recursive: true,
            WithDecryption: true
        };

        ssm.getParametersByPath(params, function (err, data) {
            if (err) console.log(err, err.stack);
            else setResult(data);
        });
    }, []);

    return result;
};

export default useFetchParams;

Any help would be massively appreciated. Thanks.

Upvotes: 3

Views: 2081

Answers (2)

jbnunn
jbnunn

Reputation: 6355

It's a bad practice to hardcode credentials like this. I'd use a library like dotenv, then creating an .env.local file with your credentials, like so:

REACT_APP_AWS_ACCESS_KEY_ID=YOUR-KEY
REACT_APP_AWS_SECRET_ACCESS_KEY=YOUR-ACCESS-ID

In your script, configure the AWS SDK with:

AWS.config.update({
    accessKeyId: process.env.REACT_APP_AWS_ACCESS_KEY_ID,
    secretAccessKey: process.env.REACT_APP_AWS_SECRET_ACCESS_KEY,
    region: 'us-east-1', 
});

Upvotes: 0

Abdeen
Abdeen

Reputation: 932

The error states that credentials are missing so it is an authentication issue, you can try setting the accessKeyId and secretAccessKey or the credentials fields directly on the SSM constructor.

So simply maintain your code as is, just make the following change:

// From
const ssm = new AWS.SSM({ region: 'eu-west-1' });

// To
const ssm = new AWS.SSM({
    region: 'eu-west-1',
    accessKeyId: 'your-access-key',
    secretAccessKey: 'your-secret-key'
});

// Or To
const ssm = new AWS.SSM({
    region: 'eu-west-1',
    credentials: {
        accessKeyId: 'your-access-key',
        secretAccessKey: 'your-secret-key'
    }
});

Upvotes: 4

Related Questions