Understanding the Different Ways to Invoke Lambda Functions
AWS Architecture BlogUnderstanding the Different Ways to Invoke Lambda Functionsby George Mao | on02 JUL 2019| inAdvanced (300),Architecture,AWS Lambda,Serverless|Permalink|ShareIn ourfirst post, we talked about general design patterns to enable massive scale with serverless applications. In this post, we’ll review the different ways you can invoke Lambda functions and what you should be aware of with each invocation model.Synchronous InvokesSynchronous invocations are the most straight forward way to invoke your Lambda functions. In this model, your functions execute immediately when you perform the Lambda Invoke API call. This can be accomplished through a variety of options, including using the CLI or any of the supported SDKs.Here is an example of a synchronous invoke using the CLI:aws lambda invoke —function-name MyLambdaFunction —invocation-type RequestResponse —payload  “[JSON string here]”The Invocation-type flag specifies a value of “RequestResponse”. This instructs AWS to execute your Lambda function and wait for the function to complete. When you perform a synchronous invoke, you are responsible for checking the response and determining if there was an error and if you should retry the invoke.Many AWS services can emit events that trigger Lambda functions. Here is a list of services that invoke Lambda functions synchronously:Elastic Load Balancing (Application Load Balancer)Amazon CognitoAmazon LexAmazon AlexaAmazon API GatewayAmazon CloudFront (Lambda@Edge)Amazon Kinesis Data FirehoseAsynchronous InvokesHere is an example of an asynchronous invoke using the CLI:aws lambda invoke —function-name MyLambdaFunction —invocation-type Event —payload  “[JSON string here]”Notice, the Invocation-type flag specifies “Event.” If your function returns an error, AWS will automatically retry the invoke twice, for a total of three invocations.Here is a list of services that invoke Lambda functions asynchronously:Amazon Simple Storage ServiceAmazon Simple Notification ServiceAmazon Simple Email ServiceAWS CloudFormationAmazon CloudWatch LogsAmazon CloudWatch EventsAWS CodeCommitAWS ConfigAsynchronous invokes place your invoke request in Lambda service queue and we process the requests as they arrive. You should useAWS X-Rayto review how long your request spent in the service queue by checking the “dwell time” segment.Poll-Based InvokesThis invocation model is designed to allow you to integrate with AWS Stream and Queue based services with no code or server management. Lambda will poll the following services on your behalf, retrieve records, and invoke your functions. The following are supported services:Amazon KinesisAmazon SQSAmazon DynamoDB StreamsAWS will manage the poller on your behalf and perform Synchronous invokes of your function with this type of integration. The retry behavior for this model is based on data expiration in the data source. For example, Kinesis Data streams store records for 24 hours by default (up to 168 hours). The specific details of each integration are linked above.ConclusionIn our next post, we’ll provide some tips and best practices for developing Lambda functions. Happy coding!TAGS:Amazon Alexa,Amazon Kinesis,amazon lambda,cloudformation,CloudWatch (Logs and Events / Rules,DynamoDB,Elastic Load Balancing,Kinesis Firehose,serverlessGeorge MaoGeorge Mao is a Specialist Solutions Architect at Amazon Web Services, focused on the Serverless platform. George is responsible for helping customers design and operate Serverless applications using services like Lambda, API Gateway, Cognito, and DynamoDB. He is a regular speaker at AWS Summits, re:Invent, and various tech events. George is a software engineer and enjoys contributing to open source projects, delivering technical presentations at technology events, and working with customers to design their applications in the Cloud. George holds a Bachelor of Computer Science and Masters of IT from Virginia Tech.
In our first post, we talked about general design patterns to enable massive scale with serverless applications. In this post, we’ll review the different ways you can invoke Lambda functions and what you should be aware of with each invocation model. Synchronous Invokes Synchronous invocations are the most straight forward way to invoke your Lambda […]
George Mao
Advanced (300) Architecture AWS Lambda Serverless