How to Deploy a Lambda function as a Component on an Edge Machine using AWS Greengrass v2

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 lets create a Lambda function and upload code from this zip file.
  • Simply open AWS > Lambda > Create function > Name that function > Select runtime as Python 3.8 > Create Function.
  • When the above steps are done you will see a screen like this.
  • Now select Upload from in the bottom right of 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 AWS search console.
  • Click on Greengrass in 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.

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.
  • 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!! message that we printed through our Lambda Function.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Abhishek Sharma

Abhishek Sharma

Data Scientist || Blogger || machinelearningprojects.net || Contact me for freelance projects on asharma70420@gmail.com