node aws 内存溢出
by Mark Hopson
马克·霍普森(Mark Hopson)
如何使用Node.js和AWS快速创建无服务器RESTful API (How to quickly create a serverless RESTful API with Node.js and AWS)
In this beginner’s guide, we’ll briefly describe the “Serverless” software architecture, and then create a RESTful API using AWS, Node.js, and Swagger in a few short minutes.
在本初学者指南中,我们将简要描述“无服务器”软件架构,然后在短短几分钟内使用AWS,Node.js和Swagger创建RESTful API。
那么什么是无服务器的? (So what’s Serverless?)
The term Serverless (a.k.a. Functions-as-a-Service) describes a type of architecture that allows code to be deployed to, and run on, ephemeral and stateless containers from third-party vendors (like Azure or AWS).
无服务器(又称“功能即服务”)一词描述了一种体系结构,该体系结构允许将代码部署到第三方供应商(例如Azure或AWS)的临时和无状态容器中并在其上运行。
无服务器的好处 (Serverless Benefits)
Reduced operational management.Serverless architectures allows developers to focus on writing code, and not worry about configuring and managing the infrastructure that their code runs on.
减少运营管理。无服务器架构允许开发人员专注于编写代码,而不必担心配置和管理其代码所基于的基础架构。
Easy, flexible scaling.Since Serverless “functions” (your Serverless applications) are stateless and always invoked by an event (like an HTTP request), you can run as many, or as few, functions as you need. More invocations mean more containers. Depending on the scale and shape of your traffic, this can be very cost efficient, since Serverless functions are typically billed per invocation.
轻松灵活的缩放比例。由于无服务器“功能”(您的无服务器应用程序)是无状态的,并且总是由事件(例如HTTP请求)调用,因此您可以根据需要运行任意数量的功能。 更多的调用意味着更多的容器。 取决于流量的规模和形状,这可能非常具有成本效益,因为无服务器功能通常按调用计费。
无服务器的缺点 (Serverless Drawbacks)
Latency for initial requests (known as “cold starts”).If the Serverless function is inactive (has not been run in a while), then handling the first invocation can require extra time to complete because the container will have to initialize (that is, allocate host, load code, and so on).
初始请求的延迟(称为“冷启动”)。如果无服务器功能处于非活动状态(已经一段时间未运行),则处理第一次调用可能需要额外的时间才能完成,因为容器将必须初始化(即分配主机,装入代码等)。
Lack of system control. Since your code is running in an environment managed by a vendor, you won’t be able to control system upgrades, or dependencies outside of your code base.
缺乏系统控制。 由于您的代码在供应商管理的环境中运行,因此您将无法控制系统升级或代码库之外的依赖项。
什么是CloudFormation? (And what’s CloudFormation?)
CloudFormation is a service from Amazon that allows you to build AWS resources using templates. A template is a configuration file (YML or JSON) for provisioning all your AWS resources such as EC2 instances, DynamoDB tables, IAM roles and permissions, or anything else.
CloudFormation是Amazon提供的一项服务,可让您使用模板构建AWS资源。 模板是一个配置文件(YML或JSON),用于供应您的所有AWS资源,例如EC2实例,DynamoDB表,IAM角色和权限或其他任何资源。
让我们开始编码! (Let’s Start Coding!)
In this tutorial, we are going to make a simple RESTful API with the following two endpoints:
在本教程中,我们将使用以下两个端点制作一个简单的RESTful API:
POST / users / $ {userId} / hello (POST /users/${userId}/hello)
The request body will be saved in a DynamoDB table. In this tutorial, the request body must have this structure:{ "email": "any@" }
请求正文将保存在DynamoDB表中。 在本教程中,请求正文必须具有以下结构:{ "email": "any@" }
GET / users / $ {userId} / hello (GET /users/${userId}/hello)
The response will contain the value for"email"
set in the POST request.
响应将包含在POST请求中设置的"email"
值。
第1步:克隆仓库 (Step 1: Clone the repo)
There are two files that you need for this tutorial:index.js
(the NodeJS code for our Lambda function) andstack.yml
(the CloudFormation stack template). To get these files, visit this Github link.
本教程需要两个文件:index.js
(Lambda函数的stack.yml
代码)和stack.yml
(stack.yml
堆栈模板)。 要获取这些文件,请访问此 Github链接。
步骤2:检查stack.yml文件 (Step 2: Examine the stack.yml file)
Pay attention to thestack.yml
in the repo, as it is the config file that will be used by CloudFormation to create everything our application will require.
请注意回购中的stack.yml
,因为stack.yml
将使用该配置文件来创建应用程序所需的一切。
Below is a detailed diagram of all the AWS resources ourstack.yml
will need to create. The names that are used in the YML are in the red boxes.
以下是我们stack.yml
需要创建的所有AWS资源的详细图表。 YML中使用的名称在红色框中。
步骤3:上传您的CloudFormation模板 (Step 3: Upload your CloudFormation Template)
After you check out the YML, head over to this link and click theCreate Stackbutton. ChooseUpload a template to Amazon S3and upload thestack.yml
file.
签出YML后,转到此链接 ,然后单击“创建堆栈”按钮。 选择上载模板到Amazon S3并上载stack.yml
文件。
On the next screen, you will be ask to pick aStack name(can be anything). After this, click Next and selectI acknowledge that AWS CloudFormation might create IAM resources, and click Next again.
在下一个屏幕上,将要求您选择堆栈名称(可以是任何名称)。 之后,单击下一步,然后选择我确认AWS CloudFormation可能会创建IAM资源,然后再次单击下一步。
At this point, your stack is being created. Wait a minute on the Stacks page until your stack’s status becomesCREATE_COMPLETE.
此时,正在创建您的堆栈。 在“堆栈”页面上等待一分钟,直到您的堆栈状态变为CREATE_COMPLETE为止。
步骤4:查找由CloudFormation创建的Lambda (Step 4: Find your Lambda created by CloudFormation)
Once your stack is complete, go and find your stack’s new Lambda here. Your Lambda’s Function name should resemble${StackName}-HelloLambda-XXXX.
堆栈完成后,请在此处找到堆栈的新Lambda。 您的Lambda的函数名称应类似于$ {StackName} -HelloLambda-XXXX。
步骤5:将代码部署(复制并粘贴)到Lambda (Step 5: Deploy (copy and paste) your code to your Lambda)
Once you’ve found your Lambda, click on it for more details. Then scroll to theFunction Codesection, change theCode entry typetoEdit code inline, then open and copyindex.js
(from the repo) into the code editor. ClickSave.
找到Lambda后,请单击它以获取更多详细信息。 然后滚动到“功能代码”部分,将“代码”输入类型更改为“内联编辑代码”,然后打开并将index.js
(从仓库中)复制到代码编辑器中。 点击保存。
At this point, your code has been “deployed” to the Lambda, and all that’s left is to deploy our API Gateway so we can send HTTP requests to it.
至此,您的代码已“部署”到Lambda,剩下的就是部署我们的API网关,以便我们可以向其发送HTTP请求。
步骤6:查找由CloudFormation创建的API网关 (Step 6: Find your API Gateway that was created by CloudFormation)
Find your API Gateway created by your CloudFormation template here. Your API Gateway’s name should resemble${StackName}-MyApiGateway.
在此处找到由CloudFormation模板创建的API网关。 您的API网关的名称应类似于$ {StackName} -MyApiGateway。
步骤7:测试您的API网关是否已连接到Lambda (Step 7: Test if your API Gateway is hooked up to Lambda)
After you found your API Gateway, we can test to see if everything is hooked up by selecting thePOSToption under/usersand then clickingTEST .
找到您的API网关后,我们可以通过选择/ users下的POST选项,然后单击TEST来测试是否已挂接所有内容。
On the Test page, setuserIdto 123, and set theRequest Bodyto the following and clickTest. If everything worked, theStatusshould be200with no data.
在“测试”页面上,将userId设置为123,并将“请求正文”设置为以下内容,然后单击“测试”。 如果一切正常,状态应为200,无数据。
After testing the POST endpoint, you can check to see if your data was saved by going to the /hello GET Test page and trying a request (remember to setuserIdto 123). The response body should contain the Request Body from the POST test (see above).
测试POST端点之后,可以转到/ hello GET Test页面并尝试请求(请记住将userId设置为123),以查看是否保存了数据。 响应主体应包含POST测试中的请求主体(请参见上文)。
Now that you’ve verified that your API Gateway, Lambda, and DynamoDB are hooked up, you can deploy your API Gateway so you can reach it from the internet.
既然您已经确认API网关,Lambda和DynamoDB已连接,则可以部署API网关,以便从Internet进行访问。
步骤8:部署您的API网关 (Step 8: Deploy your API Gateway)
To deploy your API, click the Actions menu and select Deploy API. Once the confirmation pop-up appears, setDeployment stagetoprodand then clickDeploy.
要部署您的API,请单击“操作”菜单,然后选择“部署API”。 出现确认弹出窗口后,将Deployment stage设置为prod,然后单击Deploy。
Once you’ve deployed your API, you will be forwarded to theStagespage forprod. Here you will find the domain for your API Gateway in the blue highlighted area besideInvoke URL.
部署API后,您将转到prod的“阶段”页面。 在此处,您可以在Invoke URL旁边的蓝色突出显示区域中找到API网关的域。
Using the URL from the screenshot above, I should be able to send aGET /users/123/hellorequest in my web browser like below.
使用上面的屏幕快照中的URL,我应该能够在Web浏览器中发送GET / users / 123 / hello请求,如下所示。
And that’s it! You now have a Serverless RESTful API that is scalable, reliable, doesn’t require patching or provisioning, and doesn’t cost money when idle. I hope you’ve enjoyed this tutorial, and if you have any feedback, please leave it in the comments below. Thanks!
就是这样! 现在,您有了一个无服务器RESTful API,该API具有可伸缩性,可靠性,不需要修补或置备,并且在空闲时也不需要花钱。 希望您喜欢本教程,如果有任何反馈,请留在下面的评论中。 谢谢!
其他说明和标注 (Other Notes and Callouts)
The route configuration for API Gateway is embedded inside the API Gateway (MyApiGateway) configuration insidestack.yml
, which makes the YML more of a monstrosity than it already is.
API网关的路由配置嵌入在stack.yml内的API网关(MyApiGateway)配置内,这使YML变得比以前更加stack.yml
。
Environment variables inside the HelloLambda Lambda configuration page contain the info needed to connect to the HelloTable DynamoDB table.HelloLambda Lambda配置页面中的环境变量包含连接到HelloTable DynamoDB表所需的信息。
The AWS-SDK comes bundled with every Lambda function so we can userequire('aws-sdk')
without apackage.json
. Very handy!
AWS-SDK与每个Lambda函数捆绑在一起,因此我们可以在没有package.json
情况下使用require('aws-sdk')
。 非常便利!
Instead of copying and pasting the NodeJS code into the embedded editor inside the Lambda Details page, you can deploy your code through the AWS CLI. We copy and paste for simplicity.您可以通过AWS CLI部署代码,而不是将NodeJS代码复制并粘贴到Lambda Details页面内的嵌入式编辑器中。 为了简单起见,我们复制并粘贴。
Be warned, the CloudFormation Stack Template is overwhelmingly verbose by nature. I promise it’s not just me and mystack.yml
.
请注意,CloudFormation堆栈模板本质上过于冗长。 我保证不仅仅是我和我的stack.yml
。
HelloTable DynamoDB table’s primary partition key isuserId
HelloTable DynamoDB表的主分区键为userId
From u/SalamiJack: “it’s worth calling out that API Gateway + Lambda performance, even for a warmed up, simple Lambda, is quite bad. Expect in the realm of 80–150ms response times at all times.”
来自u /SalamiJack:“值得一提的是,即使对于一个简单的Lambda进行预热,API Gateway + Lambda的性能也是相当糟糕的。 始终期望在80-150ms的响应时间范围内。”
Originally published at on March 26, .
最初于3月26日发布在上。
翻译自: /news/quickly-create-a-serverless-restful-api-with-nodejs-and-aws-lambda-api-gateway-and-a6be891cc16a/
node aws 内存溢出
如果觉得《node aws 内存溢出_如何使用Node.js和AWS快速创建无服务器RESTful API》对你有帮助,请点赞、收藏,并留下你的观点哦!