Sometimes it’s necessary to run a maintenance API script in your LogicMonitor portal. For example, I move decommissioned devices into a specific folder because I no longer want to receive any alerts on these devices. An API script helps automate the process by running once a day to disable alerts on any new devices added to this folder.
One solution is running the API script in an AWS Lambda Function.
According to Amazon:
AWS Lambda lets you run code without provisioning or managing servers. You pay only for the compute time you consume – there is no charge when your code is not running. With Lambda, you can run code for virtually any type of application or backend service – all with zero administration. Just upload your code and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger from other AWS services or call it directly from any web or mobile app.
It’s perfect for running Python or Java scripts at a rate of once per day without having to configure a cron job or Windows Scheduled Task on a server.
There are a few little changes you’ll need to make in your Python script to get it to work in Lambda.
Steps to set up a Lambda Function:
- Write the Python code as normal
- In addition to the normal code, you have to define a handler. For a basic API Python script, the below will do. Just indent the rest of the code
def lambda_handler(request_obj, context=None):
A close statement is not required.
The default Python environment in Lambda is stripped down and doesn’t include very many modules. LogicMonitor API scripts require the “requests” module, which means we will have to build a virtual Python environment, install requests, zip it up, and upload to AWS.
There are a few ways of doing this, the easiest way to is by using PIP within an Ubuntu environment.
$ sudo apt-get install python-pip python-dev build-essential $ sudo pip install --upgrade pip $ sudo pip install --upgrade virtualenv
Next, make a directory to store our script and supporting python modules. In the current example, I’ve named it disablealerts because the script disables alerts on all new devices moved to a specific LogicMonitor group.
$ mkdir disablealerts
Then we need to install the requests package to the target directory as it is required for the script to run. This command will install requests to our disablealerts directory.
$ pip install requests -t disablealerts
Next, save the Python API script in the root of the disablealerts directory and compress
cd disablealerts zip -r disablealerts.zip *
Now that we have our Python environment created and zipped we can create the Lambda Function in AWS.
Log into the AWS console and open the Lambda Section
- Click “Create Function”.
- Select “Author from scratch” and specify an appropriate name.
- Set the Runtime to Python 3.7.
- For Role, select “Choose an existing role”
- Select “lambda_basic_execution” for the existing role.
- Press the “Create Function” button
- Change the “Code entry type” to Upload a .zip file” and browse to .zip file create in the previous step and upload.
- The “Handler” needs to be the name of the python file to be executed followed by .lambda_handler, in example “index.lambda_handler”.
- The environment variables can be used for variables (optional). In the example, we could remove the Access ID and Access Key from the script itself and define them in the Environment section. The Python code to retrieve these values would be:
- AccessId = os.environ[‘AccessId’]
- AccessKey = os.environ[‘AccessKey’]
- Click the test button to run the script and ensure it works correctly in the Lambda environment.
To set the time interval click “CloudWatch Events” from the “Add triggers” section. This will open the “Configure “triggers” window and allow you to Select an appropriate rule or create a new custom rule. In the example for disabling alerts, the default rule “once_a_day_10_min_before_midnight” is perfect.
Jeff Woeber is a Solutions Engineer on the Professional Service team at LogicMonitor where he has consulted and deployed some of LogicMonitor’s largest environments. Jeff loves all things automation and has a real passion for solving the impossible problems.
Subscribe to our LogicBlog to stay updated on the latest developments from LogicMonitor and get notified about blog posts from our world-class team of IT experts and engineers, as well as our leadership team with in-depth knowledge and decades of collective experience in delivering a product IT professionals love.