ottokruse / s3-spa-upload

Upload a single page application to S3 with the right content-type and cache-control meta-data
22 stars 3 forks source link

Allow AWS profile to be specified in the CLI #10

Open arnaldop opened 5 months ago

arnaldop commented 5 months ago

Since the app is leveraging AWS code, is it possible to leverage environment variables and files to specify the AWS credentials so that this can be run from the command line with the AWS profile instead of having to write the code? Thx

ottokruse commented 5 months ago

Yeah that should work. You can pass the flag '--profile myprofile' to choose a profile. Setting env var AWS_PROFILE should work too, and other usual env vars that AWS SDKs read (eg AWS_ACCESS_KEY_ID).

arnaldop commented 5 months ago

Great to hear, but for some reason it's not working.

Partial dump of my credentials file:

16:44:07 › head -n 12 ~/.aws/credentials
[default]
aws_access_key_id = access_key
aws_secret_access_key = secret_key

[localstack]
aws_access_key_id = access_key_localstack
aws_secret_access_key = secret_key

[live]
aws_access_key_id = real_access_key
aws_secret_access_key = real_secret_key

Output of command:

› s3-spa-upload --profile live build agencycomp-stage-app
Upload started of 40 files (with concurrency: 100)
/Users/arnaldopiccinelli/.nvm/versions/node/v20.7.0/lib/node_modules/s3-spa-upload/node_modules/@smithy/smithy-client/dist-cjs/index.js:838
  const response = new exceptionCtor({
                   ^

InvalidAccessKeyId: The AWS Access Key Id you provided does not exist in our records.
    at throwDefaultError (/Users/arnaldopiccinelli/.nvm/versions/node/v20.7.0/lib/node_modules/s3-spa-upload/node_modules/@smithy/smithy-client/dist-cjs/index.js:838:20)
    at /Users/arnaldopiccinelli/.nvm/versions/node/v20.7.0/lib/node_modules/s3-spa-upload/node_modules/@smithy/smithy-client/dist-cjs/index.js:847:5
    at de_PutObjectCommandError (/Users/arnaldopiccinelli/.nvm/versions/node/v20.7.0/lib/node_modules/s3-spa-upload/node_modules/@aws-sdk/client-s3/dist-cjs/index.js:5741:10)
    at processTicksAndRejections (node:internal/process/task_queues:95:5)
    at runNextTicks (node:internal/process/task_queues:64:3)
    at listOnTimeout (node:internal/timers:540:9)
    at process.processTimers (node:internal/timers:514:7)
    at async /Users/arnaldopiccinelli/.nvm/versions/node/v20.7.0/lib/node_modules/s3-spa-upload/node_modules/@smithy/middleware-serde/dist-cjs/index.js:35:20
    at async /Users/arnaldopiccinelli/.nvm/versions/node/v20.7.0/lib/node_modules/s3-spa-upload/node_modules/@aws-sdk/middleware-signing/dist-cjs/index.js:184:18
    at async /Users/arnaldopiccinelli/.nvm/versions/node/v20.7.0/lib/node_modules/s3-spa-upload/node_modules/@smithy/middleware-retry/dist-cjs/index.js:320:38 {
  '$fault': 'client',
  '$metadata': {
    httpStatusCode: 403,
    requestId: 'PW7FX6WJBZKKNJZQ',
    extendedRequestId: 'GhjeJpur0QV6yUpZsXZj8Ee4y6728nqNXABvG26HutXk3eMvpEzaqCLj51XgAYeR37ZMEBbBnEc=',
    cfId: undefined,
    attempts: 1,
    totalRetryDelay: 0
  },
  Code: 'InvalidAccessKeyId',
  AWSAccessKeyId: 'access_key',
  RequestId: 'PW7FX6WJBZKKNJZQ',
  HostId: 'GhjeJpur0QV6yUpZsXZj8Ee4y6728nqNXABvG26HutXk3eMvpEzaqCLj51XgAYeR37ZMEBbBnEc='
}

Node.js v20.7.0

Note how towards the bottom, it says that the AWSAccessKeyId is access_key when it should be real_access_key. So it's picking up whatever is first in ~/.aws/credentials, not the named profile.

Interestingly, using the environment variable worked fine:

AWS_PROFILE=live s3-spa-upload build agencycomp-stage-app
Upload started of 40 files (with concurrency: 100)
Uploaded s3://agencycomp-stage-app/images/data_not_found.svg | cache-control=<empty> | content-type=image/svg+xml
ottokruse commented 5 months ago

Okay good to know AWS_PROFILE env var works. Will check why the --profile flag doesn't work. Maybe I should just remove the flag and document the use of AWS_PROFILE more clearly.

arnaldop commented 5 months ago

Thanks, @ottokruse!

All things being equal, I would say the profile flag is the cleaner, more standard way to accomplish this. But either way, documenting it would be great.

Thanks again for the amazing module!!!