Amplify-cli: amplify mock function ignore the configured profile for aws credentials

Created on 12 Apr 2020  路  5Comments  路  Source: aws-amplify/amplify-cli

Describe the bug
amplify mock function ignore the aws profile configured for the project, it uses the default one for credentials.
In the amplify config, we have useProfile: true & profileName:"sandbox".
AWS.config.credentials contains the 'default' profile credentials instead of the 'sandbox' profile credentials.

To Reproduce
Steps to reproduce the behavior:

  1. create an amplify project using a named aws profile (not default)
  2. create a function
  3. in the function, execute : console.log(AWS.config.credentials)
  4. Compare credentials in your .aws/credentials & credentials from the console log.

Expected behavior
Use the right credentials (named profile).

Additional context

~ $ amplify -v
4.18.0

Note: doesn't work even if profile variable is set: 'export AWS_PROFILE=sandbox'.

functions mock pending-triage

Most helpful comment

I was also super confused when I realized that mock was running towards my default profile instead of the profile defined in local-aws-info.

All 5 comments

Hello @guioum
Currently mock does not support adding environment variables, because of this the credentials are not introduced when invoking locally so it assumes the default credentials configured in your PATH.

Please follow this thread for updates on adding environment variables locally.
https://github.com/aws-amplify/amplify-cli/issues/691

@SwaySway I don't think this is accurate,

Currently mock does not support adding environment variables

Also, I don't think https://github.com/aws-amplify/amplify-cli/issues/691 is exactly the same as this topic.

Using the file function-parameters.json, I can provide stack parameters that I can use to assign environment variables in the cloudformation template, tested with amplify mock.

I am testing what happens when I assign the environment variable AWS_PROFILE via the stack parameters & env vars, with amplify mock. It might be necessary as I am struggling to invoke some AWS services without it.

I have not tested with amplify mock function.

@SwaySway , I agree with @cyrfer and @guioum I feel this is a slightly different issue. Yes, you're right it can be solved by supporting environment variables which we can then use to pass through credentials to mock but at the same time this feels like it should be default mock behaviour to be inline with all other AWS tools. I don't think its something the user should have to define.

If I have AWS_PROFILE set to a value, it should use that named profile's credential in my ~/.aws/credentials file, NOT the default, and certainly it should tell you when it's loading the profile (in this case default) if it doesn't exist, that it couldn't find xyz profile. If this error had said... no credentials found for [default] profile in ~/.aws/credentials this wouldn't have caused so much dev pain.

I dont actually normally have a default profile. I like to set them explicitly using AWS_PROFILE in tmux sessions so the no credentials loaded error was extremely confusing to me. Why would it arbritraily choose the default profile?

This should be updated to use the profile defined in amplify/.config/local-aws-info.json instead of using the default. All of the other amplify cli methods do that. Not sure why mock does not.

@SwaySway can this be reopened? I agree with @Mentioum, @cyrfer and @guioum that this is a new/different issue than you have addressed.

I was also super confused when I realized that mock was running towards my default profile instead of the profile defined in local-aws-info.

Was this page helpful?
0 / 5 - 0 ratings