Executing and Invoking Lambda Function


This chapter will explain in detail about process of executing and invoking Lambda function and the steps involved in it.

AWS Lambda Execution Model

AWS execution depends on the configuration details added for AWS Lambda Function. When the function is created, there is a memory and time allotted, which is used for the execution of AWS Lambda function.

With the help of the configuration details, AWS Lambda creates an execution context. Execution context is a temporary runtime environment which is made ready with any external dependencies such as database connection, http endpoints, third party libraries etc., if any.

When AWS Lambda function is invoked for the very first time or if the lambda function is updated, there is little latency added because of the execution context setup. However, the subsequent calls are faster in comparison to the first one. AWS Lambda tries to reuse the execution context again if the Lambda function is invoked taking lesser time.

The reuse of execution context has the following implications −

  • If there is any database connection done for the execution of Lambda, the connection is maintained for reuse. So the Lambda code has to be such that the connection has to be checked first- if exists and reused; otherwise we shall have to make fresh new connection.

  • Execution context maintains a disk space of 500MB in /tmp directory. The data required is cached in this directory. You can have additional check in the code to see if the data exists.

  • If the callbacks or some background processes if the are not complete when the Lambda function was invoked, the execution will start when the lambda function is invoked again. Incase you do not need such thing to happen make sure your processes are all ended properly, when the function execution is complete.

You should use of the execution context and the data stored in tmp directory. You will have to add necessary checks in the code to see if the required data exists before creating fresh new ones. This will save the time during execution and make it more faster.

Invoking AWS Lambda function

We can invoke AWS manually using aws cli. We have already seen how to create and deploy AWS Lambda using cli. Here, we will first create a function using aws cli and invoke the same.

Creating AWS Lambda Function using AWS CLI

You can use the following commands for creating AWS Lambda function using aws cli

Commands

create-function 
--function-name <value>
--runtime <value>
--role <value>
--handler <value>
[--code <value>] 
[--description <value>] 
[--timeout <value>] 
[--memory-size <value>] 
[--environment <value>] 
[--kms-key-arn <value>] 
[--tags <value>] 
[--zip-file <value>] 
[--cli-input-json <value>]

Command with values

aws lambda create-function 
--function-name "lambdainvoke" 
--runtime "nodejs8.10" 
--role "arn:aws:iam::625297745038:role/lambdaapipolicy" 
--handler "index.handler" 
--timeout 5 
--memory-size 256 
--zip-file "fileb://C:\nodeproject\index.zip"

The output is as shown below −

Command With Values

The function created in AWS console is as shown below −

Function Created

Code Entry Type Existing Role

Now, you can invoke the function using the command:invoke

--function-name <value>
[--invocation-type <value>]
[--log-type <value>]
[--client-context <value>]
[--payload <value>]
[--qualifier <value>]
outfile <value>

Options

--function-name − Specify the name of the function you want to invoke.

--invocation-type(string) − by default the invokation-type is requestresponse. The values available to be used with invokation-type is RequestResponse, Event and DryRun.

  • Event invocation-type is to be used for async response.

  • DryRun is to be used when you want to verify the Lambda function without need of executing it.

--log-type − It will be Tail if the invocation type is RequestResponse. It gives the last 4KB base64-encoded log data. Possible values are Tail and None.

--client-context − You can pass client specific details to the Lambda function. The clientcontext has to be in json format and base64-encoded. Maximum file size is 3583 bytes.

--payload − json format input to you lambda function.

--qualifier − You can specify Lambda function version or alias name. If you pass the function version than the api will use qualified function arn to invoke the Lambda function. If you specify alias name, the api uses alias ARN to invoke Lambda function.

outfile − This is the filename where the content will be saved.

Command with values

aws lambda invoke --function-name "lambdainvoke" --log-type 
Tail C:\nodeproject\outputfile.txt

Command Values

You can use payload option to send dummy event to the lambda function in json format as shown below.

The related AWS Lambda code is as follows −

exports.handler = async (event, callback) => {
   console.log("Hello => "+ event.name);
   console.log("Address =>"+ event.addr);
   callback(null, 'Hello '+event.name +" and address is "+ event.addr);
};

Observe that in the code, we have console event.name and event.addr. Now, let us use payload option in aws cli to send the event with name and address as follows −

aws lambda invoke --function-name "lambdainvoke" --log-type 
Tail --payload file://C:\clioutput\input.txt C:\clioutput\outputfile.txt

Thenpayload takes input as a filepath which has json input as shown −

{"name":"Roy Singh", "addr":"Mumbai"}

The corresponding output is as shown below −

Correspond Output

The output is stored in the file C:\clioutput\outputfile.txt as follows −

"Hello Roy Singh and address is Mumbai"

