Bringing AWS Lambdas to the Edge through Greengrass —
By: Sahm Samarghandi
What are Lambdas?
“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 sale 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.” (Amazon Web Services, 2019)
For me, Lambdas were my introduction to the serverless paradigm and were an absolute game changer for me! Not only do AWS Lambdas allow you to forget about any server infrastructure on the backend, but it also saves you money because these functions are only ran when they are called by a “trigger”, which can be just about any other AWS service such as: when someone access your API, the conditions for a rule are met or other events, and are completely free of charge when they are not being used. They allow for a programmer to create extremely efficient functions in the cloud and pay next to nothing for world class, top speed functionality for their web applications, mobile apps, websites and RESTful API’s.
Let’s build an API with AWS Lambda to get a better idea
If you haven’t already, sign up for an AWS Free Tier account which includes 1 million calls to Lambdas per month. Amazon provides the free tier with very generous allocations for users to build on and experiment with because they know in the long run, it will breed developers who are used to build their cloud solutions on their platform. Clever, clever! Be sure to set a billing notification because there are no warnings or barriers that keep you from choosing a service that isn’t included in Free Tier and the billing alarm will let you know when you get charged so you can prevent runaway spending.
Pick your favorite
AWS Lambdas will run your python code as happily as it will run your node application.
From their site:
AWS Lambda natively supports Java, Go, PowerShell, Node.js, C#, Python, and Ruby code, and provides a Runtime API which allows you to use any additional programming languages to author your functions. (Amazon Web Services, 2019)
Did you read that last part? “Any additional programming languages” meaning you can use virtually every language that is available to make your solutions with! We won’t dive too deep into that feature but it’s certainly a good thing to know.
New Function
We are going to use an already written example by choosing Blueprints and then typing “hello” in the search.
I’ll go with Python for this example but feel free to explore the different samples available if you are following along with this post.
Once you have made your selection click Configure.
In the belly of the beast
Alright this is exciting! We now have a Lambda created with the basic hello world template provided as a sample and are now going to test it so we can see how it works and then hook it up to API Gateway to create an API you can share with the world! The best part is the API we are building will allow for a million free calls per month!
You should now see the Lambda function console for our newly created Lambda.
On the left side you’ll see triggers. At the top of the triggers list, we see API Gateway followed by a list of other AWS services that can trigger your Lambda and provide it with information to act upon. For this reason, Lambda’s are said to be event driven functions although when we deploy a Lambda to our Greengrass core, we will tell it to be “long living” which means the Lambda function will run continuously.
Add API Gateway as a trigger by clicking on it and scroll down until you see the configuration options for API Gateway. Choose Create a new API and choose the Open security option. Click Add and then “save” in the top right corner and you’ll be able to see the address of your newly created API.
Finishing up our Lambda
As it stands right now, our Lambda is expecting input from a trigger which is stored in the “event” variable at the top. Let’s take a peek at the code and change it up a bit so we can use our Lambda with a simple Get request, meaning we can just click the link and see the result without any input.
Click on the name of your Lambda at the top of the page to bring up the inline code editor, then scroll down to the code so we can dissect the components and change it for our purpose.
The file that is loaded by default is determined by the “Handler”. The handler will call a specific function in a specific file as the entry point. All of your functionality should stem from this entry point. The format is [file_name.function_name]; our entry point is the function definition of “lambda_handler” in the “lambda_function.py” file.
In the editor, you can see that there are several print statements pulling information from “event” and a final return statement at the end. We will get rid of everything except the return statement and use a standard library called “time” in python in order to return today’s date back to the API.
Scroll back to the top, click on API Gateway then scroll down and click on your link in the configuration settings. If everything went well, you should now see a JSON object returned with today’s date!
One more for the road
Note: Greengrass is available only in certain regions so before creating your Greengrass function, check in the Greengrass console if it is available in the selected region.
Before we head into Greengrass, we need a Lambda that will do something useful on the edge. Delete the current function and related API once you are done with it and Create a new function from Blueprint. For those of you with a keen eye, you may have seen the Greengrass hello world sample in the screenshot the first time we made a function.
Past the setup code, you can see that the function handler has been commented by an AWS engineer to reflect that it does not get used in our case.
Included with the main function is the Greengrass SDK which provides the functionality to send MQTT messages to the cloud!
In our case, once the Lambda has been deployed, it will run in a loop and send a hello world response back to AWS IoT Cloud. From there the information can be pulled into a dashboard, used to trigger other events and much more. Once you become more familiar with AWS services, your options become broader when you are designing cloud solutions.
Click publish function, fill in any description you’d like and let’s move onto Greengrass.
Where the Grass is Greener
Now that we’ve created a function, we are ready to configure our group! Before we get into the how, let’s investigate the what. What the heck is Greengrass?
“AWS IoT Greengrass seamlessly extends AWS to edge devices so they can act locally on the data they generate, while still using the cloud for management, analytics, and durable storage. With AWS IoT Greengrass, connected devices can run AWS Lambda functions, execute predictions based on machine learning models, keep device data in sync, and communicate with other devices securely — even when not connected to the Internet.” (Amazon Web Services, 2019)
What does this mean? You can now bring familiar services provided by AWS to the edge on computing devices such as a Raspberry Pi or a plain old Linux box. You design and test the functionality in the cloud, and you can seamlessly integrate the functionality into your devices by remotely managing your group and deploying it to your core.
Each Greengrass group has a core acting as the center of operations, brokering messages between the devices in the group, running Lambda functions and securely connecting everything back to the cloud with world class encryption handled automatically by Greengrass. This means you can build large scale solutions that will remain secure without the headache of security configurations. Additionally, you can manage your solutions all from the AWS console from anywhere around the world making large scale more attainable than ever.
Greengrass Groups
Now that we have a bit of information on what Greengrass is, let’s get into how you can set up a group and configure the functionality.
Start by going into the Greengrass and clicking Create Group
It should automatically prompt you with a name for your core, click next and click on Create.
It will prompt you to download the appropriate certifications for your core as well as the core software to install on your favorite platform.
Instructions to setup your hardware for Greengrass can be found here and will not be included in this guide for the sake of brevity.
Almost done!
We are in the final stretch of this post and now we need to attach our Lambda to our group, setup the MQTT subscription and deploy! Once deployed we will be able to see our hello world message in IoT cloud!
Once you are finished downloading the certs and core software, you should now see the group management console.
Click on Lambdas and add Lambda on that tab. Choose existing Lambda and look for the Greengrass Lambda we created earlier.
You’ll choose your compatible Lambda that is in the same region as your Greengrass group and the published version you wish to attach.
We want to make this Lambda long running so click on edit configuration and check the radio button for long lived.
Click update and lets setup our subscription. Add a new subscription to the group and enter the source, target and topic as shown in the screenshot.
Our Greengrass group is now fully setup and we are ready to deploy!
Make sure you have your group core up and running with the core software started up successfully. Click on Actions > Deploy and let’s head to the test console to see our messages!
Head back to the IoT console and click on the test tab.
Subscribe to our topic “hello/world” and you should see the messages pouring in!
That’s all folks!
We covered a ton of material so let’s go back and review what we just built. First, we looked at AWS Lambdas, the serverless, event-driven service that allows us to create many wonderful programs without having to worry about setting up servers in the correct configuration. Lambdas are efficient, performant and abstract away the server layer in web programming allowing developers to focus on creation not configuration. We attached our function to an API allowing us to extend the functionality to a RESTful interface and share our function with the world! We then setup a Greengrass Lambda before creating a Greengrass group and attaching the Lambda to it. We setup a subscription allowing MQTT communication originating from said Lambda to AWS IoT cloud. Finally, we watched our deployed Lambda in action by seeing the messages through the IoT Test console.
Hope you had a lot of fun and learned a thing or two! You can read more about Lambdas, API Gateway and Greengrass on the AWS website and I highly encourage you to make a Free Tier account and play around with the services for yourself. You may discover a new and hopefully better approach to developing your web-based solutions!
References
Amazon Web Services. (2019, 01 29). AWS Free Tier. Retrieved from AWS Free Tier: https://aws.amazon.com/free/
Amazon Web Services. (2019, 01 29). AWS IoT Greengrass. Retrieved from AWS IoT Greengrass — Amazon Web Services: https://aws.amazon.com/greengrass/
Amazon Web Services. (2019, 1 29). AWS Lambda. Retrieved from AWS Lambda — Serverless Computer — Amazon Web Service: https://aws.amazon.com/lambda/