Obtaining Lambda Function Info
This document will go over the three lambda functions and auto provisioning used for updating info on all your AWS Lambda functions and view different lambda metrics that can be viewed in Foundry.
Running the Metrics
Setup
Install the CloudFormation template from here. To do this, go to AWS cloud formation, click the blue 'Create Stack' button, click 'Choose File' and select the file you just downloaded. For FoundryAccountId, put the ID of the Foundry account in which you want to install the Lambda Metrics(This can be found on the preferences page). For MqttTopic, input a base MQTT topic for the setup function to send data to. By convention, this parameter should be "com.companyname/lambdametrics" (i.e. "com.thinglogix/lambdametrics"). Create this CloudFormation stack, and ensure that the box on the final page is checked stating "I acknowledge that AWS CloudFormation might create IAM resources." Let this CloudFormation stack finish completing, then Lambda Metrics will be installed in the respective Foundry account.
Invoking the Hourly & Daily Metrics Lambda Functions
To invoke the hourly metrics. Log in to Foundry and navigate to the Search Manager. Perform and search that is guarantee to return some Objects (ie. typeId == Lambda). Select an object and from the two dropdowns in the light blue/green bar, select your action as Lambda Invoke and your Type as Daily for daily metrics or Hourly Invocations for hourly metrics. Once you click Send Now, whatever lambda function you selected will run.
Example Invoking Daily Metrics:
Updating for New Lambda Functions
If you add new Lambda functions to your AWS account it makes sense that you also would want them included in your Metrics reports. To update your Metrics just run the setup again with the same payload as before and you'll be able to get right back into running your metrics.
Foundry
Foundry is used to easily put together all aspects of information on your AWS lambda functions. This section will go over the object type and provide example data which a user might see on their own version of Foundry.
Object Type Template
Currently we are using an Object Type Template called Lambda. This template is used to store information on all your lambda functions. This template is also used to create the Lambdas group (whose id is used in the auto provisioning) which all Lambda objects are apart of. The Lambda object type must also have an "invocations" attribute of type integer with the "Include in Device History" characteristic. This will ensure that the invocations are written to device history so users may see previous invocation histories.
Example Object
Auto Provisioning
Auto provisioning in AWS IoT is used to update Foundry objects with the new data obtained from the two lambda functions. The topic in the FROM clause must be whatever MQTT topic you will be passing to the Lambda functions, with the suffix "lambda/#". The DeviceTypeId must be the "Lambda" device type, and the GroupId must be the "Lambdas" group that was previously created. Rule:
SELECT *, topic() as AttributeValue, "mqtt_topic" as AttributeName, "734df200-366e-11e7-8260-af38639a4779" as AccountId, "16ca4940-583b-11e7-81ae-0dd630af6a64" as DeviceTypeId, "6f40d830-5d10-11e7-b9e7-a56e9218ec9f" as GroupId FROM 'com.thinglogix/lambda/#'
Lambda Functions
Three lambda functions are used to gather information of your lambda functions. One which is run once which gathers a majority of the information on the functions. A second which will run periodically to see how many times each function was run in the past hour and a third which will gather the invocations from the past 24 hours.
create-objects-from-lambda
This function does all the Foundry and AWS setup for you so after running it once you will never have to run it again. The function creates the device types, global actions, groups, and IoT Rule used by the other Lambda functions mentioned in this doc. It also is the function which is ran to gather information that is unlikely to change to over the course of a Lambda function's life. This includes data like the MQTT Topic and Function ARN. After the function finishes running, there will exist an object for every Lambda function which will include all the data collected listed under the provided group in Foundry's asset section.
get-lambda-function-invocations
This is the function which will be run periodically and will update all of the already created objects in Foundry with the number of times each function was ran in the last hour as well as the total duration in milliseconds for it was ran for. For example if it is 4:43 PM it will get the number of invocations from 3:00 PM - 4:00 PM.
create-daily-ref-list
This is the function which will be run periodically and will update all of the already created objects in Foundry with the number of times each function was ran in the past day from Midnight - 11:59 PM. Which it will store in both a newly created Foundry Object and in the reference list in the Lambda foundry object.
Lambda Function Usage
create-objects-from-lambda
The function must be passed five key/value pairs. One with the key "mqtttopic" with value of a base MQTT topic to publish to (i.e. "com.thinglogix/") and second with the key "accountid" with the string value of the user's Foundry Account Id. The Lambda function will add the "lambda/" suffix to the MQTT topic. The third uses the key "iotrole" and takes in the IoT Role ARN which can be found in the IAM AWS section as ThingLogixFoundry-iotRuleActionRole. The next two key/value pairs are "dailyarn" which is the Lambda ARN of the create-daily-ref-list function and "hourly_arn" which is the Lambda ARN of get-lambda-function-invocations.
get-lambda-function-invocations
The function can be passed a key/value pair with the keys "mqtt_topic" with value of the base MQTT topic to publish to (default is "com.thinglogix/"). The Lambda function will add the "lambda/" suffix to the MQTT topic. Once there is a MQTT Topic the function will get the number of invocations and duration ran for each lambda function on the account from the past whole hour (i.e. If it is 4:43 PM it will get the number of invocations from 3:00 PM - 4:00 PM) and publish it to the Foundry Lambda object.
create-daily-ref-list
This is the function which will be run periodically and will update all of the already created objects in Foundry with the number of times each function was ran in the past day from Midnight - 11:59 PM. Once the function has completed running, the "Lambda History" group will contain Foundry Objects that contain references to the Lambda objects and the Lambda object will contain the newly created object in its reference list. This function must be passed key/value pairs with the keys "lambdagroup" with value of the group ID of the "Lambda" group, "accountid" with the value of the Foundry account id, "typeid" with the value of the type id of the "Daily Invocation" device type, and "invocationgroup" with the value of the group ID of the "Lambda History" group These parameters can be configured as a scheduled global action, as pictured below.