Sample Events

You can test AWS Lambda function by passing a sample event. This section gives some sample events for AWS Services. You can use the invoke command to test the output when triggered with any of the services. Observe the codes given for corresponding sample events below −

Amazon S3 Put Sample Event

{
  "Records": [{
      "eventVersion": "2.0",
      "eventTime": "1970-01-01T00:00:00.000Z",
      "requestParameters": {
         "SourceIPAddress": "127.0.0.1"
      },
      "s3": {
         "configurationId": "testConfigRule",
         "object": {
            "eTag": "0123456789abcdef0123456789abcdef",
            "sequencer": "0A1B2C3D4E5F678901",
            "key": "HappyFace.jpg",
            "size": 1024
         },
         "bucket": { 
            "arn": bucketarn,
            "name": "Sourcebucket",
            "ownerIdentity": {
               "principalId": "EXAMPLE"
            }
         },
         "s3SchemaVersion": "1.0"
      },
      "responseElements": {
         "x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH",
         "x-amz-request-id": "EXAMPLE123456789"
      },
      "awsRegion": "us-east-1",
      "eventName": "ObjectCreated:Put",
      "userIdentity": {
         "principalId": "EXAMPLE"
      },
      "eventSource": "aws:s3"
   }]
}

To get the details of the file from the s3 put event, you can use the following command −

event.Records[0].s3.object.key   //will display the name of the file

To get the bucket name, you can use the following command −

event.Records[0].s3.bucket.name  //will give the name of the bucket.

To see the EventName, you can use the following command −

event.Records[0].eventName    // will display the eventname

Amazon S3 Delete Sample Event

{
   "Records": [{
      "eventVersion": "2.0",
      "eventTime": "1970-01-01T00:00:00.000Z",
      "requestParameters": {
         "SourceIPAddress": "127.0.0.1"
      },
      "s3": {
         "configurationId": "testConfigRule",
         "object": {
            "sequencer": "0A1B2C3D4E5F678901",
            "key": "HappyFace.jpg"
         },
         "bucket": {
            "arn": bucketarn,
            "name": "Sourcebucket",
            "ownerIdentity": {
               "principalId": "EXAMPLE"
            }
         },
        "s3SchemaVersion": "1.0"
      },
      "responseElements": {
         "x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH",
         "x-amz-request-id": "EXAMPLE123456789"
      },
      "awsRegion": "us-east-1",
      "eventName": "ObjectRemoved:Delete",
      "userIdentity": {
         "principalId": "EXAMPLE"
      },
      "eventSource": "aws:s3"
   }]
} 

Amazon DynamoDB

Amazon DynamoDB can be an event on AWS Lambda when changes are made on DynamoDB table. We can perform operation like add entry, update and delete records from the DynamodDB table.

A sample event for DynamoDB add, insert and delete event is shown here −

{
  "Records": [{
      "eventID": "1",
      "eventVersion": "1.0",
      "dynamodb": {
         "Keys": {
            "Id": {
               "N": "101"
            }
         },
         "NewImage": {
            "Message": {
               "S": "New item!"
            },
            "Id": {
               "N": "101"
            }
         },
         "StreamViewType": "NEW_AND_OLD_IMAGES",
         "SequenceNumber": "111",
         "SizeBytes": 26
      },
      "awsRegion": "us-west-2",
      "eventName": "INSERT",
      "eventSourceARN": eventSourcearn,
      "eventSource": "aws:dynamodb"
   },
   {
      "eventID": "2",
      "eventVersion": "1.0",
      "dynamodb": {
         "OldImage": {
            "Message": {
               "S": "New item!"
            },
            "Id": {
               "N": "101"
            }
         },
        "SequenceNumber": "222",
        "Keys": {
            "Id": {
               "N": "101"
            }
         },
        "SizeBytes": 59,
        "NewImage": {
            "Message": {
               "S": "This item has changed"
            },
            "Id": {
				   "N": "101"
            }
         },
         "StreamViewType": "NEW_AND_OLD_IMAGES"
      },
      "awsRegion": "us-west-2",
      "eventName": "MODIFY",
      "eventSourceARN": Sourcearn,
      "eventSource": "aws:dynamodb"
   },
   {      
   "eventID": "3",
      "eventVersion": "1.0",
      "dynamodb": {
         "Keys": {
            "Id": {
               "N": "101"
            }
         },
         "SizeBytes": 38,
         "SequenceNumber": "333",
         "OldImage": {
            "Message": {
               "S": "This item has changed"
            },
            "Id": {
               "N": "101"
            }
         },
         "StreamViewType": "NEW_AND_OLD_IMAGES"
      },      "awsRegion": "us-west-2",
      "eventName": "REMOVE",
      "eventSourceARN": Sourcearn,
      "eventSource": "aws:dynamodb"    
   }]
}

Amazon Simple Notification Service

AWS Lambda can be helpful to process the notification created in Simple Notification Service (SNS). Whenever there is message published in SNS, the Lambda function can be triggered with a SNS event, which has details of the messages. This messages can be processed inside Lambda function and can be sent further to other services as per the requirement.

Once the message is entered, SNS will trigger the Lambda function. If any error tries to invoke the Lambda function, SNS will retry to call the lambda function upto three times.

Amazon SNS Sample Event

A sample event that has all the details available in AWS Lambda function to carry out the further process is shown below −

{
  "Records": [{
      "EventVersion": "1.0",
      "EventSubscriptionArn": eventsubscriptionarn,
      "EventSource": "aws:sns",
      "Sns": {
         "SignatureVersion": "1",
         "Timestamp": "1970-01-01T00:00:00.000Z",
         "Signature": "EXAMPLE",
         "SigningCertUrl": "EXAMPLE",
         "MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
         "Message": "Hello from SNS!",
         "MessageAttributes": {
            "Test": {
               "Type": "String",
               "Value": "TestString"
            },
            "TestBinary": {
               "Type": "Binary",
               "Value": "TestBinary"
            }
         },
         "Type": "Notification",
         "UnsubscribeUrl": "EXAMPLE",
         "TopicArn": topicarn,
         "Subject": "TestInvoke"
      }
   }]
}

Amazon Simple Mail Service

Amazon Simple Mail Service can be used to send messages and also to receive messages. The AWS Lambda function can be called on Simple Mail Service when the message is received.

Amazon SES Email Receiving Sample Event

The details of SES event when used inside AWS Lambda is shown below −

{
  "Records": [{
      "eventVersion": "1.0",
      "ses": {
         "mail": {
            "commonHeaders": {
               "from": [
                  "Jane Doe <janedoe@example.com>"
               ],
            "to": [
               "johndoe@Source.com"
            ],
            "returnPath": "janedoe@example.com",
            "messageId": "<0123456789Source.com>",
            "date": "Wed, 7 Oct 2015 12:34:56 -0700",
            "subject": "Test Subject"
         },
         "example": "janedoe@example.com",
         "timestamp": "1970-01-01T00:00:00.000Z",
         "destination": [
            "johndoe@example.com"
         ],
         "headers": [{
            "name": "Return-Path",
            "value": "<janedoe@example.com>"
         },
         {
            "name": "Received",
            "value": "from mailer.example.com (mailer.example.com [203.0.113.1]) by inbound-smtp.us-west-2.amazonaws.com with SMTP id o3vrnil0e2ic for johndoe@example.com; Wed, 07 Oct 2015 12:34:56 +0000 (UTC)"
         },
         {
            "name": "DKIM-Signature",
            "value": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=example.com; s=example; h=mime-version:from:date:message-id:subject:to:content-type; bh=jX3F0bCAI7sIbkHyy3mLYO28ieDQz2R0P8HwQkklFj4=; b=sQwJ+LMe9RjkesGu+vqU56asvMhrLRRYrWCbV"
         },
         {
            "name": "MIME-Version",
            "value": "1.0"
         },
         {
            "name": "From",
            "value": "Jane Doe <janedoe@example.com>"
         },
         {
            "name": "Date",
            "value": "Wed, 7 Oct 2015 12:34:56 -0700"
         },
         {
            "name": "Message-ID",
            "value": "<0123456789example.com>"
         },
         {
            "name": "Subject",
            "value": "Test Subject"
         },
         {
            "name": "To",
            "value": "johndoe@example.com"
         },
         {
            "name": "Content-Type",
            "value": "text/plain; charset=UTF-8"
         }],
         "headersTruncated": false,
         "messageId": "o3vrnil0e2ic28tr"
      },
      "receipt": {
         "recipients": [
            "johndoe@example.com"
         ],
         "timestamp": "1970-01-01T00:00:00.000Z",
         "spamVerdict": {
            "status": "PASS"
         },
         "dkimVerdict": {
            "status": "PASS"
         },
         "processingTimeMillis": 574,
         "action": {
            "type": "Lambda",
            "invocationType": "Event",
            "functionArn": "arn:aws:lambda:us-west-2:012345678912:function:example"
         },
         "spfVerdict": {
            "status": "PASS"
         },
         "virusVerdict": {
            "status": "PASS"
         }
      }
   },
   "eventexample": "aws:ses"
   }]
}

Amazon Cloudwatch Logs

AWS Lambda can be triggered from Amazon CloudWatch Logs using the CloudWatch Logs Subscriptions. CloudWatch Logs subscriptions has data real-time data about the logs which can be processed and analyzed inside AWS Lambda or could be used to load to other systems.

