什么是游览器兼容性问题原因
So, you’ve started your serverless journey. It’s new and exciting and there’s lots to learn. You begin with your first AWS Lambda function. Everything looks fine and it just works, your Lambda gets an input event and produces output.
因此,您已经开始了无服务器之旅。 它是新的,令人兴奋,还有很多东西要学习。 您将从第一个AWS Lambda函数开始。 一切看起来都很好,并且一切正常,您的Lambda得到了输入事件并产生了输出。
However, problems tend to arise when unhandled exceptions and failures are encountered. These prove to be rather expensive, when not dealt with properly, as they can cause unexpected bugs, security issues and costly Lambda retries.
但是,遇到未处理的异常和故障时,往往会出现问题。 如果处理不当,它们会变得非常昂贵,因为它们可能导致意外的错误, 安全问题以及昂贵的Lambda重试。
In this blog, we’ll discuss how to parse event schemas correctly and how to handle event validation exceptions. I’ll focus on Python, but these guidelines & tips are applicable to any other programming language.
在此博客中,我们将讨论如何正确解析事件模式以及如何处理事件验证异常。 我将重点介绍Python,但是这些准则和提示适用于任何其他编程语言。
问题? 什么问题? (Problems? What problems?)
Let’s observe the Lambda handler below.
让我们观察下面的Lambda处理程序。
The Lambda receives the event parameter, which is a Python dictionary.
Lambda接收event参数,该参数是Python字典。
If at this stage you access the dictionary without checking its validity, for the majority of Lambda invocations you will be fine. However, in some cases, the event dictionary might not have the ‘input’ key or a list, or the list won’t have at least 2 items (we check for index #1). An exception will be raised, and it won’t be caught.
如果在此阶段访问字典时没有检查其有效性,那么对于大多数Lambda调用而言,您会很好的。 但是,在某些情况下,事件字典可能没有'input'键或列表,或者列表中至少没有2个项目(我们检查索引#1)。 将引发异常,并且不会捕获到异常。
The first problem is when an exception isn’t caught in Lambda, AWS triggers Lambda retries by default (5 times by default), which will fail again and again. Since you pay for execution times, this can really add up.
第一个问题是,当Lambda中未捕获异常时,AWS默认会触发Lambda重试(默认为5次),这将一次又一次失败。 由于您需要支付执行时间,因此这确实可以加起来。
The second problem is in cases where an exception isn’t thrown, but the values are invalid. Your program could suffer from “minor” side effects, like invalid program integrity, undefined or invalid behavior bugs and even security issues.
第二个问题是在没有引发异常但值无效的情况下。 您的程序可能会遭受“较小”的副作用,例如无效的程序完整性,未定义或无效的行为错误,甚至是安全问题。
The third problem is that events are updated or changed by services regularly (especially AWS services) and the event dictionary can contain values which your Lambda didn’t expect.
第三个问题是事件会由服务(尤其是AWS服务)定期更新或更改,并且事件字典可能包含Lambda所不希望的值。
Your code will fail, and that’s ok, but it should fail in the “right” way.
您的代码将失败,并且可以,但是应该以“正确”的方式失败。
What if I told you that you can solve all three problems by combining validation and input constraint checks with one simple library?
如果我告诉您可以通过将验证和输入约束检查与一个简单的库相结合来解决所有三个问题,该怎么办?
介绍Pydantic (Introducing Pydantic)
Why Pydantic?
为什么要放屁?
1. Performance. According to Pydantic’s benchmarks, it performs at least 1.4x better than any other JSON schema validation libraries.
1.性能。 根据Pydantic的基准测试 ,它的性能至少比任何其他JSON模式验证库高1.4倍。
2. Once validated, the parsed object is used as a regular data class container.
2.验证后,已解析的对象将用作常规数据类容器。
3. Produces easy to read code, abstracts and hides data class implementation. All you need to do is inherit from BaseModel class.
3.产生易于阅读的代码,抽象并隐藏数据类的实现。 您需要做的就是从BaseModel类继承。
4. Validation errors are comprehensive and let you know exactly what has failed and where.
4.验证错误很全面,可以让您确切地知道失败的原因和失败的原因。
Let’s see Pydantic in action.
让我们看看Pydantic的实际效果。
Let’s assume you are writing a Lambda that receives a music album’s metadata and pushes it to a DynamoDB table. This is an event your new Lambda is expected to receive:
假设您正在编写一个Lambda,它接收音乐专辑的元数据并将其推送到DynamoDB表。 这是您的新Lambda有望收到的事件:
A music record has a title, genre, release date and an artist. An artist has a name and an age. Your Lambda will push this entry into a table, but first, it needs to validate the data. The Pydantic schema file will look like this:
音乐唱片具有标题,类型,发行日期和艺术家。 艺术家有名字和年龄。 您的Lambda会将此项推入表中,但是首先,它需要验证数据。 Pydantic模式文件如下所示:
Let’s go over the basics:
让我们看一下基础知识:
· Each Python dictionary is modeled as a class that extends BaseModel
·每个Python字典都建模为扩展BaseModel的类
· If a field is written in a schema, it’s mandatory and has to appear in the input (unless you define it as Optional)
·如果字段是用模式编写的,则它是必填字段,并且必须出现在输入中(除非您将其定义为Optional)
· Pydantic supports many inherit types, such as all the Python.typing library, uuid, enum, IPv4/6, dates, secrets, http urls and more
·Pydantic支持多种继承的类型,如所有的Python.typing库,UUID,枚举的IPv4 / 6,日期,秘密的HTTP URL和更多
In this case, most parameters are string or int. I also used datetime.date (for release_date), Optional (and a default value in case it’s missing) and typing’s Literal class (genre can only be one of three strings Metal/Rock/Lame)
在这种情况下,大多数参数是字符串或整数。 我还使用了datetime.date(用于release_date),Optional(如果缺少它,则使用默认值)和键入Literal类(类型只能是Metal / Rock / Lame三个字符串之一)
· Notice the validator decorator? It’s a great tool for adding constraint checks on variable data. In this case, I want to validate that the age is larger than 18. If the input age is smaller than 18, a Pydantic ValidationError will be raised and this will be printed: “artist age is invalid (type=value_error)”. Pydantic’s ValidationError exception is very specific and you will know right away what went wrong
·注意验证器的装饰者吗? 这是在变量数据上添加约束检查的好工具。 在这种情况下,我想验证年龄是否大于18。如果输入年龄小于18,则将引发Pydantic ValidationError并将其打印出来:“艺术家年龄无效(类型=值错误)”。 Pydantic的ValidationError异常非常具体,您将立即知道出了什么问题
· There’s also a powerful utility called Root validator. It runs after all class parameters are verified and allows defining strong relationship checks between parameters. You can read more about it here
·还有一个功能强大的实用程序,称为“根验证器”。 它在验证所有类参数之后运行,并允许定义参数之间的严格关系检查。 您可以在此处了解更多信息
This is what your improved Lambda will look like:
这是经过改进的Lambda的外观:
You can access class members after the parsed_event is created (because it passed validation). All exceptions are handled in order to avoid automatic retries.
您可以在创建parsed_event之后访问类成员(因为它通过了验证)。 处理所有异常是为了避免自动重试。
This example is the gold standard for input validation of every Lambda.
此示例是每个Lambda输入验证的黄金标准。
验证? 不只是为了输入 (Validation? Not just for input)
Schema validation can also be applied to boto3 API calls (they return a dictionary).
模式验证也可以应用于boto3 API调用(它们返回字典)。
Some events can be very complex to map.
有些事件的映射可能非常复杂。
The beauty of Pydantic is that you don’t have to map ALL the schema you receive, but only what you actually *use* and care about.
Pydantic的优点在于,您不必映射所收到的所有模式,而只需映射您实际使用和关心的内容即可。
Look at the following boto3 EventBridge call:
查看以下boto3 EventBridge调用 :
And the matching schema:
以及匹配的架构:
Let’s assume you just require the status code from the response. In that case, you can clean up the schema. Pydantic will ignore fields that are not mapped and will only raise a validation error regarding the defined fields.
假设您只需要响应中的状态码。 在这种情况下,您可以清理架构。 Pydantic将忽略未映射的字段,只会引发有关已定义字段的验证错误。
The improved version will look like this:
改进的版本将如下所示:
Your code is now more robust to changes. It will only fail validation if the fields you care about are changed, and won’t be affected by changes in the other fields.
您的代码现在对更改更健壮了。 仅当您关注的字段已更改且不会受到其他字段更改的影响时,它才会通过验证。
总结一下 (Let’s sum it up)
Become a validation hero by following these steps:
遵循以下步骤,成为验证英雄:
· Use schema validation wherever possible: on API inputs and responses
·尽可能使用模式验证:针对API输入和响应
· Add custom validators to validate value constraints
·添加自定义验证器以验证值约束
· Catch ValidationError exceptions and handle them. Don’t let exceptions go unhandled to avoid failing retries
·捕获ValidationError异常并进行处理。 不要让异常无法处理以避免重试失败
From my experience, it is very practical to share schema files and definitions between services and teams as a way to publish Lambda APIs.
根据我的经验,在服务和团队之间共享架构文件和定义作为发布Lambda API的一种方式非常实用。
You can also reduce code duplication by writing validator’s function libraries, which other services and teams can import. For example, phone number validations, AWS region or ARN string validations that are very common.
您还可以通过编写验证器的函数库来减少代码重复,其他服务和团队可以导入该函数库。 例如,非常常见的电话号码验证,AWS区域或ARN字符串验证。
翻译自: https://medium.com/cyberark-engineering/aws-lambda-event-validation-from-zero-to-hero-2ca950acd2ea
什么是游览器兼容性问题原因