Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mock function not using mock API #6384

Closed
dmost714 opened this issue Jan 13, 2021 · 14 comments
Closed

Mock function not using mock API #6384

dmost714 opened this issue Jan 13, 2021 · 14 comments
Assignees
Labels
feature-request Request a new feature mock Issues tied to the mock functionality

Comments

@dmost714
Copy link

Note: If your issue/bug is regarding the AWS Amplify Console service, please log it in the
Amplify Console GitHub Issue Tracker

Describe the bug
When testing my lambda function via mock, the API_MYPROJECT_GRAPHQLAPIENDPOINTOUTPUT environment variable points to amazonaws.com even though the mock api server is running.

Amplify CLI Version
4.41.1

To Reproduce
Terminal 1: amplify mock
Terminal 2: amplify status
Terminal 3: amplify mock function mylambda --event src/event.json --timeout 7

Expected behavior
amplify status should show the AppSync mock endpoint that's running.
The API_MYPROJECT_GRAPHQLAPIENDPOINTOUTPUT environment variable should point to the mock endpoint.

Desktop (please complete the following information):

  • OS: Mac BigSur 11.1 Macbook Pro M1
  • Node Version v14.12.0

Additional context
The react app DOES correctly switch between the cloud and the mock endpoint.
GraphiQL explorer runs on localhost.

Thanks

@attilah attilah added mock Issues tied to the mock functionality pending-triage Issue is pending triage enhancement and removed pending-triage Issue is pending triage labels Jan 14, 2021
@attilah
Copy link
Contributor

attilah commented Jan 14, 2021

@dmost714 just to make sure I understand correctly you have a Lambda that is configured to access the GraphQL API and when mock server is running you would like that mock function should invoke the mocked and not the deployed GraphQL API?

The two 'mocks' are different processes as the invocations as well, but it seems to be a useful enhancement

@dmost714
Copy link
Author

@attilah Exactly correct.

I'd like to do all my testing locally. The webapp (react) is using the mocked graphql and S3. I have an amplify generated lambda function (a webhook that will be called by a 3rd party). When I mock test the function, it always writes data to the cloud instead of the local gql server.

I have 3 terminals, one in Visual Studio running the webapp, one running "amplify mock" for api & storage, and one to mock trigger the lambda function.

Thank you, I'm enjoying using Amplify very much.

-d-

@dmost714
Copy link
Author

Now that I understand this is an enhancement, I see the documentation talks about updating the web/mobile app config, but not the Lambda's config.

To help anyone finding this thread before the enhancement is added...

The environment variable will not be overwritten by prefixing it to the command:
API_AMPYMAIL_GRAPHQLAPIENDPOINTOUTPUT="http://192.168.1.226:20002" amplify mock function mylambda --event src/event.json --timeout 7

However, using a different environment variable is a perfectly acceptable workaround.
It is also necessary to set the API KEY if you're using one:
MOCK_GQL_ENDPOINT="http://192.168.1.226:20002/graphql" MOCK_API_KEY="da2-fakeApiId123456" amplify mock function MyLambda --event src/event.json --timeout 7

And add this at the beginning of your lambda:

if (process.env.MOCK_GQL_ENDPOINT) {
    process.env.API_XXXX_GRAPHQLAPIENDPOINTOUTPUT = process.env.MOCK_GQL_ENDPOINT
    process.env.API_XXXX_GRAPHQLAPIKEYOUTPUT = process.env.MOCK_API_KEY
}

Thanks.

@attilah
Copy link
Contributor

attilah commented Jan 14, 2021

Since our goal to make this low friction for developers we probably will want to provide an automated way to make this happen and an additional flag for mock function to disable this behavior.

It is a bearable workaround I think, for now.

@dmost714
Copy link
Author

