aws lambda code example

Example 1: lambda python

add = lambda a, b : a + b
add(3,6) ## 9

Example 2: lambda expressions

A lambda expression is a short block 
of code which takes in parameters
and returns a value. Lambda expressions
are similar to methods, but they do
not need a name and they can be
implemented right in the body of a method.

parameter -> expression

To use more than one parameter, wrap them in parentheses:

(parameter1, parameter2) -> expression

Example
Use a lamba expression in the ArrayList's 
forEach() method to print every item in the list:

import java.util.ArrayList;

public class Main {
  public static void main(String[] args) {
    ArrayList<Integer> numbers = new ArrayList<Integer>();
    numbers.add(5);
    numbers.add(9);
    numbers.add(8);
    numbers.add(1);
    numbers.forEach( (n) -> { System.out.println(n); } );
  }
}

Example 3: whats a lambda

x = lambda a, b, c, d, e, f: a + b + c + d + e + f
print(x(31231, 312, 312, 31, 12, 31))

Example 4: aws cli lambda

aws lambda create-function \
    --function-name my-function \
    --runtime nodejs10.x \
    --zip-file fileb://my-function.zip \
    --handler my-function.handler \
    --role arn:aws:iam::123456789012:role/service-role/MyTestFunction-role-tges6bf4

Example 5: aws lambda tutorial

callback();                // It will return success, but no indication to the caller
callback(null);            // It will return success, but no indication to the caller
callback(null, "success"); // It will return the success indication to the caller
callback(error);           //  It will return the error indication to the caller

Example 6: With which of the following can you run code without provisioning or managing servers and pay only for the compute time consumed (there is no charge when the code is not running)?

AWS Lambda lets you run code without provisioning or managing servers

Tags: