简体   繁体   English

无服务器框架、yaml 和 AWS lambda/DynamoDB 概念

[英]Serverless framework, yaml and AWS lambda/DynamoDB concepts

I recently explored serverless and I would like to create a "kind of simple" backend for my app.我最近探索了无服务器,我想为我的应用程序创建一个“一种简单的”后端。 It should like CRUD connected to DynamoDB like it is nicely shown here .它应该像连接到DynamoDBCRUD一样, 这里很好地展示了它。 And then later converted into logging users with Cognito.然后用 Cognito 转换为登录用户。 Link1 and link2 ( 链接 1链接 2 (

During my implementation I have got into a few issue and would like to ask you for help because I don't like using something I only copied and don't exactly know how it is working:在我的实施过程中,我遇到了一些问题并想向您寻求帮助,因为我不喜欢使用我只复制并且不完全知道它是如何工作的东西:

  1. Using callbacks vs returns in lambda functions Please can you describe the difference between these two?在 lambda 函数中使用回调与返回请您描述这两者之间的区别吗? I have found some resources on this, however, they won't clear it for me.我在这方面找到了一些资源,但是,他们不会为我清除它。 On Amazon Docs and here Stack overflow .Amazon Docs此处 Stack Overflow上。

  2. Why I don't need to set CORS (headers) when initializing lambdas with serverless as opposed to lambda functions created in the amazon console (like here )?为什么在使用无服务器初始化 lambda 时不需要设置CORS(标头) ,而不是在亚马逊控制台中创建的 lambda 函数(如此)?

  3. In serverless frameworks what is the difference between functions.events.http and functions.events.httpapi ?在无服务器框架中functions.events.httpfunctions.events.httpapi有什么区别?

  4. Serverless.yml Is there any guide to YAML? Serverless.yml有没有 YAML 的指南? I understand that Amazon writing user guides and templates like AWS::IAM::Role or for Dynamo Tables and all others.我了解亚马逊编写用户指南和模板,例如AWS::IAM::Role或用于Dynamo Tables和所有其他人。 And also that resources make a cloudformation but what is the purpose for example for Lambda functions listing there when I create/code them?而且这些资源也形成了云,但是当我创建/编码它们时,Lambda 函数的目的是什么? To reference them?引用它们? Add them privileges?添加他们的权限?

Also how the referencing works there?还有参考是如何在那里工作的? Do I only specify resources that should be created or can I also reference already existing ones via ARN?我是只指定应该创建的资源,还是可以通过 ARN 引用已经存在的资源? If so how?如果有怎么办?

Do the policies are only set globally like documentation showing or the only other option is to use plugin serverless-iam-roles-per-function ?策略是否仅像显示的文档一样全局设置,或者唯一的其他选择是使用插件serverless-iam-roles-per-function The same question could be applied to roles and policies in resources.同样的问题也可以应用于资源中的角色和策略。

I am just overwhelmed with the documentation and all the different guides and sometimes can't find the right answers.我只是对文档和所有不同的指南感到不知所措,有时找不到正确的答案。 I appreciate any answers to any of my questions: I like serverless functions and see huge potential in it and therefore want to get more knowledge about it and start using it more :)我很感激我的任何问题的任何答案:我喜欢无服务器功能并看到它的巨大潜力,因此希望获得更多关于它的知识并开始更多地使用它:)

Cheers and thanks in advance Lukas!提前干杯和感谢卢卡斯!

There's a lot here, but I'll take a shot at addressing these questions at a high level.这里有很多内容,但我将尝试从高层次上解决这些问题。 If you have follow-up questions or want a more in-depth answer, I'd suggest posting a different StackOverflow question that focuses on the specifics.如果您有后续问题或想要更深入的答案,我建议您发布一个关注细节的不同 StackOverflow 问题。 You are more likely to get more community participation if your question is focused and concise.如果您的问题集中且简洁,您更有可能获得更多的社区参与。

Using callbacks vs returns in lambda functions Please can you describe the difference between these two?在 lambda 函数中使用回调与返回 请您描述这两者之间的区别吗? I have found some resources on this, however, they won't clear it for me.我在这方面找到了一些资源,但是,他们不会为我清除它。 On Amazon Docs and here Stack overflow.在 Amazon Docs 和这里 Stack Overflow。

The difference boils down to asynchronous vs synchronous handlers.差异归结为异步与同步处理程序。 I don't think I can explain the difference any better than the resources that you've linked.我认为我无法比您链接的资源更好地解释差异。 However, I will say that it comes down to personal preference.但是,我会说这取决于个人喜好。

You can implement your lambdas with either approach, it's up to your personal preference.您可以使用任何一种方法来实现您的 lambda,这取决于您的个人喜好。 Do you like managing callbacks or do you prefer to use async/await?你喜欢管理回调还是更喜欢使用 async/await?

Why I don't need to set CORS (headers) when initializing lambdas with serverless as opposed to lambda functions created in the amazon console (like here)?为什么在使用无服务器初始化 lambda 时不需要设置 CORS(标头),而不是在亚马逊控制台中创建的 lambda 函数(如此处)?

The serverless-stack demo puts the CORS response neatly into a helper method.无服务器堆栈演示将 CORS 响应巧妙地放入辅助方法中。 It looks like you're using it here .看起来你在这里使用它 When you call this method in your lambdas, you are setting the CORS headers.当您在 lambdas 中调用此方法时,您正在设置 CORS 标头。

In serverless frameworks what is the difference between functions.events.http and functions.events.httpapi?在无服务器框架中,functions.events.http 和 functions.events.httpapi 之间有什么区别?

functions.events.http refers to API Gateway's REST API offering (APIG v1). functions.events.http是指 API 网关的 REST ZDB974238714CA8DE634A7CE1GDF18 产品。 functions.events.httpapi refers to API Gateways HTTP API offering (APIG v2). functions.events.httpapi指 API 网关 HTTP API 产品(APIG v2)。

You can read about the differences between REST API and HTTP API offerings here .您可以在此处阅读REST API 和 HTTP ZDB974238714CA8DE6434A7CE1DZ083 产品之间的差异。

Serverless.yml Is there any guide to YAML? Serverless.yml 有没有 YAML 的指南? I understand that Amazon writing user guides and templates like AWS::IAM::Role or for Dynamo Tables and all others.我了解亚马逊编写用户指南和模板,例如 AWS::IAM::Role 或用于 Dynamo Tables 和所有其他人。 And also that resources make a cloudformation but what is the purpose for example for Lambda functions listing there when I create/code them?而且这些资源也形成了云,但是当我创建/编码它们时,Lambda 函数的目的是什么? To reference them?引用它们? Add them privileges?添加他们的权限?

The Serverless Framework docs have a guide to the serverless.yml options when you set the provider to aws .当您将提供程序设置为aws时,无服务器框架文档提供了有关 serverless.yml 选项的指南 The serverless.yml file is an abstraction over Cloudformation. serverless.yml 文件是对 Cloudformation 的抽象。 The idea is that serverless.yml is easier to write than verbose Cloudformation.这个想法是 serverless.yml 比冗长的 Cloudformation 更容易编写。 You'd define functions in your serverless.yml to create/configure your lambdas in AWS.您将在 serverless.yml 中定义函数以在 AWS 中创建/配置您的 lambda。 For example, here's a snippet from your serverless.yml例如,这是您的 serverless.yml 中的一个片段

functions:
  login:
    handler: login.login
    name: my-login-function
    memorySize: 128
    description: Function that log user.
    events:
    - httpApi: 
        path: /login
        method: post

This creates a lambda in AWS with the code you have written in the login.js file.这将使用您在 login.js 文件中编写的代码在 AWS 中创建 lambda。 It also sets the memory size and sets up an API Gateway HTTP API endpoint behind /login.它还设置 memory 大小并设置 API 网关 HTTP ZDB974238714CA8DE634A7CE1DZ03 端点后面。 You can check out the Cloudformation template that is created from this serverless.yml file in your .serverless directory.您可以查看从.serverless目录中的这个 serverless.yml 文件创建的 Cloudformation 模板。 It's....a lot.这是……很多。

Do I only specify resources that should be created or can I also reference already existing ones via ARN?我是只指定应该创建的资源,还是可以通过 ARN 引用已经存在的资源? If so how?如果有怎么办?

The serverless docs give an example of how to point to existing API endpoints. 无服务器文档给出了如何指向现有 API 端点的示例。

 apiGateway: # Optional API Gateway global config
    restApiResources: # List of existing resources that were created in the REST API. This is required or the stack will be conflicted
      '/users': xxxxxxxxxx
      '/users/create': xxxxxxxxxx

Do the policies are only set globally like documentation showing or the only other option is to use plugin serverless-iam-roles-per-function?策略是否仅像显示的文档一样全局设置,或者唯一的其他选择是使用插件 serverless-iam-roles-per-function? The same question could be applied to roles and policies in resources.同样的问题也可以应用于资源中的角色和策略。

I'm not sure I understand the question.我不确定我是否理解这个问题。 In general, I'd suggest spending some time reading What is IAM?一般来说,我建议花一些时间阅读什么是 IAM? from the serverless-stack website.来自无服务器堆栈网站。 It does a fantastic job of covered IAM Users/Roles/Policies/Groups/etc.它在涵盖 IAM 用户/角色/策略/组/等方面做得非常出色。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在使用无服务器框架时从AWS Lambda访问DynamoDB? - How to access DynamoDB from AWS Lambda when using the Serverless Framework? 适用于 Lambda 无服务器架构的 AWS DynamoDB 与 RDS - AWS DynamoDB vs RDS for Lambda serverless architecture 无服务器框架+ AWS + Lambda + DynamoDB + GraphQL + Apollo服务器=无法使POST请求生效 - Serverless framework + AWS + Lambda + DynamoDB + GraphQL + Apollo Server = Can't make POST Request Work 在其他AWS账户中查询Dynamodb-无服务器框架 - Querying Dynamodb in a different AWS account - Serverless Framework 使用无服务器框架将库/依赖项注入 AWS Lambda - Injecting libraries/dependencies into AWS Lambda with Serverless Framework 无服务器框架Facebook Bot Slow(AWS Lambda) - Serverless Framework Facebook Bot Slow (AWS Lambda) 无服务器框架-AWS Lambda依赖项限制超出 - Serverless Framework - AWS Lambda dependency limit exceed AWS Lambda function on Java with Serverless framework and GraalVM - AWS Lambda function on Java with Serverless framework and GraalVM 使用无服务器框架在 lambda AWS 中授予授权代码 - Authorization code grant in lambda AWS with Serverless Framework 无服务器-与RethinkDB + AWS Lambda相比DynamoDB(糟糕)的性能 - Serverless - DynamoDB (terrible) performances compared to RethinkDB + AWS Lambda
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM