Serverless Framework:如何实现完整 "infrastructure as code"?
Serverless Framework: ways to achieve full "infrastructure as code"?
我遵循了出色的指南(Serverless Stack) that creates a typical CRUD serverless infrastructure with a react frontend. It's using the Serverless Framework for AWS。
我不喜欢的是 bootstrap 设置涉及大量手动单击 GUI(主要是亚马逊的控制台界面)。 IE。该设置不受版本控制,并且不容易重现。用 CI/CD 进程等扩展它并不容易。在这个例子中,需要手动设置以下资源:
- AWS Cognito 用户池
- AWS Cognite 用户池应用程序
- AWS Cognito 联合身份池
- AWS DynamoDB 实例
- AWS S3 存储桶 (x3)(这也托管 frontend)
- AWS CloudFront 分布
- AWS Route53 区域文件
从代码构建的唯一资源是无服务器函数 (lambda) 本身,以及 API 网关实例。这是无服务器框架使用其 serverless.yml file. But all of the above resources are not automatically created. They sometimes need to be referenced to 使用其 ARN 所做的,但它们不是由 serverless.yml 配置创建的。 运行 这样的生产系统(严重依赖于通过 GUI 手动创建服务)似乎有风险。
我在想解决这个问题的方法是使用 Terraform 或 Cloudformation。但是无服务器框架本身已经在使用 Cloudformation 来设置 Lambdas,尽管没有用于其他资源。那么如何消除这一差距呢?换句话说,如何用代码重建 Serverless Stack 中描述的整个设置?
让 CloudFormation 设置 Serverless 似乎很奇怪,也许是不可能的,然后它有自己的 Cloudformation 模板来设置 lambda。扩展无服务器框架可能更有意义,不仅可以定义需要在 serverless deploy
上创建的函数和 API 网关,还可以定义其他资源,如 DynamoDB 或 Cognito 用户池。是否已经有任何人这样做的例子或尝试?
我同意这方面的文档会成为一个很好的 pull request here。
您是正确的,serverless
正在后台使用 CloudFormation。该框架确实通过 serverless.yml
.
的 resources
键向您公开了底层的 CloudFormation 机制。
我觉得the intent of the framework is that you would put the rest of these resources (Cognito stuff, S3, etc.) in the resources:
section of your serverless.yml
file, using regular old CloudFormation syntax.
例如,此文件将创建 DynamoDB table 和 S3 存储桶,此外还有无服务器功能:
service: aws-nodejs # NOTE: update this with your service name
provider:
name: aws
runtime: nodejs6.10
functions:
hello:
handler: handler.deletecustomer
events:
- http:
path: /deletecustomer
method: post
cors: true
resources:
Resources:
tablenotes:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: noteId
AttributeType: S
- AttributeName: userId
AttributeType: S
KeySchema:
- AttributeName: userId
KeyType: HASH
- AttributeName: noteId
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: '5'
WriteCapacityUnits: '5'
mysamplebucket:
Type: AWS::S3::Bucket
Properties:
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: error.html
AccessControl: Private
VersioningConfiguration:
Status: Suspended
如果您是 CloudFormation 的新手,我还建议您看一下 CloudFormer。
基于@Mike Patrick 的选项,添加我对无服务器框架和其他类似无服务器焦点工具的理解。
正如您所说,对于 Serverless 项目,涉及的资源非常多。将它们组合在一起并非易事。所以选择一个合适的工具很难。
比较 Serverless framework
与 Cloudformation
和 Terraform
,无服务器框架是 无服务器专家,Cloudformation 和 Terraform 是 GP
Cloudformation 和 terraform 完全Infrastructure as Code
,涵盖了大部分资源。
Serverless 框架是一个中间层,仅用于生成 Cloudformation 模板,该模板主要仅用于与 Serverless 相关的资源。
可以直接写在Cloudformation模板里,但是模板文件会很大,用它的JSON/Yaml模板很难维护。 serverless.yml
几十行,serverless框架可以生成千行甚至几千行的cloudformation模板。节省了很多处理cloudformation代码的时间。
让无服务器框架处理所有 AWS 资源没有意义,其他工具已经做得很好。
Serverless 框架仍在开发中,因为它的流行,每天都有许多开发人员参与到其中添加功能。也许有一天你可以得到你需要的东西,但现在你必须在某些情况下将无服务器框架与 Cloudformation 或 Terraform 或其他工具混合在一起。
您肯定已经可以使用各种部署工具将几乎所有内容部署为 IaC(事实上,我们每天都在工作)。
如果您碰巧主要使用无服务器;,那么您可以选择像无服务器框架 (SF) 这样的东西来抽象一些使用 CloudFormation (CF) 时固有的 complexity/inflexibility。无论 CF 能做什么,SF 都能做,但 SF 有一个插件系统,允许 运行 代码调用 API(例如,它可以允许您创建 CF 不支持的资源)。
我遵循了出色的指南(Serverless Stack) that creates a typical CRUD serverless infrastructure with a react frontend. It's using the Serverless Framework for AWS。
我不喜欢的是 bootstrap 设置涉及大量手动单击 GUI(主要是亚马逊的控制台界面)。 IE。该设置不受版本控制,并且不容易重现。用 CI/CD 进程等扩展它并不容易。在这个例子中,需要手动设置以下资源:
- AWS Cognito 用户池
- AWS Cognite 用户池应用程序
- AWS Cognito 联合身份池
- AWS DynamoDB 实例
- AWS S3 存储桶 (x3)(这也托管 frontend)
- AWS CloudFront 分布
- AWS Route53 区域文件
从代码构建的唯一资源是无服务器函数 (lambda) 本身,以及 API 网关实例。这是无服务器框架使用其 serverless.yml file. But all of the above resources are not automatically created. They sometimes need to be referenced to 使用其 ARN 所做的,但它们不是由 serverless.yml 配置创建的。 运行 这样的生产系统(严重依赖于通过 GUI 手动创建服务)似乎有风险。
我在想解决这个问题的方法是使用 Terraform 或 Cloudformation。但是无服务器框架本身已经在使用 Cloudformation 来设置 Lambdas,尽管没有用于其他资源。那么如何消除这一差距呢?换句话说,如何用代码重建 Serverless Stack 中描述的整个设置?
让 CloudFormation 设置 Serverless 似乎很奇怪,也许是不可能的,然后它有自己的 Cloudformation 模板来设置 lambda。扩展无服务器框架可能更有意义,不仅可以定义需要在 serverless deploy
上创建的函数和 API 网关,还可以定义其他资源,如 DynamoDB 或 Cognito 用户池。是否已经有任何人这样做的例子或尝试?
我同意这方面的文档会成为一个很好的 pull request here。
您是正确的,serverless
正在后台使用 CloudFormation。该框架确实通过 serverless.yml
.
resources
键向您公开了底层的 CloudFormation 机制。
我觉得the intent of the framework is that you would put the rest of these resources (Cognito stuff, S3, etc.) in the resources:
section of your serverless.yml
file, using regular old CloudFormation syntax.
例如,此文件将创建 DynamoDB table 和 S3 存储桶,此外还有无服务器功能:
service: aws-nodejs # NOTE: update this with your service name
provider:
name: aws
runtime: nodejs6.10
functions:
hello:
handler: handler.deletecustomer
events:
- http:
path: /deletecustomer
method: post
cors: true
resources:
Resources:
tablenotes:
Type: AWS::DynamoDB::Table
Properties:
AttributeDefinitions:
- AttributeName: noteId
AttributeType: S
- AttributeName: userId
AttributeType: S
KeySchema:
- AttributeName: userId
KeyType: HASH
- AttributeName: noteId
KeyType: RANGE
ProvisionedThroughput:
ReadCapacityUnits: '5'
WriteCapacityUnits: '5'
mysamplebucket:
Type: AWS::S3::Bucket
Properties:
WebsiteConfiguration:
IndexDocument: index.html
ErrorDocument: error.html
AccessControl: Private
VersioningConfiguration:
Status: Suspended
如果您是 CloudFormation 的新手,我还建议您看一下 CloudFormer。
基于@Mike Patrick 的选项,添加我对无服务器框架和其他类似无服务器焦点工具的理解。
正如您所说,对于 Serverless 项目,涉及的资源非常多。将它们组合在一起并非易事。所以选择一个合适的工具很难。
比较 Serverless framework
与 Cloudformation
和 Terraform
,无服务器框架是 无服务器专家,Cloudformation 和 Terraform 是 GP
Cloudformation 和 terraform 完全Infrastructure as Code
,涵盖了大部分资源。
Serverless 框架是一个中间层,仅用于生成 Cloudformation 模板,该模板主要仅用于与 Serverless 相关的资源。
可以直接写在Cloudformation模板里,但是模板文件会很大,用它的JSON/Yaml模板很难维护。 serverless.yml
几十行,serverless框架可以生成千行甚至几千行的cloudformation模板。节省了很多处理cloudformation代码的时间。
让无服务器框架处理所有 AWS 资源没有意义,其他工具已经做得很好。
Serverless 框架仍在开发中,因为它的流行,每天都有许多开发人员参与到其中添加功能。也许有一天你可以得到你需要的东西,但现在你必须在某些情况下将无服务器框架与 Cloudformation 或 Terraform 或其他工具混合在一起。
您肯定已经可以使用各种部署工具将几乎所有内容部署为 IaC(事实上,我们每天都在工作)。
如果您碰巧主要使用无服务器;,那么您可以选择像无服务器框架 (SF) 这样的东西来抽象一些使用 CloudFormation (CF) 时固有的 complexity/inflexibility。无论 CF 能做什么,SF 都能做,但 SF 有一个插件系统,允许 运行 代码调用 API(例如,它可以允许您创建 CF 不支持的资源)。