简体   繁体   English

SAM模板和Cloudformation模板的区别

[英]Difference between SAM template and Cloudformation template

I'm finding it hard to understand the difference between SAM template and Cloudformation template.我发现很难理解 SAM 模板和 Cloudformation 模板之间的区别。 I know that SAM template can be used to define Serverless Applications like Lambda, but how does that make it different from Cloudformation template?我知道 SAM 模板可用于定义无服务器应用程序,如 Lambda,但这与 Cloudformation 模板有何不同? Is the syntax different?语法不同吗? I can still specify the Lambda definitions in cloudformation template.我仍然可以在 cloudformation 模板中指定 Lambda 定义。 So, my question is why should I care about SAM?所以,我的问题是我为什么要关心 SAM? Won't knowing about just cloud formation template be sufficient?仅仅了解云形成模板还不够吗?

From CloudFormation's perspective, SAM is a transform.从 CloudFormation 的角度来看,SAM 是一种转换。 Meaning: SAM templates are syntactically equivalent, but they allow you to define your serverless app with more brevity.含义:SAM 模板在语法上是等效的,但它们允许您更简洁地定义无服务器应用程序。 The SAM template eventually gets expanded into full CFN behind the scenes. SAM 模板最终在幕后扩展为完整的 CFN。 If you already know CFN, but want to write less YAML code, SAM may be beneficial to you.如果您已经了解 CFN,但想少写一些 YAML 代码,SAM 可能对您有所帮助。 The idea is to reduce your effort.这个想法是减少你的努力。

Like @Luis Colon said, SAM is a transform.就像@Luis Colon 所说,SAM 是一种变换。 What that means, is that at the top of a SAM Template there is a Transform statement that lets CloudFormation know to run an intrinsic function, Transform, on this SAM template to turn it into a CloudFormation template.这意味着,在 SAM 模板的顶部有一个 Transform 语句,它让 CloudFormation 知道在此 SAM 模板上运行内部函数 Transform 以将其转换为 CloudFormation 模板。 So, all SAM Templates will eventually be converted into CF templates, but for the end-user in most cases it is easier to just use the SAM template.因此,所有 SAM 模板最终都会转换为 CF 模板,但对于最终用户而言,在大多数情况下,仅使用 SAM 模板会更容易。 For instance, for a simple application with Lambdas triggered by a new API you're creating, the SAM template will let you accomplish this in fewer lines than CloudFormation.例如,对于包含由您正在创建的新 API 触发的 Lambda 的简单应用程序,SAM 模板将使您能够以比 CloudFormation 更少的行数完成此操作。

To extend this, the Serverless Framework behaves similarly.为了扩展这一点,无服务器框架的行为类似。 Serverless is designed to work across platforms (AWS, Azure, etc.).无服务器旨在跨平台(AWS、Azure 等)工作。 It's syntax looks very similar to SAM, and it too converts the template into the target platform's (ie. AWS) fuller version of the template (ie. CloudFormation template).它的语法看起来与 SAM 非常相似,它也将模板转换为目标平台(即 AWS)的更完整版本的模板(即 CloudFormation 模板)。

SAM templates are a superset of Cloudformation. SAM 模板是 Cloudformation 的超集。 Any Cloudformation template can be run through SAM unchanged, and it will work.任何 Cloudformation 模板都可以通过 SAM 原样运行,并且可以正常工作。 SAM supports all the types available in Cloudformation templates, so you can think of SAM as "CloudFormation++". SAM 支持 Cloudformation 模板中可用的所有类型,因此您可以将 SAM 视为“CloudFormation++”。

However, SAM also gives you additional "transforms" that allow you to define certain concepts succinctly, and SAM will figure out what you mean and fill in the the missing pieces to create a full, expanded, legal Cloudformation template.但是,SAM 还为您提供了额外的“转换”,允许您简洁地定义某些概念,SAM 将弄清楚您的意思并填写缺失的部分以创建完整的、扩展的、合法的 Cloudformation 模板。

Example: For SAM (and Serverless Framework) users, who deal mostly in Lambda functions, one of the more most useful transforms is the Events property on the Lambda function -- SAM will add all the objects needs to access that function through an API path in API Gateway.示例:对于主要处理 Lambda 函数的 SAM(和无服务器框架)用户,最有用的转换之一是 Lambda 函数上的Events属性——SAM 将添加通过 API 路径访问该函数所需的所有对象在 API 网关中。

Resources:
  HelloWorldFunction:
    Type: AWS::Serverless::Function
    Properties:
      CodeUri: HelloWorldFunction
      Handler: app.lambdaHandler
      Runtime: nodejs12.x
      Events:  # <--- "Events" property is not a real Cloudformation Lambda property
        HelloWorld:
          Type: Api
          Properties:
            Path: /hello
            Method: get

