Amplify-cli: process.env.API_KEY is undefined

Created on 15 Jul 2020  路  4Comments  路  Source: aws-amplify/amplify-cli

Describe the bug

Following the https://docs.amplify.aws/cli/function#query, it used:

headers: {
  'x-api-key': process.env.API_KEY
}

To Reproduce
Steps to reproduce the behavior:

  1. Create a graphql API
  2. Add a model with @auth and give it { allow: public, provider: apiKey }
  3. Create a function and specify that you want to access API from that function.
  4. Console log process.env and you'll see that API_KEY is not there.

Expected behavior

Should API_KEY be there or not?

Code Snippet

Just follow steps above.

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

What is Configured?
If applicable, please provide what is configured for Amplify CLI:

  • Which steps did you follow via Amplify CLI when configuring your resources.
  • Which resources do you have configured?

    • If applicable, please provide your aws-exports file:

      const awsmobile = { "aws_project_region": "us-east-1", "aws_cognito_identity_pool_id": "us-east-1:xxx-xxxx-xxxx-xxxx-xxxxxxxx", "aws_cognito_region": "us-east-1", "aws_user_pools_id": "us-east-1_xxx", "aws_user_pools_web_client_id": "xxxx", "oauth": {} };

    • If applicable, please provide your manual configuration example:

      { Auth: { identityPoolId: 'XX-XXXX-X:XXXXXXXX-XXXX-1234-abcd-1234567890ab', region: 'XX-XXXX-X', identityPoolRegion: 'XX-XXXX-X', userPoolId: 'XX-XXXX-X_abcd1234', userPoolWebClientId: 'a1b2c3d4e5f6g7h8i9j0k1l2m3', mandatorySignIn: false, authenticationFlowType: 'USER_PASSWORD_AUTH', oauth: { domain: 'your_cognito_domain', scope: ['phone', 'email', 'profile', 'openid', 'aws.cognito.signin.user.admin'], redirectSignIn: 'http://localhost:3000/', redirectSignOut: 'http://localhost:3000/', responseType: 'code' // or 'token', note that REFRESH token will only be generated when the responseType is code } } }

  • If applicable, provide more configuration data, for example for Amazon Cognito, run aws cognito-idp describe-user-pool --user-pool-id us-west-2_xxxxxx (Be sure to remove any sensitive data)


Environment

npx envinfo --system --binaries --browsers --npmPackages --npmGlobalPackages

Smartphone (please complete the following information):

  • Device: [e.g. iPhone6]
  • OS: [e.g. iOS8.1]
  • Browser [e.g. stock browser, safari]
  • Version [e.g. 22]

Additional context

I'm trying to do this:

// somewhere above
const { GraphQLClient } = require('graphql-request');

// inside handler
const client = new GraphQLClient(
  endpoint,
  {
    headers: {
      'x-api-key': process.env.API_KEY
    }
  }
);

_You can turn on the debug mode to provide more info for us by setting window.LOG_LEVEL = 'DEBUG'; in your app._

bug documentation functions pending-release

Most helpful comment

@aprilmintacpineda The fix for this has been released in CLI v 4.26.0. The environment variable will be populated in API__GRAPHQLAPIKEYOUTPUT. I have a docs PR above to update the docs to the new env variable as well.

All 4 comments

I saw this but unsure if related, since it talked about mock. https://github.com/aws-amplify/amplify-cli/issues/3124

Transferring to CLI

I reproduced the bug.
We will work on a fix soon.
In the meanwhile, you can manually add the environment variable to the Lambda Function to unblock your work.

  • Open the AWS Lambda Console by execute amplify function console.
  • Scroll down to the Environment variables section and click on Edit
  • Click Add environment variable

You can find the ApiKey from the amplify/backend/amplify-meta.json file, it's in the output of the api resource: api.<api-name>/output/GraphQLAPIKeyOutput

@aprilmintacpineda The fix for this has been released in CLI v 4.26.0. The environment variable will be populated in API__GRAPHQLAPIKEYOUTPUT. I have a docs PR above to update the docs to the new env variable as well.

Was this page helpful?
0 / 5 - 0 ratings