Chapter 2

Chapter 2. Your first Lambda function

  • Creating your first AWS Lambda function
  • Understanding function configurations and settings
  • Testing functions from the web console
  • Using the AWS command-line interface to call functions
synchronously (returning a result) or asynchronously (for example, subscribing a function to an event)

group of functions can be used to build an event-driven application in which the logic is bound to
events coming from outside (a client application) or inside (relationships among data).

function that’s looking into the event for a name to “greet” and returns “Hello <name>!”

Log in with your AWS credentials and select Lambda from the Compute section; choose your
preferred AWS region from the menu at the top right (usually the one closest to you to reduce
network latency) and then click “Get Started Now” on the welcome page.

Function Names

lowerCamelCase convention, joining all words together, starting with a lowercase letter and using uppercase for the first letter of every word after the first on


  • Edit code inline, straight from the web browser
  • Upload a zip file from your local environment
  • Upload a zip file from Amazon S3
You can then choose a trigger for the new function  A trigger is a source of events that will execute the function, providing the event an input. 



2.2. Writing the function

Leave the option to “Edit code inline” selected and write the following code in the online editor below it, depending on the runtime you chose. 
console.log('Loading function');
exports.handler = (event, context, callback) => {
   console.log('Received event:',
       JSON.stringify(event, null, 2));
   console.log('name =', event.name);
   var name = '';
   if ('name' in event) {
       name = event['name'];
   } else {
       name = 'World';
   }
   var greetings = 'Hello ' + name + '!';
   console.log(greetings);
   callback(null, greetings);
};

JSON

{ "name": "John" }
The JSON.stringify() method converts a JavaScript object or value to a JSON string, optionally replacing values if a replacer function is specified or optionally including only the specified properties if a replacer array is specified.

initialization phase before the function
In the initialization, you should put code that can be executed only once; for example,
to open a connection to a database
runtimes implement an easy way to write centralized logs to Amazon CloudWatch Logs.
For Node.js, anything written by console.log() goes to CloudWatch
function takes as input an event and a context
if a “name” key is present in the input event, then that name is used to prepare a “greeting
resulting greeting is then logged and returned by the function.
case, callback(null, data) is used to terminate successfully and return the greeting.
you need to specify which function inside your code should be called by AWS Lambda.
You can do that via the Handler field below the code.
the default value for Node.js is index.handler
AWS Lambda needs to have the permissions to do what it’s supposed to do.
AWS Identity and Access Management (IAM) roles and policies,
function is executed it assumes the role given in the configuration
policy” describes what you can do in terms of actions, resources, and conditions.
function is interacting only with Amazon CloudWatch Logs (for logging)
use “myBasicExecutionRole”
How much memory to use. This setting also affects the quantity of CPU power and the cost of
executing thThe timeout after which the function is automatically terminated.This setting is used
to avoid mistakes that could start long-running functions, e function,

2.4. Testing the function


test it directly from the web console.
Test button in the upper left of the console.
events are expressed using a JSON syntax that’s translated to native objects or types when the
event is received by the actual runtime
{
 "name": "John"
}
Save and test” at the bottom of the window, the function is invoked with the test event you provided.

2.5. Executing the function through the Lambda API


any Lambda function can be executed via the AWS Lambda Invoke API call.
can use the AWS command-line interface.



The JSON.stringify() method converts a JavaScript object or value to a JSON string,
optionally replacing values if a replacer function is specified or optionally including only the
specified properties if a replacer array is specified.


console.log('Loading function');
exports.handler = (event, context, callback) => {
   console.log('Received event:',
       JSON.stringify(event, null, 2));
   console.log('greet =', event.greet);
   console.log('name =', event.name);
   var greet = '';
   if ('greet' in event) {
       greet = event.greet;
   } else {
       greet = 'Hello';
   };
   var name = '';
   if ('name' in event) {
       name = event.name;
   } else {
       name = 'World';
   }
   var greetings = greet + ' ' + name + '!';
   console.log(greetings);
   callback(null, greetings);
};


JSON test event
{
 "greet": "Hi",

 "name": "John"

AddThree

exports.handler = (event, context, callback) => { var total = event.value1 + event.value2 + event.value3; var result = " Total is of 3 numbers : " + total; callback(null, result); };

JSON{ "value1": 10, "value2": 20, "value3": 30 }

myDate

exports.handler = (event, context, callback) => {
    const result = Date();
    callback(null, result);

};


No comments:

Post a Comment

To All, If you are working in starter accounts please move the work into the classroom account so I can review. For the previous ...