This ticket is still open, but the behavior appears to have changed.
Was I hallucinating before, or now?
I'm running CLI v4.44.0.

  1. Prefixing amplify mock function MyLambda ... with environment variables no longer appears to work. The vars aren't appearing in process.env anymore.
  2. However, process.env.API_XXXXX_GRAPHQLAPIENDPOINTOUTPUT is now being populated with 'http://localhost:20002/graphql' when the mock server is running. I could swear it wasn't before.
  3. EXCEPT, the API_XXXXX_GRAPHQLAPIKEYOUTPUT value is stuck on the cloud/non-mock value so authentication is failing when running the mock server locally. If I overwrite it to 'da2-fakeApiId123456' in code, my auth works locally.

Hope the above helps.

@dmost714
Copy link
Author

OK, did some regression testing and can confirm the behavior changed in CLI v4.44.0.

Amplify CLI v4.41.0, 4.41.1, 4.41.2, 4.42.0, 4.43.0 all accept environment vars from the command line. They all issue a warning when I run in my environment to the effect of Could not find ref for "....". Using unsubstituted value., and they all point to the 'cloud' GQL even when I'm running mock.

Starting with CLI v4.44.0, I no longer get the 'Could not find ref' warning when running amplify mock and the proces.env vars in my lambda are different. I no longer get the environment vars passed in on the command line, but the GRAPHQLAPI vars are changing to point to the local mock server (yay). Unfortunately, the api key is not changing, so my auth is failing.

v4.43.0 (LOCALENV:123456 found)

process.env {
  __CFBundleIdentifier: 'com.apple.Terminal',
  TMPDIR: '/var/folders/h9/jv9vj58d64s9vbzngq1vd38m0000gn/T/',
  XPC_FLAGS: '0x0',
  LaunchInstanceID: '536A9EFA-62A9-4769-81E6-59D614729157',
  TERM: 'xterm-256color',
  SSH_AUTH_SOCK: '/private/tmp/com.apple.launchd.aEjpfylFc0/Listeners',
  SECURITYSESSIONID: '186b8',
  XPC_SERVICE_NAME: '0',
  TERM_PROGRAM: 'Apple_Terminal',
  TERM_PROGRAM_VERSION: '440',
  TERM_SESSION_ID: '03E3FB3E-A4BC-4F3F-89CF-A49F6308E838',
  SHELL: '/bin/zsh',
  HOME: '/Users/username',
  LOGNAME: 'username',
  USER: 'username',
  PATH: '/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Library/Apple/usr/bin',
  SHLVL: '1',
  PWD: '/Users/username/dev/xxxxx',
  OLDPWD: '/Users/username/dev',
  LANG: 'en_US.UTF-8',
  LOCALENV: '123456',
  _: '/usr/local/bin/amplify',
  __CF_USER_TEXT_ENCODING: '0x1F5:0x0:0x0',
  ENV: 'dev',
  REGION: 'us-east-1-fake',
  API_xxxxx_GRAPHQLAPIIDOUTPUT: '6xvvoju5hnhjhenx3wrymmohxa',
  API_xxxxx_GRAPHQLAPIENDPOINTOUTPUT: 'https://wwutavkk6fbalnqsk33ot7vk2y.appsync-api.us-east-2.amazonaws.com/graphql',
  API_xxxxx_GRAPHQLAPIKEYOUTPUT: 'da2-v2rzyvg5cze5ji2sknwa2j6fxm',
  AUTH_xxxxxC92C22E5_USERPOOLID: 'us-east-2_3mb5qnXhK'
}

v4.44.0 (LOCALENV:123456 missing):

