Skip to content

Node.js AWS S3: Fix InvalidAccessKeyId error | ShillehTek

October 22, 2023

Video Tutorial (Optional)

Watch first if you want to follow along with the full walkthrough for fixing the AWS S3 InvalidAccessKeyId error in a Node.js app.

Project Overview

AWS S3 InvalidAccessKeyId fix in Node.js: In this guide, you will troubleshoot a Node.js application using Amazon S3 with the AWS SDK for JavaScript (aws-sdk), and fix the error: InvalidAccessKeyId: The AWS Key Id you provided does not exist in our records.

This came up after setting up the AWS SDK and attempting to upload a document to an S3 bucket from a Node component.

  • Time: 10 to 20 minutes
  • Skill level: Beginner
  • What you will build: A working Node.js to S3 upload setup that loads valid AWS credentials
Node.js app upload component configured to send documents to an Amazon S3 bucket
Example Node.js upload component that triggers the InvalidAccessKeyId error when credentials are not loaded correctly.

Parts List

From ShillehTek

  • None

External

  • Node.js project using the AWS SDK for JavaScript (aws-sdk)
  • An AWS account with access to IAM Security Credentials
  • An S3 bucket (already created or available)

Note: The AWS SDK for JavaScript automatically searches for the shared credentials file named credentials in the standard OS-specific path.

Step-by-Step Guide

Step 1 - Reinstall aws-sdk in your Node project

Goal: Ensure your local AWS SDK install is clean, especially if your npm setup is out of date.

What to do: Reinstall the AWS SDK package in your project.

Code:

npm install aws-sdk

Expected result: The dependency is reinstalled successfully and your project is ready to retry loading credentials.

Step 2 - Confirm the shared AWS credentials file path

Goal: Make sure the AWS SDK can find your credentials, since it auto-searches the shared credentials file named credentials.

What to do: Verify that the file exists at the correct location for your OS. If it does not exist, create it in the path below (do not use any other path).

  • The shared credentials file on Linux, Unix, and macOS: ~/.aws/credentials
  • The shared credentials file on Windows: C:\Users\USER_NAME\.aws\credentials
Example AWS shared credentials file path and filename for SDK auto-discovery
Example of where the AWS shared credentials file is expected to be located.

Expected result: The AWS SDK can locate the shared credentials file from the standard path for your operating system.

Step 3 - Generate and save valid AWS access keys

Goal: Ensure the credentials file contains a valid access key id and secret access key that match an existing AWS IAM credential.

What to do: Edit the shared credentials file and fill in your access key id and secret access key. If you do not have keys yet, generate them from your AWS account.

To generate the credentials:

  • Go to the Security Credentials page in your AWS account (available from the dropdown after clicking your email in the top-right).
  • Create an access key, save it, and store it safely.
  • Insert the corresponding information into your credentials file and save the file.
AWS account Security Credentials page where access keys are created
Use the AWS Security Credentials page to create access keys.
AWS console view showing access key creation and details to copy into the credentials file
Create the access key, then copy the values into your shared credentials file.

Expected result: Your shared credentials file contains valid keys, and subsequent S3 upload attempts no longer throw InvalidAccessKeyId.

Conclusion

You fixed the AWS S3 InvalidAccessKeyId issue in a Node.js app by reinstalling aws-sdk and making sure the AWS SDK is loading valid keys from the shared credentials file path. In many cases, these steps resolve the mismatch that triggers the error.

Want parts and tools for your next build? Shop at ShillehTek.com. If you want help designing or integrating an IoT or cloud-connected system for your product, check out our IoT consulting services.