Amplify-cli: ENV parameter not passed to Lambda when locally invoked

Created on 1 May 2019  路  10Comments  路  Source: aws-amplify/amplify-cli

Describe the bug
process.env.ENV is not defined when invoking lambda locally via amplify function invoke ..., but it is defined when running the lambda within AWS.

To Reproduce
Steps to reproduce the behavior:

  1. Do amplify checkout on the env of your choosing
  2. Run a lambda locally via amplify function invoke...
  3. process.env.ENV is not defined within the lamdba

Expected behavior
Since amplify supports multienv and is aware of current env being used, the amplify function invoke... command should set the ENV var when invoking the lambda.

The ENV var is correctly set in deployed lambdas since it is set in the cloudformation file.

Additional context
For now I am running ENV=``./scripts/currentAmplifyEnv.sh`` amplify function invoke mylambdafn where currentAmplifyEnv.sh is a simple script I have that gets the current env from amplify.

enhancement functions pending-review

Most helpful comment

I also need the other env variables - this comment that you add to the lambda files is incorrect when running lambda locally:

You can access the following resource attributes as environment variables from your Lambda function
var environment = process.env.ENV
var region = process.env.REGION
var apiDryEyeAmplifyCLIGraphQLAPIIdOutput = process.env.API_DRYEYEAMPLIFYCLI_GRAPHQLAPIIDOUTPUT
var apiDryEyeAmplifyCLIGraphQLAPIEndpointOutput = process.env.API_DRYEYEAMPLIFYCLI_GRAPHQLAPIENDPOINTOUTPUT

All 10 comments

I also need the other env variables - this comment that you add to the lambda files is incorrect when running lambda locally:

You can access the following resource attributes as environment variables from your Lambda function
var environment = process.env.ENV
var region = process.env.REGION
var apiDryEyeAmplifyCLIGraphQLAPIIdOutput = process.env.API_DRYEYEAMPLIFYCLI_GRAPHQLAPIIDOUTPUT
var apiDryEyeAmplifyCLIGraphQLAPIEndpointOutput = process.env.API_DRYEYEAMPLIFYCLI_GRAPHQLAPIENDPOINTOUTPUT

any update here?

Has a fix for this been found yet?

This would be great to access the process.env variables locally otherwise you still have to push them to AWS before you can test.

I agree, the process.env variables (especially since they are depended on by the snippet) should be added pre-invoking the function. This shouldn't be too difficult to do since most of the meta data you need is in the /backend/amplify-meta.json file anyways.

@undefobj would appending to the process.env object be a suitable implementation or is there another way you'd like to handle this functionality?

+1 for enabling access to process.env variables when using amplify function invoke to test locally

I'd like to see this as well, or at least an explanation on how to do it.

Today I had the same problem. How can I test my function without mocking temporary variables...
Here is what I did:

  1. Create new .env file into amplify/backend/function/[your_function]/src/.env
EXAMPLE_VARIABLE=XYZ
  1. On your terminal navigate to function/[your_function]/src
  2. Install npm package - npm install dotenv
  3. Add require('dotenv').config() into the first line of your function code at function/[your_function]/src/index.js
  4. You can test it quickly by adding console.log():
exports.handler = async (event) => {
    console.log('env: ', process.env)

...
}

That's it, it should work now!

Not sure what are the implications of this approach because server less is still a new thing for me

@korzewski ^^ worked like a charm!

The problem with using dotenv is that the .env file is uploaded on amplify push.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gabriel-wilkes picture gabriel-wilkes  路  3Comments

mwarger picture mwarger  路  3Comments

davo301 picture davo301  路  3Comments

ffxsam picture ffxsam  路  3Comments

jexh picture jexh  路  3Comments