Friday 16 November 2018

AWS Lambda

Getting Started

  • It is a Compute Service
  • Run your code with out Managing servers
  • Server and operating system maintenance, capacity provisioning and automatic scaling, code monitoring and logging will be handled by AWS Lambda on behalf of yourself.
  • Scales automatically, from a few requests per day to thousands per second
  • You pay only for the compute time you consume
  • You are responsible only for your code
  • All you need to do is supply your code in one of the languages that AWS Lambda supports (currently Node.js, Java, C#, Go and Python)
  •  Your code will be executed in response to the events such as
    • Changes to data in an Amazon S3 bucket or an Amazon DynamoDB table.
    • HTTP requests using Amazon API Gateway;
    • Invoke your code using API calls made using AWS SDKs
  • Your code will be available in Lambda Functions
  • You can use the AWS Lambda console, AWS CLI, and AWS SDKs to create a Lambda function.
  •  Following steps make you to understand the Lambda function which generates a Random 
number between two given numbers using the Nodes.js

Steps

          Step 1 : Log In with AWS Console

          Step 2 : Go to Compute section and click on Lambda(Run Code in Response to           
                      Events)

          Step 3 : New Web page will be opened and click on Get Started Now to create
           New Function

            Step 4 : New Web page will be opened which has Number of Blue         
                         Prints(Sample Codes) such as

    • https-request,
    • hello-world,  
    • Cloudwatch-logs-process-data etc


             Step 5 : Select a run time as Node.js 4.3

           Step 6 : Select hello-world(A Starter AWS Lambda function)

          Step 7 : New Web page will be opened, which has Configure trigger details but 
                       we are not going to do anything will Trigger for this blog so we will skip this 
                       step, Click on Next.

           Step 8 : New web page will be opened which has Configure Function details


          Step 9 : Input the Function Name as ‘random-number-generator’ and leave  the                                 Description as it is.

           Step 10 : Select Runtime as Node.js 4.3


           Step 11 : Select Code entry type as one of following option ‘Edit code 
                          inline’,‘Upload  a .zip file’ and ‘Upload a file from Amazon S3’. I am
                          selecting ‘Edit code inline’ for this Blog


Function using Node.js 4.3

          ‘use strict’
          Console.log(‘Loading function’);

          Export.handler = (event, context, callback) => {
    Let min = 0;
    Let max = 10;
    Let generatedNumber =  Math.floor(Math.random()*max) + min;

   Call back(null, generatedNumber);
          };

          Step 12 : Need to set the lambda function handler and role, we have to do   the    
          below things,


    • There will be handler text field which has value index.handler  it populates     automatically 
    • select a role from the following roles Choose an existing role, creating new role            from templates and Create a custom role. In this blog I am selecting creating new      role from templates
    • Enter Role Name as ‘basic-lambda-execute-role’
    • Enter Policy templates as ‘Simple Microservice permission’


           Step 13 : We have to do the below Advanced Settings



    • Set Memory as 128 MB which is default value but this is a very basic function so 128 MB is more than enough
    • Set time out as 3 Seconds which is also default one, if your function hasn’t finish within the time out period then Amazon kills it and shows an error message
    • By default VPC as ‘No VPC’


             Step 14 : Click on Next which goes to the New Page which has the following     
                            Review details.


    • Name of the Function
    • Description
    • Runtime
    • Handler
    • Role Name
    • Policy Templates
    • Memory
    • Timeout
    • VPC


           Step 15 : Click on Create function once review is completed.



           Step 16 : New Web page will be available with following details




       Code               : You can see your code

Configuration  :  You can see the ‘lambda function handler and role’ and 
                              Advanced Setting details

Triggers          :  You can see the Trigger details but no Trigger available                             for this Blog.

Monitoring      : Here you can see Invocations, Duration,
                            events and Throttles

          Step 17 : Click on Test button in the same page, you can see the Execution
                         resultSummary(which has Request id, Duration, Billed Duration,
                         Resource configured, Max memory used etc) and Log output  down the 
                         screen










`




4 comments: