5

I have react web app that uses aws amplify to build its backend. This backend consists of a graphql api with a dynamodb table as data source (created by the model declared in my graphql schema) and a lambda function that fetches information from a 3rd party api and saves the results into the dynamodb table to make it available for the app.

Following this tutorial:

https://docs.amplify.aws/cli/function#graphql-from-lambda

I created the lambda function running the "amplify add function" command. I answered "yes" when I was asked if the lambda should have access to other amplify resources and I gave the lambda write and update access to my graphql api, so that I can perform mutations from the lambda.

When I finish the creation process, amplify informs me that I have two new environment variables available in the lambda:

API_<MY_APP>_GRAPHQLAPIENDPOINTOUTPUT
API_<MY_APP>_GRAPHQLAPIIDOUTPUT

The problem is, however, that I also need the api key in order to be able to perform the mutations (my graphql api has "api key" as primary id method).

The tutorial makes use of process.env.API_KEY. However they don't explain where this variable comes from, as in my case its value is undefined.

Do you know how to make the api key available to my lambda?

P.S.: Hardcoding is not an option, as I have several backend environments.

1 Answer 1

1

The env var is called API_<APP_NAME>_GRAPHQLAPIKEYOUTPUT. It appears only if you enable API KEY authentication for your GraphQL API.

Here is an example of what is printed into the console when you add access permission for a Lambda function to access a GraphQL API:

You can access the following resource attributes as environment variables from your Lambda function
        API_AMPLIFYTEST_GRAPHQLAPIENDPOINTOUTPUT
        API_AMPLIFYTEST_GRAPHQLAPIIDOUTPUT
        API_AMPLIFYTEST_GRAPHQLAPIKEYOUTPUT

Also, the Amplify CLI generates a comment on top of your Lambda function index file with the list of useful env vars.

I tested this with Amplify CLI version 4.36.0.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.