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@email.com" }

请求正文将保存在DynamoDB表中。 在本教程中,请求正文必须具有以下结构: { "email": "any@email.com" }

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) and stack.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 the stack.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 our stack.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 the Create Stack button. Choose Upload a template to Amazon S3 and upload the stack.yml file.

签出YML后,转到此链接 ,然后单击“ 创建堆栈”按钮。 选择上载模板到Amazon S3并上载stack.yml文件。

On the next screen, you will be ask to pick a Stack name (can be anything). After this, click Next and select I 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 becomes CREATE_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 the Function Code section, change the Code entry type to Edit code inline, then open and copy index.js (from the repo) into the code editor. Click Save.

找到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 the POST option under /users and then clicking TEST .

找到您的API网关后,我们可以通过选择/ users下的POST选项,然后单击TEST测试是否已挂接所有内容

On the Test page, set userId to 123, and set the Request Body to the following and click Test. If everything worked, the Status should be 200 with 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 set userId to 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, set Deployment stage to prod and then click Deploy.

要部署您的API,请单击“操作”菜单,然后选择“部署API”。 出现确认弹出窗口后,将Deployment stage设置为prod ,然后单击Deploy

Once you’ve deployed your API, you will be forwarded to the Stages page for prod. Here you will find the domain for your API Gateway in the blue highlighted area beside Invoke URL.

部署API后,您将转到prod的“ 阶段”页面。 在此处,您可以在Invoke URL旁边的蓝色突出显示区域中找到API网关的域。

Using the URL from the screenshot above, I should be able to send a GET /users/123/hello request 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 inside stack.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 use require('aws-sdk') without a package.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 is userId

    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 medium.com on March 26, 2018.

最初于2018年3月26日发布在medium.com上。

翻译自: https://www.freecodecamp.org/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相关推荐

  1. node aws 内存溢出_如何使用Node和AWS S3设置简单的图像上传

    node aws 内存溢出 by Filip Jerga 由Filip Jerga 如何使用Node和AWS S3设置简单的图像上传 (How to set up simple image uploa ...

  2. node aws 内存溢出_在AWS Elastic Beanstalk上运行生产Node应用程序的现实

    node aws 内存溢出 by Jared Nutt 贾里德·努特(Jared Nutt) 在AWS Elastic Beanstalk上运行生产Node应用程序的现实 (The reality o ...

  3. vue项目node编译内存溢出(node)

    vue项目在启动以后,进行修改内容,编译出错,自动退出启动 报错如下 经过查找 FATAL ERROR: CALL_AND_RETRY_LAST Allocation failed - JavaScr ...

  4. aws生态系统集成商_通过通用数据访问扩展AWS生态系统

    aws生态系统集成商 Amazon Web Services(AWS)可帮助组织托管和管理其数据流程,例如构建数据可视化和执行ETL任务. 在CData,我们可以轻松地将AWS Services与异构 ...

  5. sam服务器是什么_使用SAM CLI将机器学习模型部署到无服务器后端

    sam服务器是什么 介绍 (Introduction) Over the last year at CMRA, we have been incorporating more machine lear ...

  6. 服务器创建多个dhcp服务_如何在15分钟内创建无服务器服务

    服务器创建多个dhcp服务 by Charlee Li 通过李李 如何在15分钟内创建无服务器服务 (How to create a serverless service in 15 minutes) ...

  7. 栈溢出 栈内存溢出_全栈溢出开发人员

    栈溢出 栈内存溢出 This article is part of a web development series from Microsoft. Thank you for supporting ...

  8. node开启子线程_多进程 amp; Node.js web 实现

    更好阅读体验: 多进程 & Node.js 实现 · 语雀​www.yuque.com 进程与线程 进程和线程的诞生要从多任务谈起,多任务是指操作系统可以在同一时间内运行多个应用程序,CPU ...

  9. arthas 排查内存溢出_【spark-tips】spark2.4.0触发的executor内存溢出排查

    版本升级背景 spark 2.4.0 最近刚发版,新增了很多令人振奋的特性.由于本司目前使用的是spark 2.3.0版本,本没打算这么快升级到2.4.0.无奈最近排查出的两个大bug迫使我们只能对s ...

最新文章

  1. 常用深度学习框——Caffe/TensorFlow / Keras/ PyTorch/MXNet
  2. tf.squeeze示例代码
  3. 【Java小工匠聊密码学】-密码学--综述
  4. notepad++的NppFTP插件远程连接linux操作系统
  5. phpMyAdmin常用设置
  6. 编译时检查JPA查询
  7. cups共享linux打印机_linux入门-映射网络驱动器
  8. java多线程命名,命名线程和当前线程
  9. 随想录(分布式系统)
  10. spring复杂数据类型传递
  11. 深入C++中构造函数、拷贝构造函数、赋值操作符、析构函数的调用过程总结
  12. 商务与经济统计++原书第12版+[(美)安德森著][机械工业出版社][2015.07][515页][13854037]第一章读书笔记
  13. 微信小程序tabbar消失_微信小程序tabbar不显示解决办法
  14. 对draw.io第一次加载慢的探讨
  15. 【信源编码技术】实验1-浊音清音爆破音的时域及频域特性分析
  16. 科大讯飞输入法android离线语音,讯飞输入法Android5.0.1752 离线语音更轻快的表达...
  17. 几个设计原则的优先级排序
  18. 【其他】记录这次曲折的域名选择及搜索引擎seo优化的过程
  19. 开源工具 — 有了这款免费的开源作图工具,我放弃了Visio
  20. 【物联网】阿里云物联网应用托管

热门文章

  1. 一个逻辑清晰的购物车模型
  2. java 初识对象和对象引用的关系
  3. SpringBoot b2b2c 多用户商城系统(十五)Springboot整合RabbitMQ...
  4. ASP.NET 下载文件方式
  5. 如何让phpmyadmin输入密码再进入
  6. 测试驱动开发与行为驱动开发中的测试先行方法
  7. PARAMETERS 指令
  8. hibernate 全面学习【lazy策略 】
  9. {好文备份}SQL索引一步到位
  10. java List集合中contains方法总是返回false