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:
amplify checkout
on the env of your choosingamplify function invoke...
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.
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:
amplify/backend/function/[your_function]/src/.env
EXAMPLE_VARIABLE=XYZ
function/[your_function]/src
npm install dotenv
require('dotenv').config()
into the first line of your function code at function/[your_function]/src/index.js
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
.
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: