2

I have started a aws project with help of serverless framework, but i have one question regarding run lambda function.

How can I run lambda function with input parameters? I can do it via amazon console, lambda test configuration->test event. but I cannot find a correspondning function in serverless, does anyone know?

Thanks

1 Answer 1

3

For the lambda part

You can use event.json file:

{
  "principalId": "1234",
  "inputVar": "foo"
}

and then run sls function run.

According to docs, if don't specify any stage, function will run locally, if you do specify a stage, function will run deployed code in corresponding stage. BUT the docs seems outdated, you also need to pass -d flag like:

sls function run myFunction -s dev -d

This command will invoke your deployed lambda function, with parameters from your local event.json file

Here is the source code for function run options.

For APIG integration

There are some samples in documentation.

If you don't want to use templates, you can just insert related code in your s-function.json, inside the endpoint description.

  "endpoints": [
  ...

    "requestTemplates": {
      "application/json": {
        "principalId": "$context.authorizer.principalId",
        "apiKey": "$context.identity.apiKey",
        "inputVar": "$input.json('inputVar')"
      }
    }
  ...
  ]

Syntax is as described in API Gateway Accessing the $input Variable doc.

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

3 Comments

Thanks Alper, it works for me now. while you metioned -d option, i wonder is there anyway to just upload lambda function and endpoints(api gateway) to amazon without deploy (i mean without create a new version everytime), sometimes, i just want to test if it works on amazon.
afaik no. auto versioning is always triggered. but you can use prune plugin to delete old versions.
You can deploy one off function $ serverless deploy function -f functionName. It is much faster than deploying the entire project $ serverless deploy. It skips the CloudFront update. Here is some sample code - http://serverless-stack.com/chapters/deploy-the-apis.html

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.