Text Source
#AND #AMPLIFY #USING #FUNCTIONS #LAMBDA #AWS #WITH #PACKAGES #REUSE #HOW #NODEJS #LAMBDA #LAYERS
How to Reuse Node.js Packages with AWS Lambda Functions Using Amplify and Lambda Layers
In this article, you'll learn how to inject custom packages on AWS Lambda Functions' Runtime by using AWS Lambda Layers. You'll also use Amplify to develop, deploy, and distribute your applications.
Serverless applications are great for those who don't want to pay for idle machines or even manage cloud infrastructures. This post will focus on a gentle introduction to the Amplify Framework using Node.js. You'll install a custom Node.js package once and inject it on all your functions using AWS Lambda Layers.
Injecting a custom package on AWS Lambda functions' runtime using AWS Lambda Layers to monitor and extend HTTP requests.
In the next steps, you'll create a real application and publish it on the AWS Infrastructure. Before starting to code make sure you have the following requirements set up on your environment:
One of my favorites cloud platforms that provides serverless infrastructure is Amazon Web Services. They have long been developing and shipping platforms that empower companies to release software faster than if they had to configure and handle repetitive configurations.
If you're familiar with Serverless applications, you might have heard about the Serverless Framework. It's a multi-cloud framework for managing serverless architectures by using configuration files, deploying and running apps using a single command.
Even so, developers still need install plugins and manage configuration files by themselves and it could take a while to build a complex workflow.
So, what kind of CLI do you like most? A CLI that would ask you about what you want such as external connections (Database, Storage, Queue, etc), authentication flow, external permissions, and so on? Yeah my friend, welcome to AWS Amplify.
AWS Amplify is an ecosystem that helps back end, front end, and integration developers. If you take a look at its official docs, you'll see the huge list of possible libraries and examples to work on both back end and front and applications.
When you have your AWS CLI configured and your environment set, run the following command to install AWS Amplify globally on your machine:
Now, you'll initialize an amplify project by running the command below. To do so, you'll need to check a few options on the CLI wizard. Notice that when prompted you can press Enter on your keyboard to choose default values.
Through the next steps, you'll create a function. This function will be used to store dependencies which will be injected later into functions around the AWS Lambda Functions ecosystem.
Choose the options according to the bold text options below. Notice that for the compatible runtimes, you'll need to hit the space key on your keyboard to select the runtime.
Going to the Layer's path amplify/backend/function/apmAgentLayer you may have seen a few folders created by Amplify. As we're working on a Node.js project, all node modules must be installed on lib/nodejs.
I built an example of an Application Performance Monitor to show how to use the Node.js performance hooks feature to measure duration between requests and change HTTP response readers. It will help show you other possibilities to implement shared code and extend Node.js behaviour.
The first step here is to install the shared dependencies and upload them to the AWS. Go to the path generated by the CLI amplify/backend/function/apmAgentLayer/lib/nodejs and then install the package using the following commands:
Once you've installed your package you can just deploy it and inspect it later via the AWS Console. Notice that we haven't added any code yet. The goal at this point is just to prepare this library for further use.
At this point, you already have a local Amplify infrastructure project ready to add API Routes, Routines, link with AWS services, and so on.
The command below will be useful for generating an ExpressJS-based project and an AWS Lambda function. It'll also link the function on your nearly created AWS Lambda Layer and expose it on the AWS API Gateway.
Provide a friendly name for your resource to be used as a label for this category in the project: myApi
Choose the function template that you want to use: Serverless ExpressJS function (Integration with API Gateway)
As I'm using VSCode, the last wizard's answer will open the app.js file on my editor so I can edit. Now without adding other dependencies, let's import the Lambda Layer shared module on the first line of this file using the code below:
After editing the file, go to the terminal and hit Enter and choose the answers showed in bold below:
At the time of writing, we can't test Lambda Layers locally using AWS Amplify. But you're going deploy your project to AWS and test it in production by running the amplify push command again.
Notice that it will prin…
On this page of the site you can watch the video online How to Reuse Node.js Packages with AWS Lambda Functions Using Amplify and Lambda Layers with a duration of hours minute second in good quality, which was uploaded by the user How4Pc mac10 07 August 2020, share the link with friends and acquaintances, this video has already been watched 41 times on youtube and it was liked by 0 viewers. Enjoy your viewing!