amplify aws_如何使用Amplify和Lambda层重用带有AWS Lambda函数的Node.js包

amplify aws

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.

在本文中,您将学习如何使用AWS Lambda Layers在AWS Lambda Functions的运行时注入自定义包。 您还将使用Amplify开发,部署和分发应用程序。

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.

无服务器应用程序非常适合那些不想为空闲机器付费甚至不想管理云基础架构的人。 这篇文章将重点介绍使用Node.jsAmplify框架的 简要介绍 您将安装一次自定义Node.js程序包,然后使用AWS Lambda Layers将其注入所有功能。

By the end of this post you will have learned how to deploy serverless functions on AWS by:

在本文结束时,您将通过以下方法学习如何在AWS上部署无服务器功能:

  • Creating a Node.js Web API using Express.js as a serverless function using the Amplify Framework

    使用Amplify框架将Express.js用作无服务器功能来创建Node.js Web API

  • Injecting a custom package on AWS Lambda functions' runtime using AWS Lambda Layers to monitor and extend HTTP requests.

    使用AWS Lambda Layers在AWS Lambda函数的运行时注入自定义包,以监视和扩展HTTP请求。

要求 (Requirements)

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:

在接下来的步骤中,您将创建一个真实的应用程序并将其发布在AWS Infrastructure上。 在开始编码之前,请确保您在环境上设置了以下要求:

介绍 (Introduction)

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.

我最喜欢的提供无服务器基础架构的云平台之一是Amazon Web Services。 他们长期以来一直在开发和发布平台,使公司能够比必须配置和处理重复配置更快地发布软件。

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.

那么,您最喜欢哪种CLI? 一个CLI会询问您所需的信息,例如外部连接(数据库,存储,队列等),身份验证流,外部权限等? 是的,我的朋友,欢迎来到AWS Amplify。

