Amplify-cli: API_KEY is not passed to lambda as a environment variable in mock mode

Created on 5 Jan 2020  Â·  9Comments  Â·  Source: aws-amplify/amplify-cli

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
I add API_KEY auth as an additional auth type to my API. When I start mock, the fake API_KEY is added to aws-exports.js, but in the lambda function process.env.API_KEY returns null
I wonder how you pass the API_KEY as environment variable in the example below
https://aws-amplify.github.io/docs/cli-toolchain/quickstart#graphql-from-lambda

Amplify CLI Version
4.7.0

To Reproduce

  1. Create an auth with API_KEY auth type
  2. Add a lambda function and print process.env.API_KEY
  3. Add the function as a resolver to an AppSync query.
  4. amplify mock
  5. Invoke the AppSync query

Expected behavior
process.env.API_KEY should contain the key value in aws-exports.js file

Screenshots
If applicable, add screenshots to help explain your problem.

Desktop (please complete the following information):

  • OS: Mac
  • Node Version. v8.11.1

Additional context
I hard code the key value in the lambda function as a workaround

enhancement mock

All 9 comments

For me, it is not only mock where this does not work, but also regular lamda functions. I tried everything I can think of, from adding it to the stacks parameters, adding it to backend-config.json -> function -> dependson (where it just gets replaced when updating the function via the CLI), ... Looks to me right now, the only way to use graphql from a lamda function is either not use API_KEY authentication, or setting it manually :(

But after hours of trying to get the key into the lamda function, it feels kinda mocking that in the documentation at https://aws-amplify.github.io/docs/cli-toolchain/quickstart#graphql-from-lambda the API_KEY just is there in the environment by just using the CLI....

API_KEY is for mock. When deploying in the cloud, use IAM, although it is not documented very well.

On Jan 10, 2020, at 07:47, Robin Wenglewski notifications@github.com wrote:


For me, it is not only mock where this does not work, but also regular lamda functions. I tried everything I can think of, from adding it to the stacks parameters, adding it to backend-config.json -> function -> dependson (where it just gets replaced when updating the function via the CLI), ... Looks to me right now, the only way to use graphql from a lamda function is either not use API_KEY authentication, or setting it manually :(

—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or unsubscribe.

@gaochenyue Are you accessing the Lambda function as a resolver for the AppSync API or are you invoking the AppSync API via a Lambda function?

@kaustavghosh06 We are doing both. Browser initiates the request from AppSync API 1 with a function resolver and the resolver function call AppSync API 2. The first request is Cognito auth and second request is API_KEY auth.

For mock you can set your local process env variables to your API Key. Are you referring to the ability of setting the API_KEY as environment variables when giving access to a lambda function for an AppSync API?

@kaustavghosh06 yes, could you please pass API KEY as an environment variable to function automatically when starting mock mode so I can have an env reference in the function, e.g. process.env.API_KEY

@gaochenyue Yes we're tracking this as a feature request.

I'm having the same issue, any updates?

@escape-velocity-labs IAM is supported with mock. API_KEY is not needed anymore unless you have other reasons to use API_KEY with mock

Was this page helpful?
0 / 5 - 0 ratings

Related issues

darrentarrant picture darrentarrant  Â·  3Comments

davo301 picture davo301  Â·  3Comments

jexh picture jexh  Â·  3Comments

ffxsam picture ffxsam  Â·  3Comments

kstro21 picture kstro21  Â·  3Comments