Developing on AWS Lambda (Part 2): Understanding AWS event sources
In Part 1 we talked about the basics of programming with AWS Lambda. Now, lets review the different ways Lambda functions are invoked. There are two broad ways you can invoke Lambda functions. Let’s take a look at them:
- Programmatically via the AWS SDK, CLI, or API
- Automatically via a built-in AWS event source. There are well over 100 services within the AWS Cloud and most of them can invoke Lambda functions
Programmatically
Lambda functions are invoked through the API Action: Invoke. There are a few required parameters such as FunctionName
and InvocationType
. You can call this API directly by constructing the correct HTTP POST payload (and provide a valid AWS Sigv4 signature), however, it’s much easier to use the CLI or SDK. Both of these issue calls against the Invoke action. With the CLI, you can do the following:
aws lambda invoke --function-name myFunction response.json
Notice, we didn’t specify the InvocationType — this is because the CLI defaults to RequestResponse
, which is a Synchronous invocation. You can change an invoke to Asynchronous by specifying the value Event
.
aws lambda invoke --function-name myFunction --invocation-type Event confirmation.json