Recently I was trying to deploy a Lambda function as a component on an Edge Machine using AWS Greengrass v2 for a client and you won’t believe I have never done such a frustrating task in my life. After doing it wrong for about 30-40 times I finally deployed it 🙂
And this is what today’s blog is about. I don’t want anyone else to face the same frustration that I faced while doing this process. So without any further due let’s do it…
Step 1 – Create a Lambda Function.
- This is the simple lambda function we are using today.
- This lambda function will just print Deployed successfully!! in the logs of the component, we are going to deploy.
- For printing logs, we are using a library called loguru.
- For this purpose, we will create our Lambda from a zip containing lambda_function.py and loguru library files.
- To install loguru in the current folder use the command.
pip install loguru -t .
- After it’s installed, your folder should look like this.
- It has lambda_function.py and loguru library files.
- Now we need to select all these and create a compressed zip file out of it.
- Now let’s create a Lambda function and upload the code from this zip file.
- Simply open AWS > Lambda > Create function > Name that function > Select runtime as Python 3.8 > Create Function.
Note – AWS Greengrass v2 is compatible with Python 3.7 and python 3.8 but not Python 3.9.
- When the above steps are done you will see a screen like this.
- Now select Upload from in the bottom right of the image and select .zip file.
- It will successfully load all the files from your zip and will show them as below. If your zip is very big (like 30 40 MBs or even further) it might not show folders and code like this and that’s totally fine.
Step 2 – Creating versions and alias for our Lambda Function.
- Now to make a component out of our lambda function, first we need to create a version and then give that version an alias(nickname).
- Click on the Actions in the top right corner of the Lambda Function screen and click on Publish new version.
- It will ask version description. Write v1.
- Then again click on Actions and click on Create Alias.
- In its name write v1 and click on Save.
Step 3 – Make a Component in AWS Greengrass using this Lambda Function.
- Now search Greengrass in the AWS search console.
- Click on Greengrass in the left sidebar and then click on Components.
- Click on Create Component and it will open up a screen like shown below.
- On this screen first of all select Import Lambda Function and select your Lambda function from the dropdown.
- Give your component a name.
- I named it Test-Component.
- Now scroll below and select No Container in Isolation mode if you don’t have any special use of Greengrass containerization. Read more about it here.
- Scroll down and click on Create Component.
- If you see a screen like shown below, your component is made successfully and is ready to be deployed.
Note – There is one more very important configuration present here. Its name is Pinned Lambda. Pinned Lambda means it is long-living Lambda and it will not stop unless stopped explicitly. If you have a use case in which you have to continuously do some work or continuously assess something, write your code inside a while loop in lambda_function.py, make your component pinned lambda, and do all other steps same.
Step 4 – Deploy a Lambda function as a Component on an Edge Machine using AWS Greengrass v2.
- Now click on the Deploy button on the top.
- A list of core devices will popup, select the core device in which you want to deploy the component and click on Next.
- In step 1 give your deployment a name and just click Next.
- In step 2 select/check the components. It’s a good practice to select the below 2 components also. If you don’t see them, just click on the blue toggle button and search for them.
- Click Next.
- In steps 3, 4, and 5 just click Next, Next and Next and you are done with your deployment.
If everything you did till now fits in place you have successfully deployed your component on the Edge device.
- Now let’s check the logs in the edge machine and verify the deployment.
- The path at which the logs are stored is /greengrass/v2/logs/<component-name>.log
- Simply open your edge machine and enter the following commands.
sudo -i // Enter your password cd /greengrass/v2/logs/ ls -lrt
- And it will open up a list of all deployed components.
- Sometimes it takes some time (3-4 minutes) to deploy.
- Once you see your component’s name there open the file using the following command.
more <log file name>
- And Boom here is the results. We can see the Deployed successfully!! the message that we printed through our Lambda Function.
And this is how you Deploy a Lambda function as a Component on an Edge Machine using AWS Greengrass v2.
Do let me know if there’s any query while you deploy a Lambda function as a component on an Edge Machine using AWS Greengrass v2 by contacting me via email or LinkedIn.
So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…
Read my previous post: How to build OpenCV with Cuda and cuDNN support in Windows