AWS Lambda Check
1. Intro to Lambda Computing
This page is a step-by-step guide to using the AWS Lambda check type.
From What is AWS Lambda?
Lambda is a compute service that lets you run code without provisioning or managing servers. Lambda runs your code on a high-availability compute infrastructure and performs all of the administration of the compute resources, including server and operating system maintenance, capacity provisioning and automatic scaling, code monitoring, and logging. You can run code for virtually any application or back-end service with Lambda. All you need to do is supply your code in one of the languages that Lambda supports.
Use Case for an AWS Lambda check.
Powerful: AWS Lambda has many runtimes available.
AWS Lambda functionality adds flexibility to the Apica Scripted Checks. The Scripted Check Options have a pre-selected set of Runtime languages, listed below. Select the Run AWS Lambda icon for additional languages beyond these to develop checks from language resources you already may have.
Apica's Scripted Checks | Amazon Web Services Lambda Runtimes |
---|---|
|
|
With AWS Lambda, you could use any one of these services to create your new ASM check.
Additionally, you can use Lambda container images to provide your Elastic Container, the runner for this check.
Other Reasons to Use the AWS Lambda check
Example: You have a very secure Virtual Private Cloud where no traffic is allowed inside, and perhaps you don't want to use a private Apicanet agent. You can spawn your Lambda execution inside your secure VPC and take full advantage of the AWS permissions model. You could use it to give your Lambda access to additional services such as databases or an AWS secret store.
Â
In our next section, we'll step through writing a very simple Python Lambda. And we will then add it into our ASM account and then run it and see the results.
Lambda Check Overview
Establish an AWS Lambda Account.
Prepare by importing the Libraries your script will need.
Create the AWS Lambda Function, which the ASM Check will call for execution.
Define the Main Functions of the script.
Script the check. (Note: You could use a Scripted Check here.)
Create a New Lambda Function.
Import Your Script into that Lambda Function.
Create the Lambda Event that ASM will display.
Create the AWS Lambda Check on any ASM-supported AWS Regions on the Apica Monitoring Platform.
Collect, compare and analyze the ASM Check Results OR send the results to integrated systems that use ASM as a data source.
2. Coding Lambda
For this AWS Lambda example, we will use Python to create the code for our cloud function. This example will be a simple Lambda; we will not be importing any libraries other than the default Python libraries.
Our goal is to create a check that returns a random value.
It appears simplistic, but this is to provide an example of creating a Lambda.
Step | Screenshot |
---|---|
Import Libraries
 | import random
import time |
Define the Main FunctionWe must keep track of the name of the main function. It needs to receive two things:
These objects are passed from AWS Lambda when we configure it later. Â | def main(event, context): Â |
Scripting the CheckThis script will generate the dictionary, a list of values we will measure for further analysis.
The expandable JSON result format will allow us to present a message with the generated random value, a unit of time measurement (milliseconds), and a Lambda function (set to True). |
json_value = {
"returncode": 0,
"start_time": time.time (),
"end_time": time.time(),
"message": f'Random value: {rand_value}.',
"unit": "ms",
"value": rand_value,
“lambda": True
} Â |
The finished script | |
Running the checkThe returned dictionary script finished here with an exit code of 0 but nothing else. But when we upload this to Lambda, we'll show you how to test it and make sure that everything is okay. | Â Â |
And the next step is uploading our script into AWS.
3. AWS Setup
Here, we will create the function, which will be called by our check inside the Amazon AWS UI.
Step | Screenshot |
---|---|
Get into the AWS Lambda ServiceLog into your Amazon account enter Lambda in the Search field. If you have visited Lambda earlier, there should be a shortcut. Â | Â |
Create New Lambda FunctionClick Create Function. | |
Configure the Function
 | |
Permissions(Optional) Set your Lambda function to run with specific permissions for a specific existing (or new) role that has the rights that it needs to run correctly. | For example, if the function needs to access a relational database or other Amazon services, this is where you would set that. |
Create FunctionClick Create Function. Â Â Here's our DemoFunction with an AWS-created default/placeholder function called | |
Import Your ScriptPaste the Script Code into the DefaultFunction section  | |
Create Test EventIn the main definition, you have
| |
Deploy the AWS Changes, Test
| Â |
Edit Runtime HandlerFind Runtime settings beneath the test results and note the Handler shows as named lambda_handler Click Edit to change this to our name of main Click Save | Â |
TestClick Test to repeat the test, now with the handler name updated to main. We can see that the test Status shows Succeeded What we see is going to be our check response. |
So the above is how you create the Lambda function in Amazon, and it's ready for use. But first, let's add a JSON Event to our check before we set up the check in ASM.
4. Adding a JSON Event
To demonstrate some of the power of Lambda, we'll be editing our code directly in AWS Lambda.
We will be adding just one additional field to our results because it's essential to show the power of our expandable JSON results.
Step | Screenshot |
---|---|
Add an Event FieldIn AWS Lambda, we're going to add a field called Click Deploy Click Test to do a quick test, and we'll see that the event has our test event that we created earlier. We'll see how this works when we run the ASM function. Note the test Status now shows Succeeded. | Â |
Next, we'll be creating our check in the ASM UI. And we'll be running this and retrieving a result from it.
5. ASM Check Creation
We're going to create our check in ASM.
Step | Screenshot |
---|---|
Navigate to ASM, New Check+. Under the Scripted Check banner, click the Run AWS Lambda icon. The check wizard will open. So let's create a new check. | |
Step 1: Name, Description, and TagsProvide a name for your check. In this example, we will call it New Lambda Check. Add any Description and Tags to help organize this check. Click Next | |
Step 2a: Command and locationAWS Region: This is the AWS region that your Lambda resides in. It may not default to your location, which you'll find that in the upper right-hand corner of your Amazon screen, or your Amazon UI. In this example, it says Frankfurt, but up in the browser URL, you will see So, select Function Name: Our example function name is simply DemoFunction, which we will type in, and find in our AWS UI, indicated to the right. | Â |
Step 2b: Command and locationAWS access key & AWS secret key. It would be best if you had this from AWS. You may need to ask your Operations team to get you an AWS Access Key and AWS Secret Key to execute this function. Enter these here. Base64 Encoded JSON Payload. This JSON payload is the event that is passed here. For this field, create a JSON object; Base64 encode it and paste it in here. You can use a Base64 encoding website http://base64encode.org . In this example, we'll create a very simple JSON by entering 'hello world,' and then we will encode it; this is our encoded JSON, and then we will paste that in the field. Â Â Â Â Â Â Â Â Â Â Â Â Location. Select for this example Montreal and then press Next.
| Â Â |
Step 3: Interval, Thresholds, & Monitor GroupsSet this check to run manually, and check the Monitor Group(s) to organize the check under. In this screenshot, It was put under the Driver Monitor Group. Click Next. Â | Â |
Create checkAfter Acknowledging the confirmation page, press Create. For Testing:
We've created our ASM check. | Â |
And in our next section, we will run the check and examine our results.
6. Run Result
We've created and configured our AWS Lambda check. We are ready to run it in ASM and view the results.
Step | Screenshot |
---|---|
Check details page and manually run the check. We see the result value/response time of 47 milliseconds. Â Â Â Â If you click into this data point, in the Result section, you can see the | Â |
You can provide almost any kind of data in this check type. As long as you have an Amazon account, the possibilities with this check are endless.
As mentioned before, you can use many different types of runtimes. If you're a Ruby person and cannot have an Apica Private Agent inside your firewall, you can use Lambda.
Using AWS Identity and Access Management, you can set very fine-grained permissions.
Can't find what you're looking for? Send an E-mail to support@apica.io