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

Apica's Scripted Checks

Amazon Web Services Lambda Runtimes

  • Java

  • Python 3+

  • JavaScript

  • Node.js

  • Python

  • Ruby

  • Java

  • Go

  • .NET Core

  • C#

  • PowerShell

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

  1. Establish an AWS Lambda Account.

  2. Prepare by importing the Libraries your script will need.

  3. Create the AWS Lambda Function, which the ASM Check will call for execution.

    1. Define the Main Functions of the script.

    2. Script the check. (Note: You could use a Scripted Check here.)

    3. Create a New Lambda Function.

    4. Import Your Script into that Lambda Function.

  4. Create the Lambda Event that ASM will display.

  5. Create the AWS Lambda Check on any ASM-supported AWS Regions on the Apica Monitoring Platform.

  6. 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

Step

Screenshot

Import Libraries

Import random because we need a random number.

Import time because we're going to need to know the start and end times of our check execution for our check.

 

import random import time

Define the Main Function

We must keep track of the name of the main function. It needs to receive two things:

  1. An event

  2. A context.

These objects are passed from AWS Lambda when we configure it later.

 

def main(event, context):

 

Scripting the Check

This script will generate the dictionary, a list of values we will measure for further analysis.

  • Generate a random value (rand_value) between 0 and 100.

  • Create json_value that will capture all the defined return values that we want: Return Code, time

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).

rand_value = random.randint(0, 100)

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 check

The 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

Step

Screenshot

Get into the AWS Lambda Service

Log into your Amazon account enter Lambda in the Search field. If you have visited Lambda earlier, there should be a shortcut.

 

 

Create New Lambda Function

Click Create Function.

Configure the Function

  • Name it "DemoFunction"

  • Select the Runtime, Python 3.9

 

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 Function

Click Create Function.

 

 

Here's our DemoFunction with an AWS-created default/placeholder function called lambda handler.

Import Your Script

Paste the Script Code into the DefaultFunction section

 

Create Test Event

In the main definition, you have event and context. We have not defined a test event yet, so if you click Test, AWS will throw an error prompting you to define the event, opening an event template.

  • Enter your test event name

  • Click Create

Deploy the AWS Changes, Test

  • Deploy your changes

  • Our pasted code has changed the name of the Runtime Handler from lambda_handler to main so that a test will return an error. This error is not a problem because we can edit the Handler to match the code.

  • Note the test Status shows Failed.

 

Edit Runtime Handler

Find 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

 

Test

Click 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

Step

Screenshot

Add an Event Field

In AWS Lambda, we're going to add a field called event. And it will be the event that's passed in; i.e., the event is data from the check itself. So when we add it in this section, we can show that happening.

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

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 Tags

Provide 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 location

AWS 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 eu-central-1.console.aws.ams:on.com/lambda/hometregion-=eu-central-14

So, select eu-central-1 for this example or whatever matches your AWS Region in the dropdown choices.

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 location

AWS 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 Groups

Set 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 check

After Acknowledging the confirmation page, press Create.

For Testing:

  • Disable failover

  • Set to Maximum Attempts to One

  • Click Save

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

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 Lambda: true event as well as the hello: "world" object that we encoded in Base64 and put inside our check configuration.

 

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