AWS Lambda functions are great for simple logic running periodically (among other things). In this article, we’ll create a simple AWS Lambda function in Python that retrieves stock prices from a REST API every minute. Let’s get straight to it!
Create a Lambda Function
First, we need to create a function. Follow the instructions illustrated below to do this.
Editing the Lambda’s Code
The function’s configuration screen can seem quite confusing at first, but all you need to do is scroll down to get to the code editor. While there are a few different ways to add code to your Lambda, using the provided editor (which is the default option for Python) is the easiest.
Replace the default code in the editor with the following, and hit the Save button at the top-right.
from urllib.request import urlopen
from contextlib import closing
import json
def lambda_handler(event, context):
with closing(urlopen("https://financialmodelingprep.com/api/v3/stock/real-time-price/GOOGL")) as responseData:
jsonData = responseData.read()
deserialisedData = json.loads(jsonData)
price = deserialisedData['price']
print(price);
return price
Here we are simply retrieving Google’s stock price using the Financial Modeling Prep Stock Realtime Price API, which is open and doesn’t require any authentication.
Next to the Save button at the top-right, there’s a Test button. Click it, and the following screen comes up.
Just enter a name (e.g. “Test”) and hit the Create button further below. We’re not using the input JSON data, so you can just ignore it.
Next, click the Test button at the top-right again, and your Lambda function will be executed:
The results are shown below the code, and these include various metadata (such as a Request ID and execution time) as well as Google’s stock price of 1082.38, which we retrieved from the REST API and logged using the print
statement in the code.
Running Periodically
We now have a working Lambda function, but so far we have to invoke it manually every time. Let’s set it up so that it runs every minute.
Scroll back to the top, and you’ll see a placeholder telling you to “Add triggers from the list on the left“. Following that advice, click on “CloudWatch Events” to the left.
This has the effect of adding “CloudWatch Events” as a trigger in the slot where the placeholder text was, but what you might not notice at first is that the lower part of the page changes from the code editor to a “Configure triggers panel“. This can be quite confusing for those new to AWS Lambda who might not intuit right away that clicking on the boxes will affect the content in some other part of the page.
By scrolling down, we can configure the new trigger.
Filling in most of the settings (e.g. choosing a name) is easy, bearing in mind that there are some restrictions (e.g. some characters, such as spaces, are restricted in the name).
The only tricky part is where we specify how frequently we want the function to be executed. For this, we can use cron
or rate
expressions (refer to AWS documentation: Schedule Expressions Using Rate or Cron). By using an expression of rate(1 minute)
, we configure the function to run every minute, which is the smallest supported interval.
Once this is all set up, click the Add button to set up the trigger. Then, don’t forget to click the Save button at the top-right of the page to apply the changes to the Lambda function.
Checking Output in CloudWatch
After waiting a few minutes for the function to run a few times, we can go into CloudWatch and check the output of each execution.
From the AWS Services, locate CloudWatch. Go into Logs from the left menu, and locate the log group for our Lambda function (in this case it’s /aws/lambda/StockChecker
).
Select the most recent log stream (the one at the top), and if you scroll to the end, you should see logs showing the function’s execution every minute, as well as whatever we’re writing to standard output (in this case, Google’s stock price).
We can see that the function is executing every minute, and we’re logging a stock price each time. The US stock market is closed right now, and that’s why the stock price is always the same (you’d expect it to change frequently when the market is active).
Conclusion
At this point, we have a simple, working AWS Lambda function (written in Python) that runs every minute and retrieves Google’s stock price. To keep things simple, we’re just writing it to standard output, which means we can see the value in CloudWatch – but we could also expand the code to build something useful from this.