

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 创建你的 AWS SAM 模板
<a name="tutorial-lambda-sam-template"></a>

创建一个 AWS SAM 模板文件来指定基础架构中的组件。

**创建 AWS SAM 模板**

1.  创建一个名为 `SAM-Tutorial` 的目录。

1.  在 `SAM-Tutorial` 目录中创建名为 `template.yml` 的文件。

1.  将以下 YAML 代码复制到 `template.yml` 中。这是 AWS SAM 模板。

   ```
   AWSTemplateFormatVersion : '2010-09-09'
   Transform: AWS::Serverless-2016-10-31
   Description: A sample SAM template for deploying Lambda functions.
   
   Resources:
   # Details about the myDateTimeFunction Lambda function
     myDateTimeFunction:
       Type: AWS::Serverless::Function
       Properties:
         Handler: myDateTimeFunction.handler
         Runtime: nodejs18.x
   # Instructs your myDateTimeFunction is published to an alias named "live".      
         AutoPublishAlias: live
   # Grants this function permission to call lambda:InvokeFunction
         Policies:
           - Version: "2012-10-17"		 	 	 
             Statement: 
             - Effect: "Allow"
               Action: 
                 - "lambda:InvokeFunction"
               Resource: '*'
         DeploymentPreference:
   # Specifies the deployment configuration      
             Type: Linear10PercentEvery1Minute
   # Specifies Lambda functions for deployment lifecycle hooks
             Hooks:
               PreTraffic: !Ref beforeAllowTraffic
               PostTraffic: !Ref afterAllowTraffic
               
   # Specifies the BeforeAllowTraffic lifecycle hook Lambda function
     beforeAllowTraffic:
       Type: AWS::Serverless::Function
       Properties:
         Handler: beforeAllowTraffic.handler
         Policies:
           - Version: "2012-10-17"		 	 	 
   # Grants this function permission to call codedeploy:PutLifecycleEventHookExecutionStatus        
             Statement: 
             - Effect: "Allow"
               Action: 
                 - "codedeploy:PutLifecycleEventHookExecutionStatus"
               Resource:
                 !Sub 'arn:aws:codedeploy:${AWS::Region}:${AWS::AccountId}:deploymentgroup:${ServerlessDeploymentApplication}/*'
           - Version: "2012-10-17"		 	 	 
   # Grants this function permission to call lambda:InvokeFunction        
             Statement: 
             - Effect: "Allow"
               Action: 
                 - "lambda:InvokeFunction"
               Resource: !Ref myDateTimeFunction.Version
         Runtime: nodejs18.x
   # Specifies the name of the Lambda hook function      
         FunctionName: 'CodeDeployHook_beforeAllowTraffic'
         DeploymentPreference:
           Enabled: false
         Timeout: 5
         Environment:
           Variables:
             NewVersion: !Ref myDateTimeFunction.Version
             
   # Specifies the AfterAllowTraffic lifecycle hook Lambda function
     afterAllowTraffic:
       Type: AWS::Serverless::Function
       Properties:
         Handler: afterAllowTraffic.handler
         Policies:
           - Version: "2012-10-17"		 	 	 
             Statement: 
   # Grants this function permission to call codedeploy:PutLifecycleEventHookExecutionStatus         
             - Effect: "Allow"
               Action: 
                 - "codedeploy:PutLifecycleEventHookExecutionStatus"
               Resource:
                 !Sub 'arn:aws:codedeploy:${AWS::Region}:${AWS::AccountId}:deploymentgroup:${ServerlessDeploymentApplication}/*'
           - Version: "2012-10-17"		 	 	 
             Statement: 
   # Grants this function permission to call lambda:InvokeFunction          
             - Effect: "Allow"
               Action: 
                 - "lambda:InvokeFunction"
               Resource: !Ref myDateTimeFunction.Version
         Runtime: nodejs18.x
   # Specifies the name of the Lambda hook function      
         FunctionName: 'CodeDeployHook_afterAllowTraffic'
         DeploymentPreference:
           Enabled: false
         Timeout: 5
         Environment:
           Variables:
             NewVersion: !Ref myDateTimeFunction.Version
   ```

此模板指定以下内容。有关更多信息，请参阅 [AWS SAM 模板概念](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-template-basics.html)。

**一个名为 `myDateTimeFunction` 的 Lambda 函数**  
 发布此 Lambda 函数时，模板中的 `AutoPublishAlias` 行将其链接到名为 `live` 的别名。在本教程的后面部分，此函数的更新会触发部署 AWS CodeDeploy ，从而逐步将生产流量从原始版本转移到更新的版本。

**两个 Lambda 部署验证函数**  
 以下 Lambda 函数是在 CodeDeploy 生命周期挂钩期间执行的。该函数包含代码，用于验证更新的 `myDateTimeFunction` 的部署。验证测试的结果通过 `PutLifecycleEventHookExecutionStatus` API 方法传递给 CodeDeploy 。如果验证测试失败，则部署失败并回滚。  
+  `CodeDeployHook_beforeAllowTraffic` 在 `BeforeAllowTraffic` 挂钩期间运行。
+  `CodeDeployHook_afterAllowTraffic` 在 `AfterAllowTraffic` 挂钩期间运行。
这两个函数的名称以 `CodeDeployHook_` 开头。`CodeDeployRoleForLambda` 角色仅允许在 Lambda 函数中，采用以此前缀开头的名称调用 Lambda `invoke` 方法。有关更多信息，请参阅《*CodeDeploy API 参考*》中的[AppSpec AWS Lambda 部署的 “挂钩” 部分](reference-appspec-file-structure-hooks.md#appspec-hooks-lambda)和[PutLifecycleEventHookExecutionStatus](https://docs.aws.amazon.com/codedeploy/latest/APIReference/API_PutLifecycleEventHookExecutionStatus.html)。

**自动检测更新的 Lambda 函数**  
 `AutoPublishAlias` 术语指示框架检测 `myDateTimeFunction` 函数何时发生了变化，然后使用 `live` 别名进行部署。

**部署配置**  
 部署配置决定了您的 CodeDeploy应用程序将流量从原始版本的 Lambda 函数转移到新版本的速率。此模板指定预定义的部署配置 `Linear10PercentEvery1Minute`。  
 您无法在 SA AWS M 模板中指定自定义部署配置。有关更多信息，请参阅 [使用创建部署配置 CodeDeploy](deployment-configurations-create.md)。

**部署生命周期挂钩函数**  
 `Hooks` 部分指定在生命周期事件挂钩期间运行的函数。`PreTraffic` 指定在 `BeforeAllowTraffic` 挂钩期间运行的函数。`PostTraffic` 指定在 `AfterAllowTraffic` 挂钩期间运行的函数。

**Lambda 调用另一个 Lambda 函数的权限**  
 指定的`lambda:InvokeFunction`权限授予 AWS SAM 应用程序使用的角色调用 Lambda 函数的权限。当 `CodeDeployHook_beforeAllowTraffic` 和 `CodeDeployHook_afterAllowTraffic` 函数在验证测试期间调用部署的 Lambda 函数时，必须具备该权限。