aws lambda
Now-a-days, any kind of computing tasks such as Real-time data processing, pre-process data for Machine learning models, Serverless back-ends (web, mobile, Internet of Things) can be done using AWS Lambda.
ñ流,一个天,任何种类的计算任务,如实时数据处理,机器学习模型,无服务器后端处理前数据(网络,物联网的移动电话,互联网),可以使用AWS LAMBDA完成。
While performing all above mentioned tasks, achieving scalability, higher performance and cost efficiency, and also not worrying about the underlying infrastructure, can be tiresome.
在执行上述所有任务时,实现可伸缩性,更高的性能和成本效率以及不担心底层基础结构可能很烦人。
But AWS has offered a powerful set of tools for building scalable and secure applications.Discover the various Lambda benefits, including the following.
但是AWS提供了一套强大的工具来构建可扩展和安全的应用程序。发现Lambda的各种好处,包括以下内容。
When to VPC-Enable a Lambda Function?
何时启用VPC的Lambda函数?
Only enable your functions for VPC access when you need to interact with a private resource located in a private subnet.
仅当需要与位于专用子网中的专用资源进行交互时,才启用用于VPC访问的功能。
High Availability
高可用性
Design Lambda for high availability by selecting multiple subnets in different AZ’s. 通过选择不同可用区中的多个子网来设计Lambda以实现高可用性。 If an Availability Zone (AZ) goes down, other AZ’s need to have sufficient IP addresses allocated to handle concurrent Lambda requests. 如果可用区(AZ)出现故障,则其他可用区需要分配足够的IP地址来处理并发的Lambda请求。Note: Each Lambda execution will require one private IP address to handle the request. So we need to allocate sufficient IP addresses in a subnet for HA.
注意:每次执行Lambda都需要一个私有IP地址来处理请求。 因此,我们需要在子网中为HA分配足够的IP地址。
Concurrency
并发
Always reserve the concurrency limit for each Function so the impact is isolated to only that function if the number of events surges for any reason. 始终为每个功能保留并发限制,因此,如果事件数量由于任何原因激增,则影响仅隔离到该功能。 Concurrency provisioning for Lambda Function. Lambda函数的并发配置。Balance between memory and cost
内存和成本之间的平衡
Monitor the usage of the memory and execution time through CloudWatch Logs and then adjust the configuration accordingly.
通过CloudWatch Logs监视内存使用情况和执行时间,然后相应地调整配置。
CloudWatch logs of the Lambda Function. Lambda函数的CloudWatch日志。Performance
性能
a) Cold start — When we invoke an inactive lambda function or a new Lambda function for the first time, it does download the code from S3, download all the dependencies, create a container, and start the application before it executes the code. This whole duration (except the execution of code) is known as a cold start time
a) 冷启动 — 首次调用不活动的lambda函数或新的Lambda函数时,它确实从S3下载代码,下载所有依赖项,创建容器并在执行代码之前启动应用程序。 这整个持续时间(代码执行除外)称为冷启动时间
b) Warm start — Once the container is up and running, for subsequent Lambda invocation, Lambda is already initialized and it just needs to execute the application logic and that duration is, called warm start time.
b) 热启动 — 一旦容器启动并运行,对于随后的Lambda调用,Lambda已被初始化,它只需要执行应用程序逻辑,该持续时间称为热启动时间 。
Lambda Function Lifecycle. Lambda函数生命周期。Improve the Lambda performance overall using following:
使用以下方法整体提高Lambda性能:
1. If possible, Choose interpreted languages like Node.js, Python rather than Java, C++ to reduce the cold start time.
1. 如果可能 ,请选择诸如Node.js,Python之类的解释型语言,而不是Java,C ++等,以减少冷启动时间。
2. Use the default network environment unless you need a VPC resource with a private IP. Because setting up ENI (Elastic Network interface) takes significant time and add to the cold start time.
2.使用默认的网络环境,除非您需要带有专用IP的VPC资源。 因为设置ENI(弹性网络接口)需要花费大量时间,而且会增加冷启动时间。
3. Remove all unnecessary dependencies which are not required to run the function. Keep only the ones which are required at runtime only.
3.删除运行该功能不需要的所有不必要的依赖项。 仅保留运行时所需的那些。
4. Use Global/Static variables, Singleton objects — these remain alive until the container goes down. So any subsequent call does not need to reinitialize these variables/objects.
4.使用Global / Static变量,Singleton对象-这些变量将保持活动状态,直到容器关闭。 因此,任何后续调用都不需要重新初始化这些变量/对象。
5. Define your database connections at a global level so that it can be reused for subsequent invocation.
5.在全局级别定义数据库连接,以便可以将其重新用于后续调用。
Security
安全
1. One IAM Role should be mapped with only one function even though multiple functions need same IAM policies. It helps to ensure the least privilege policies when any enhancement happens for the security policies for the specific function.
1.即使多个功能需要相同的IAM策略,也应仅将一个IAM角色映射为一个功能。 当针对特定功能的安全策略进行任何增强时,它有助于确保最少特权策略。
2. As Lambda would be running on shared VPC, it is not good practice to keep the AWS credential in code.
2.由于Lambda将在共享VPC上运行, 因此将AWS凭证保留在代码中并不是一种好习惯。
Blue Green Deployment
蓝绿色部署
1. We can publish multiple versions of a function using Versioning and Aliases features.
1.我们可以使用“ 版本控制”和“ 别名”功能发布功能的多个版本。
2. Use version feature during development for creating multiple environments. Do not use it directly for Production environment as every time we upload new code, the version will be incremented and clients need to point to the new one.
2.在开发过程中使用版本功能来创建多个环境。 不要在生产环境中直接使用它,因为每次我们上传新代码时,版本都会增加,并且客户需要指向新代码。
3. Aliases refer to a specific version of the lambda function. So if the code changes and a newer version is published, event source will still point to the same alias but the alias will be updated to refer to the newer version.
3. 别名是指lambda函数的特定版本。 因此,如果代码更改并且发布了较新的版本,则事件源仍将指向相同的别名,但是该别名将被更新以引用较新的版本。
Monitoring
监控方式
1. Lambda function automatically tracks the number of requests, the execution duration per request, and the number of requests resulting in an error and publishes the associated CloudWatch metrics. You can leverage these metrics to set CloudWatch custom alarms as well.
1. Lambda函数自动跟踪请求数,每个请求的执行持续时间以及导致错误的请求数,并发布相关的CloudWatch指标。 您还可以利用这些指标来设置CloudWatch自定义警报。
CloudWatch Duration Metric. CloudWatch持续时间指标。2. Use X-Ray to identify potential bottlenecks in the Lambda execution.
2.使用X射线识别Lambda执行过程中的潜在瓶颈。
Miscellaneous
杂
1. Don’t use AWS Lambda Console for developing Production code.
1.请勿使用AWS Lambda Console开发生产代码。
2. Use AWS SAM or Serverless framework for development.
2.使用AWS SAM或无服务器框架进行开发。
3. Plan for CI/CD for Lambda deployment same as what you do for other deliverable.
3.与其他交付项目一样,为Lambda部署计划CI / CD。
4. Use Environment Variables (and AWS Systems Manager Parameter Store) to separate code from configuration.
4.使用环境变量(和AWS Systems Manager参数存储 )将代码与配置分开。
翻译自: https://medium.com/@kalpeshbhangre96/aws-lambda-best-practices-e9f27845fffd
aws lambda
相关资源:微信小程序源码-合集6.rar