process.env {
  ENV: 'dev',
  REGION: 'us-east-2',
  API_XXXXX_GRAPHQLAPIIDOUTPUT: '6xvvoju5hnhjhenx3wrymmohxa',
  API_XXXXX_GRAPHQLAPIENDPOINTOUTPUT: 'http://localhost:20002/graphql',
  API_XXXXX_GRAPHQLAPIKEYOUTPUT: 'da2-v2rzyvg5cze5ji2sknwa2j6fxm',
  AUTH_XXXXXC92C22E5_USERPOOLID: 'us-east-2_3mb5qnXhK',
  AWS_ACCESS_KEY_ID: 'AKIAYY7SXJJ6SWHS4HH7',
  AWS_SECRET_ACCESS_KEY: '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',
  _X_AMZN_TRACE_ID: 'amplify-mock-x-amzn-trace-id',
  AWS_EXECUTION_ENV: 'AWS_Lambda_amplify-mock',
  AWS_LAMBDA_FUNCTION_MEMORY_SIZE: '128',
  AWS_LAMBDA_FUNCTION_VERSION: '1',
  AWS_LAMBDA_INITIALIZATION_TYPE: 'on-demand',
  AWS_LAMBDA_LOG_GROUP_NAME: 'amplify-mock-aws-lambda-log-group-name',
  AWS_LAMBDA_LOG_GROUP_STREAM_NAME: 'amplify-mock-aws-lambda-log-group-stream-name',
  TZ: 'UTC',
  _HANDLER: 'index.handler',
  AWS_REGION: 'us-east-2',
  AWS_LAMBDA_FUNCTION_NAME: 'myLambda',
  LAMBDA_TASK_ROOT: '/Users/username/dev/xxxxx/amplify/backend/function/myLambda',
  LAMBDA_RUNTIME_DIR: '/Users/username/dev/xxxxx/amplify/backend/function/myLambda',
  __CF_USER_TEXT_ENCODING: '0x1F5:0x0:0x0'
}

@dmost714
Copy link
Author

Just notice the new environment vars have my AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. Ugh. Deactived.

@m2broth
Copy link

m2broth commented Mar 2, 2021

@dmost714 Hi, Have you found a workaround for how to solve this? Trying to find some automated way but it seems to be impossible now

@dmost714
Copy link
Author

dmost714 commented Mar 2, 2021

@m2broth Yes, my workaround it to overwrite the APIKEYOUTPUT variable when mock is running.

In index.js I add the following:

exports.handler = async (event) => {
    //console.log('process.env', process.env)
    //console.log('event', JSON.stringify(event))
    if (process.env.AWS_EXECUTION_ENV === 'AWS_Lambda_amplify-mock') {
        console.log("BUGFIX: overwrite GRAPHQLAPIKEYOUTPUT to use mock key")
        process.env.API_AMPYMAIL_GRAPHQLAPIKEYOUTPUT = 'da2-fakeApiId123456'
    }

Hope that helps.

(PS: If you're also using a mock GraphQL, beware it behaves differently than production for queries that return non-nullable results: #6450)

@m2broth
Copy link

m2broth commented Mar 2, 2021

@dmost714 Thanks. Implemented a similar temporary solution.

@itsmarkodonnell
Copy link

@dmost714 thanks, that worked for me as a fix for now

@dmost714
Copy link
Author

dmost714 commented Apr 9, 2021

@dmost714 thanks, that worked for me as a fix for now

Hearing that is a nice start to my morning. If your lambda is a mock, but your GQL is in the cloud you won't want to replace the cloud key with the mock key. I'm now checking where the GQL endpoint is hosted to determine which key to use:
(-1 !== process.env.API_AMPYMAIL_GRAPHQLAPIENDPOINTOUTPUT.indexOf('localhost')

@cjihrig
Copy link
Contributor

cjihrig commented Apr 17, 2021

I believe the API related environment variables are now being populated correctly by mock.

If you're also using a mock GraphQL, beware it behaves differently than production for queries that return non-nullable results

That behavior should be fixed now too. Please try with the latest version of the CLI.

@github-actions
Copy link

This issue has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 24, 2021
@josefaidt josefaidt added feature-request Request a new feature and removed enhancement labels Sep 3, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature-request Request a new feature mock Issues tied to the mock functionality
Projects
None yet
Development

No branches or pull requests

7 participants