Amazon CloudWatch Logs Sample Event

{
   "awslogs": {
      "data": "H4sIAAAAAAAAAHWPwQqCQBCGX0Xm7EFtK+smZBEUgXoLCdMhFtKV3akI8d0bLYmibvPPN3wz00CJxmQnTO41whwW
      QRIctmEcB6sQbFC3CjW3XW8kxpOpP+OC22d1Wml1qZkQGtoMsScxaczKN3plG8zlaHIta5KqWsozoTYw3/djzwhpL
      wivWFGHGpAFe7DL68JlBUk+l7KSN7tCOEJ4M3/qOI49vMHj+zCKdlFqLaU2ZHV2a4Ct/an0/ivdX8oYc1UVX860fQ
      DQiMdxRQEAAA=="
   }
}

Amazon API Gateway

AWS Lambda function can be invoked on https url. IT can be done on GET, POST, PUT. When the https url is invoked, the AWS Lambda function is also triggered and the data passed to https using get/post can be made available inside AWS Lambda to be used to insert in DynamoDB or to send mail etc.

API Gateway Proxy Request Event

{
   "path": "/test/hello",
   "headers": {
      "Accept":  "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
      "Accept-Encoding": "gzip, deflate, lzma, sdch, br",
      "Accept-Language": "en-US,en;q=0.8",
      "CloudFront-Forwarded-Proto": "https",
      "CloudFront-Is-Desktop-Viewer": "true",
      "CloudFront-Is-Mobile-Viewer": "false",
      "CloudFront-Is-SmartTV-Viewer": "false",
      "CloudFront-Is-Tablet-Viewer": "false",
      "CloudFront-Viewer-Country": "US",
      "Host": "wt6mne2s9k.execute-api.us-west-2.amazonaws.com",
      "Upgrade-Insecure-Requests": "1",
      "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",
      "Via": "1.1 fb7cca60f0ecd82ce07790c9c5eef16c.cloudfront.net (CloudFront)",
      "X-Amz-Cf-Id": "nBsWBOrSHMgnaROZJK1wGCZ9PcRcSpq_oSXZNQwQ10OTZL4cimZo3g==",
      "X-Forwarded-For": "192.168.100.1, 192.168.1.1",
      "X-Forwarded-Port": "443",
      "X-Forwarded-Proto": "https"
   },
   "pathParameters": {
      "proxy": "hello"
   },
   "requestContext": {
      "accountId": "123456789012",
      "reexampleId": "us4z18",
      "stage": "test",
      "requestId": "41b45ea3-70b5-11e6-b7bd-69b5aaebc7d9",
	   "identity": {
         "cognitoIdentityPoolId": "",
         "accountId": "",
         "cognitoIdentityId": "",
         "caller": "",
         "apiKey": "",
         "exampleIp": "192.168.100.1",
         "cognitoAuthenticationType": "",
         "cognitoAuthenticationProvider": "",
         "userArn": "",
         "userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",
         "user": ""
      },
      "reexamplePath": "/{proxy+}",
      "httpMethod": "GET",
      "apiId": "wt6mne2s9k"
   },
   "reexample": "/{proxy+}",
   "httpMethod": "GET",
   "queryStringParameters": {
      "name": "me"
   },
   "stageVariables": {
      "stageVarName": "stageVarValue"
   }
}

API Gateway Proxy Response Event

{
   "statusCode": 200,
   "headers": {
      "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
      "Accept-Encoding": "gzip, deflate, lzma, sdch, br",
      "Accept-Language": "en-US,en;q=0.8",
      "CloudFront-Forwarded-Proto": "https",
      "CloudFront-Is-Desktop-Viewer": "true",
      "CloudFront-Is-Mobile-Viewer": "false",
      "CloudFront-Is-SmartTV-Viewer": "false",
      "CloudFront-Is-Tablet-Viewer": "false",
      "CloudFront-Viewer-Country": "US",
      "Host": "wt6mne2s9k.execute-api.us-west-2.amazonaws.com",
      "Upgrade-Insecure-Requests": "1",
      "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 OPR/39.0.2256.48",
      "Via": "1.1 fb7cca60f0ecd82ce07790c9c5eef16c.cloudfront.net (CloudFront)",
      "X-Amz-Cf-Id": "nBsWBOrSHMgnaROZJK1wGCZ9PcRcSpq_oSXZNQwQ10OTZL4cimZo3g==",
      "X-Forwarded-For": "192.168.100.1, 192.168.1.1",
      "X-Forwarded-Port": "443",
      "X-Forwarded-Proto": "https"
   },
   "body": "Hello World"
}
Advertisements