Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mock environment variables for Amplify Function

I created an AWS Lambda function (named getDictionaryTranslation) with Amplify. The handler function look like:

exports.handler = async function (event, context) { //eslint-disable-line
  const appId = process.env.APP_ID;
  const appKey = process.env.APP_KEY;
  context.done(null, `${appId}|${appKey}`);
};

Then I created a GraphQL Query from this function

type Query {
  getDictionaryTranslation(word: String!, lang: String!): String! @function(name: "getDictionaryTranslation-${env}")
}

We can set APP_ID and APP_KEY value on AWS Console by following this document https://docs.aws.amazon.com/lambda/latest/dg//env_variables.html

However, when running amplify mock in my local machine. How can I set these variables values?

like image 662
Minh Nguyen Avatar asked Jan 26 '26 19:01

Minh Nguyen


1 Answers

just create and store your .env variable on the root path of your function:

amplify/backend/function/<function name>/.env

So in your case:

amplify/backend/function/getDictionaryTranslation/.env

That will overwrite the env variables you added with the CLI and allow you to configure any runtime env variables for amplify mock, which also means in that .env file you need to have all the variables you set using the CLI for that specific cloud function.

More details on the doc: Function mock environment variables

like image 70
Bandisa Masilela Avatar answered Jan 29 '26 11:01

Bandisa Masilela