The SAM template snippet shown above gets transformed/expanded into several API Gateway objects (a RestApi, a deployment, and a stage).上面显示的 SAM 模板片段被转换/扩展为多个 API 网关对象(一个 RestApi、一个部署和一个阶段)。 The AWS::Serverless::Function type used in this snippet is not a real Cloudformation type -- you won't find it in the docs.此代码段中使用的AWS::Serverless::Function类型不是真正的 Cloudformation 类型——您不会在文档中找到它。 SAM expands it into a Cloudformation template containing a AWS::Lambda::Function object and several different AWS::ApiGateway::* objects that Cloudformation understands. SAM 将其扩展为一个 Cloudformation 模板,其中包含一个AWS::Lambda::Function对象和 Cloudformation 理解的几个不同的AWS::ApiGateway::*对象。

To give you an idea of how much manual coding this saves you, here's what the expanded version of the above SAM template looks like as a full Cloudformation template:为了让您了解这为您节省了多少手动编码,以下是上述 SAM 模板的扩展版本作为完整 Cloudformation 模板的样子:

Resources:
  HelloWorldFunctionHelloWorldPermissionProd:
    Type: AWS::Lambda::Permission
    Properties:
      Action: lambda:InvokeFunction
      Principal: apigateway.amazonaws.com
      FunctionName:
        Ref: HelloWorldFunction
      SourceArn:
        Fn::Sub:
        - arn:aws:execute-api:${AWS::Region}:${AWS::AccountId}:${__ApiId__}/${__Stage__}/GET/hello
        - __Stage__: "*"
          __ApiId__:
            Ref: ServerlessRestApi

  HelloWorldFunctionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Action:
          - sts:AssumeRole
          Effect: Allow
          Principal:
            Service:
            - lambda.amazonaws.com
      ManagedPolicyArns:
      - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
      Tags:
      - Value: SAM
        Key: lambda:createdBy

  ServerlessRestApiProdStage:
    Type: AWS::ApiGateway::Stage
    Properties:
      DeploymentId:
        Ref: ServerlessRestApiDeployment_NNN
      RestApiId:
        Ref: ServerlessRestApi
      StageName: Prod

  ServerlessRestApiDeployment_NNN:
    Type: AWS::ApiGateway::Deployment
    Properties:
      RestApiId:
        Ref: ServerlessRestApi
      Description: 'RestApi deployment id: ???'
      StageName: Stage

  ServerlessRestApi:
    Type: AWS::ApiGateway::RestApi
    Properties:
      Body:
        info:
          version: '1.0'
          title:
            Ref: AWS::StackName
        paths:
          "/hello":
            get:
              x-amazon-apigateway-integration:
                httpMethod: POST
                type: aws_proxy
                uri:
                  Fn::Sub: arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${HelloWorldFunction.Arn}/invocations
              responses: {}
        swagger: '2.0'

  HelloWorldFunction:
    Type: AWS::Lambda::Function
    Properties:
      Code:
        S3Bucket: aws-sam-cli-managed-default-samclisourcebucket-???
        S3Key: temp/???
      Tags:
      - Value: SAM
        Key: lambda:createdBy
      Handler: app.lambdaHandler
      Role:
        Fn::GetAtt:
        - HelloWorldFunctionRole
        - Arn
      Timeout: 3
      Runtime: nodejs12.x

Previously, if you were authoring pure Cloudformation, you would have had to code all this by hand, over and over, for each API Gateway endpoint that you wanted to create.以前,如果您正在创作纯 Cloudformation,则必须为要创建的每个 API 网关端点一遍又一遍地手动编写所有这些代码。 Now, with a SAM template, you define the API as an "Event" property of the Lambda function, and SAM (or Serverless Framework) takes care of the drudgery.现在,使用 SAM 模板,您将 API 定义为 Lambda 函数的“事件”属性,SAM(或无服务器框架)会处理这些苦差事。

In the old days, when we had to do all this by hand, it totally sucked.在过去,当我们不得不手工完成所有这些工作时,它完全糟糕透了。 But now, everything is glorious again.但现在,一切又恢复了光彩。

You can imagine SAM as an extended form of CloudFormation.您可以将 SAM 想象成 CloudFormation 的扩展形式。 SAM makes Serverless/Lambda deployments easier. SAM 使无服务器/Lambda 部署更加容易。
Even CloudFormation can deploy lambda scripts using inline scripts but it has a limitation of 4096 characters and you cannot pack custom dependencies, python libraries.即使 CloudFormation 也可以使用内联脚本部署 lambda 个脚本,但它有 4096 个字符的限制,并且您不能打包自定义依赖项,python 个库。

So to make Lambda/Serverless deployments easy SAM is used.因此,为了使 Lambda/Serverless 部署变得简单,我们使用了 SAM。 SAM is a CLI tool. SAM 是一个 CLI 工具。 You cannot find SAM in AWS Console.您在 AWS 控制台中找不到 SAM。
In case of python deployment, sam will read the requirements.txt file build a package, and will deploy the package when you wish to sam deploy在 python 部署的情况下,sam 将读取requirements.txt文件构建 package,并在您希望sam deploy时部署 package
So at the end of the day you can write as much lengthy Lambda Code, use as many libraries you want and even import your custom libraries ie complete flexibility.所以在一天结束时,您可以编写尽可能多的冗长 Lambda 代码,使用您想要的尽可能多的库,甚至可以导入您的自定义库,即完全的灵活性。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM