Amplify-cli: Add ability to load .env file and set event for `amplify function invoke`

Created on 10 Jan 2019  路  6Comments  路  Source: aws-amplify/amplify-cli

Is your feature request related to a problem? Please describe.

I want to test out a lambda function locally. I am running into three annoying things:

  1. If I need to set environment variables (like if I鈥檓 working with Stripe and need to set the secret key or just to set ENV), I have to manually pass those into the call like: ENV=dev STRIPE_SECRET_KEY=sec_foo amplify function invoke myfuncname

  2. If I want to specify an event, I have to first come up with a valid event (which I鈥檓 getting by logging the event to the console then looking at cloudwatch logs JSON.stringify(req.apiGateway.event)), and then setting amplify/backend/functions/myfuncname/src/event.json to be that content, which ends up getting committed into code too.

  3. I have to hit enter twice to accept the default script file and handler function

Describe the solution you'd like

I wish I could run the command as such:

amplify function invoke myfuncname --env ../.env --event ./events/myfuncname-get.json -y

In this case:

  • --env - uses dotenv to set process.env variables for me
  • --event - specifies the event JSON
  • -y - auto accepts the default script and handler function

Describe alternatives you've considered

I鈥檝e written my own bash script that does some of this work for me. It also sets AWS_SDK_LOAD_CONFIG=1 and AWS_PROFILE=myprofilename

Additional context

Similarly would love if amplify push accepted a -y argument (I guess I could do yes | amplify push for this one).

Also, I would absolutely love if there was an easy to use event generator to generate valid GET/POST/PUT/DELETE requests (in this case I鈥檓 working with lambdas that use API Gateway).

One more thing, why does amplify function invoke myfuncname remain open and listening on a port after it runs? Am I able to do something with it or do I have to just manually kill it each time?

feature-request functions

Most helpful comment

Any news with this matter?

All 6 comments

Am I able to do something with it or do I have to just manually kill it each time?

You can hit different routes of the function/endpoint. for example, if your app.js looks like below:

app.get("/items", function(req, res) {
  // Add your code here
  res.json({ success: "get call succeed!", url: req.url });
});

Then you can curl localhost:3000/items

Has this gotten anywhere yet?

you can achieve this goal with VS code debug configuration file.

Any news with this matter?

I've added a comment about local .env - maybe you will find it useful https://github.com/aws-amplify/amplify-cli/issues/1378#issuecomment-589179820

@gaochenyue can you elaborate?

Was this page helpful?
0 / 5 - 0 ratings