Search code examples
amazon-web-servicesaws-sdkaws-cliaws-sdk-jsaws-sdk-nodejs

Use AWS CLI profile for specific node program's context


I have several AWS profile that I use for managing different projects. Let's call the default one "user1", and the one I want to use "user2"

So if I just do a aws s3 ls it gives me user1's info of course, or if I do aws s3 ls --profile=user2 it gives me user2's, all good here

I also know that I can do export AWS_DEFAULT_PROFILE=user2 then aws s3 ls, which correctly gives me user2's buckets

The problem is, I have a Node application that when I run locally I want to use user2's profile. I've tried all manner of AWS_DEFAULT_PROFILE=user2 npm run dev and such but can't figure out how to do it?

Question How can I run a local Node program and have its AWS SDK use my desired profile?


Solution

  • It should normally work like below. You need to pass the profile name beforehand.

    AWS_PROFILE=user1 node script.js
    

    Ref: https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html