Lambda Events with Serverless

Hey guys,

I’ve been using serverless invoke -p to pass the input inside event.json as an event to a few of my Lambdas and I was wondering, is it possible to pass the input in this file to a Lambda that is invoked by other means? For example if I use a Cron schedule to invoke the Lambda, am I able to give it the same input as in the event.json file, or is it only possible to get the information of the Cron event?

You can invoke the same lambda or other lambda functions with the aws SDK

Check out: http://stackoverflow.com/questions/31714788/can-an-aws-lambda-function-call-another#31714788

So you could schedule a lambda cron function to run every 5 minutes (lets say) and inside that function call:

var aws = require('aws-sdk');
var lambda = new aws.Lambda({
  region: 'us-west-2' //change to your region
});

lambda.invoke({
  FunctionName: 'name_of_your_lambda_function',
  Payload: JSON.stringify(event, null, 2) // pass params
}, function(error, data) {
  if (error) {
    context.done('error', error);
  }
  if(data.Payload){
   context.succeed(data.Payload)
  }
});

You will just need to make sure your cron function has the correct IAM roles that allow it to invoke the other function

Hi David,

Thanks for the reply. This is something that I considered doing, however it solves the problem by creating another Lambda which is something that I am trying to avoid. The inputs that I am trying to pass in are ones that are usually environment/test dependent and can change often. Examples are setting True or False for DryRun in certain functions, and setting which account ARN to use for others. I was trying to find something that would allow me to alter a json file and then allow me to deploy the Lambda in Serverless along with the json in order to use it as an input for any kind of invocation(manual, or otherwise) along with any other event messages that were passed. However, after looking into it some more on both the Serverless and Amazon side, I’m not sure that this is possible to do at this time as it seems that it will just receive input based on how it was invoked (e.g. SNS invoked functions receiving message based inputs, and Cron invoked functions receiving Cron related inputs).

It seems to me that if you were invoking a Lambda on a schedule and passing it the event.json data, that data is not able to change so essentially its just hardcoded data.

Why not just either hardcode the data in your script or embed the file in the Lambda function and then access it through require("./event.json") ?

Alternatively, put the JSON file somewhere where Lambda can access it ( S3 , a HTTP site etc ) and then when the Lambda is triggered by the schedule you can download the file before executing the rest of the script.

Thanks for the response dsole,

I don’t want to hardcode the data into the script for a couple of reasons. Some of the inputs that I feed in are used in multiple places throughout the script and changing a line in vim is quicker (and easier to understand for others that are reading the script) than going into an IDE and refactoring several lines, especially when you want to change things while troubleshooting. As well, using a json file with Key:Value pairs improves the code by allowing it to become more generic and reusable.

Accessing the file from S3 is something that I’ll keep in mind, but after messing around inside of the AWS Console and reading more AWS documentation, I found out that under CloudWatch->Events->Rules, you can configure the input of a rule to be a matched event, part of a matched event, or json text. This rule is created by Serverless based on what is under events for a function in the serverless.yml file, so it makes sense to me that there should be some way of specifying the input that the rule passes to the Lambda when it invokes it (likely this would be something that is done inside of the serverless.yml file).

Do you have any advice based on the above (i.e. how would I go about altering the serverless.yml file to create the invocation rule to pass in the input that is equivalent to that found in the event.json file)?

I’m no expert, but I don’t think that is supported.
For it to be deployed, the constant JSON input on a CloudWatch rule’s target would need to be set in the CloudFormation template created by Serverless. Specifically, it would need to populate the “Input” property on a Target set on a “AWS::Events::Rule” resource.
But looking at the code for the schedule event, that Input property is not set, and so that indicates that its not currently supported. Perhaps you could modify the schedule event’s source code or maybe the generated template could be extended if you create a plugin ?