AWS放大 (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.

AWS Amplify是一个生态系统,可帮助后端,前端和集成开发人员。 如果查看其官方文档 ,您会看到大量可能在后端,前端和应用程序上运行的库和示例列表。

When you have your AWS CLI configured and your environment set, run the following command to install AWS Amplify globally on your machine:

配置好AWS CLI并设置环境后,运行以下命令以在您的计算机上全局安装AWS Amplify:

npm install -g amplify-cli

npm install -g amplify-cli

Next, let's initialize a work directory by creating a folder:

接下来,让我们通过创建文件夹来初始化工作目录:

mkdir app && cd app

mkdir app && cd app

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.

现在,您将通过运行以下命令来初始化一个放大项目。 为此,您需要在CLI向导中检查一些选项。 请注意,出现提示时,您可以按键盘上的Enter键选择默认值。

amplify init

amplify init

Choose the options according to the bold text below:

根据粗体文本选择选项 下面:

  • Enter a name for the project app

    输入项目应用程序的名称

  • Enter a name for the environment dev

    输入环境开发人员的名称

  • Choose your default editor: Visual Studio Code

    选择默认编辑器: Visual Studio Code

  • Choose the type of app that you're building javascript

    选择您要构建javascript的应用程序的类型

Please tell us about your project

请告诉我们您的项目

  • What javascript framework are you using none

    没有使用什么JavaScript框架

  • Source Directory Path:  src

    源目录路径: src

  • Distribution Directory Path: dist

    分发目录路径: dist

  • Build Command:  npm run-script build

    生成命令: npm run-script build

  • Start Command: npm run-script start

    启动命令: npm run-script start

初始化共享项目 (Initialising the shared project)

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.

在接下来的步骤中,您将创建一个函数。 该函数将用于存储依赖关系,这些依赖关系随后将注入到AWS Lambda Functions生态系统周围的函数中。

Running the command below will guide you towards steps to create your Lambda Layer:

运行以下命令将指导您逐步创建Lambda层:

amplify function add

amplify function add

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.

根据粗体文本选择选项 下面的选项。 请注意,对于兼容的运行时,您需要按键盘上的空格键以选择运行时。

  • Select which capability you want to add: Lambda layer (shared code & resource used across functions)

    选择要添加的功能:Lambda层(跨函数共享的代码和资源)
  • Provide a name for your Lambda layer: apmAgentLayer

    为您的Lambda图层提供一个名称: apmAgentLayer

  • Select up to 2 compatible runtimes: NodeJS

    最多选择2个兼容的运行时: NodeJS

  • The current AWS account will always have access to this layer.

    当前的AWS账户将始终有权访问此层。
  • Optionally, configure who else can access this layer. (Hit to skip) Public

    (可选)配置其他人可以访问此层。 (点击跳过) 公开

✅ Lambda layer folders and files created: amplify/backend/function/apmAgentLayer

created创建的Lambda图层文件夹和文件:amplify / backend / function / apmAgentLayer

安装自定义模块 (Installing custom modules)

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.

转到图层的路径amplify / backend / function / apmAgentLayer,您可能已经看到Amplify创建的一些文件夹。 在我们处理Node.js项目时,所有节点模块必须安装在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.

我构建了一个应用程序性能监视器的示例,以显示如何使用Node.js 性能挂钩功能来度量请求之间的持续时间并更改HTTP响应阅读器。 它将帮助您展示实现共享代码和扩展Node.js行为的其他可能性。

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:

此处的第一步是安装共享依赖项,并将其上载到AWS。 转到CLI 放大/后端/功能/ apmAgentLayer / lib / nodejs生成的路径,然后使用以下命令安装软件包:

cd amplify/backend/function/apmAgentLayer/lib/nodejs
npm i @erickwendel/ew-agent

部署中 (Deploying)

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.

安装软件包后,您可以部署它并稍后通过AWS控制台对其进行检查。 请注意,我们尚未添加任何代码。 此时的目标只是准备该库以备将来使用。

Run the following command to upload your Lambda Layer:

运行以下命令以上传您的Lambda层:

amplify push

amplify push

创建Web API函数 (Creating the Web API function)

At this point, you already have a local Amplify infrastructure project ready to add API Routes, Routines, link with AWS services, and so on.

此时,您已经有一个本地Amplify基础设施项目,可以添加API路由,例程,与AWS服务的链接等。

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.

以下命令对于生成基于ExpressJS的项目和AWS Lambda函数很有用。 它还将在几乎创建的AWS Lambda层上链接该函数,并将其在AWS API Gateway上公开。

amplify api add

amplify api add

Choose the options according to the bold text below:

根据粗体文本选择选项 下面:

  • Please select from one of the below mentioned services: REST

    请从以下提及的服务之一中进行选择: REST

  • Provide a friendly name for your resource to be used as a label for this category in the project: myApi

    为您的资源提供一个友好名称,以用作项目中此类别的标签: myApi

  • Provide a path (e.g., /book/{isbn}): /hi

    提供路径(例如/ book / {isbn}): / hi

  • Choose a Lambda source Create a new Lambda function

    选择Lambda源创建新的Lambda函数

  • Provide a friendly name for your resource to be used as a label for this category in the project: myApi

    为您的资源提供一个友好名称,以用作项目中此类别的标签: myApi

  • Provide the AWS Lambda function name: myApi

    提供AWS Lambda函数名称: myApi

  • Choose the runtime that you want to use: NodeJS

    选择您要使用的运行时: NodeJS

  • Choose the function template that you want to use: Serverless ExpressJS function (Integration with API Gateway)

    选择要使用的功能模板: 无服务器ExpressJS功能(与API Gateway集成)

  • Do you want to access other resources in this project from your Lambda function? No

    您是否要通过Lambda函数访问此项目中的其他资源? 没有

  • Do you want to invoke this function on a recurring schedule? No

    是否要定期重复调用此功能? 没有

  • Do you want to configure Lambda layers for this function? Yes

    是否要为此功能配置Lambda层?

  • Provide existing layers or select layers in this project to access from this function (pick

    在此项目中提供现有图层或选择图层以从此功能访问(选择

    up to 5):

    最多5个):

    apmAgentLayer

    apmAgentLayer

  • Select a version for apmAgentLayer: 1

    选择apmAgentLayer的版本: 1

  • Do you want to edit the local lambda function now? Yes

    您要立即编辑本地Lambda函数吗?

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:

当我使用VSCode时,最后一个向导的答案将在编辑器上打开app.js文件,以便我进行编辑。 现在,无需添加其他依赖关系,让我们使用以下代码在该文件的第一行导入Lambda Layer共享模块:

require('@erickwendel/ew-agent').start()

After editing the file, go to the terminal and hit Enter and choose the answers showed in bold below:

编辑完文件后,转到终端并按Enter,然后选择下面以粗体显示的答案:

  • Restrict API access No

    限制API的访问

  • Do you want to add another path? No

    您要添加其他路径吗? 没有

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.

在撰写本文时,我们无法使用AWS Amplify在本地测试Lambda层。 但是您要将项目部署到AWS并通过以下方式在生产中进行测试 再次运行amplify push命令。

Notice that it will print out which resources need to be updated and which resources will be created on this deploy. It'd take a while to perform all operations and your output should look like the below:

注意,它将在此部署上打印出需要更新哪些资源以及将创建哪些资源。 执行所有操作将需要一些时间,您的输出应如下所示:

As your terminal may have shown, your API has now a URL. My generated URL is https://nlq7x7onj0.execute-api.us-east-1.amazonaws.com/dev and the route will be hi, which we created together in the previous steps.

如您的终端所显示的,您的API现在具有一个URL。 我生成的URL是https://nlq7x7onj0.execute-api.us-east-1.amazonaws.com/dev ,路由为hi ,这是我们在前面的步骤中一起创建的。

Let's trigger a request using the cURL (or even your browser) to see check what happens:

让我们使用cURL(甚至您的浏览器)触发一个请求,以查看发生了什么:

curl -i https://nlq7x7onj0.execute-api.us-east-1.amazonaws.com/dev/hi

After running it, the API should respond with a JSON response with the following content {"success":"get call succeed!","url":"/hi"}. The Lambda Layer was injected and it should have changed your response headers adding the x-instrumented-by and x-request-id keys like this:

在运行它之后,API应该使用以下内容{"success":"get call succeed!","url":"/hi"}来响应JSON响应。 Lambda层已被注入,它应该已经更改了您的响应头,并添加了x-instrumented-byx-request-id键,如下所示:

x-instrumented-by: ErickWendel
x-request-id: 5ddf1343-e42e-4e33-b1e1-936c303c14c8

If you're curious about what Amplify has managed for you during this tutorial, run amplify console and browse on the dashboard. You can see mine below:

如果您对本教程中Amplify为您管理的内容感到好奇,请运行amplify console并在仪表板上浏览。 您可以在下面看到我的:

打扫干净 (Cleaning up)

To remove all resources created by Amplify, run amplify delete.

要删除Amplify创建的所有资源,请运行amplify delete

结论 (Conclusion)

There are a lot of ways to improve your experience on serverless applications. The Amplify Framework can help you build next-generation apps and avoid repetitive tasks.

有很多方法可以改善无服务器应用程序的体验。 Amplify Framework可以帮助您构建下一代应用程序并避免重复的任务。

Check the official docs to see other possibilities to build powerful APIs using cutting-edge technologies such as GraphQL and AWS AppSync. I'm sure it'll help you a lot!

查看官方文档,了解使用GraphQL和AWS AppSync等尖端技术构建强大API的其他可能性。 我相信它将对您有很大帮助!

感谢您的阅读 (Thank you for reading)

I really appreciate the time we spent together. I hope this content will be more than just text. I hope it will have made you a better thinker and also a better programmer. Follow me on Twitter and check out my personal blog where I share all my valuable content.

我真的很感谢我们一起度过的时间。 我希望这些内容将不仅仅是文字。 我希望它将使您成为一个更好的思想家和一个更好的程序员。 在Twitter上关注我,并查看我的个人博客 ,我在其中分享了我所有的宝贵内容。

See ya! 🍻

拜拜! 🍻

翻译自: https://www.freecodecamp.org/news/how-to-reuse-packages-with-aws-lambda-functions-using-amplify/

amplify aws

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值