创建您的 AWS SAM 模板
创建 AWS SAM 模板文件,用于指定基础设施中的组件。
创建 AWS SAM 模板
-
创建一个名为 的目录。
SAM-Tutorial
. -
在您的
SAM-Tutorial
目录,创建名为template.yml
. -
将以下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: nodejs10.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: nodejs10.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: nodejs10.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 模板概念.
- A Lambda 调用功能
myDateTimeFunction
-
这时 Lambda 功能已发布,
AutoPublishAlias
模板中的线路将其链接到名称的别名live
...在本教程的稍后中,此函数的更新将触发部署 AWS CodeDeploy 从原始版本到更新版本的递增将生产流量递增到最新版本。 - 两个 Lambda 部署验证函数
-
以下内容 Lambda 功能在期间执行 CodeDeploy 生命周期挂钩。功能包含验证已更新部署的代码
myDateTimeFunction
...验证测试的结果传递到 CodeDeploy 使用ITSITSPutLifecycleEventHookExecutionStatus
API方法。如果验证测试失败,则部署失败并回滚。-
CodeDeployHook_beforeAllowTraffic
在BeforeAllowTraffic
挂钩期间运行。 -
CodeDeployHook_afterAllowTraffic
在AfterAllowTraffic
挂钩期间运行。
两个函数的名称开始于
CodeDeployHook_
...TheThetheCodeDeployRoleForLambda
角色允许呼叫 Lambdainvoke
方法仅在 Lambda 使用此前缀开头的名称的函数。有关详细信息,请参阅 AppSpec 部署的 AWS Lambda“hooks”部分 和 百分比生命周期类似状态 在 CodeDeploy API参考. -
- 自动检测更新的 Lambda 函数
-
TheThethe
AutoPublishAlias
术语告诉框架,以检测myDateTimeFunction
功能更改,然后使用live
别名。 - 部署配置
-
部署配置确定您的 CodeDeploy 应用程序将流量从 Lambda 新版本的功能。此模板指定预定义的部署配置
Linear10PercentEvery1Minute
.注意 在 AWS SAM 模板中无法指定自定义部署配置。有关更多信息,请参阅 Create a Deployment Configuration.
- 部署生命周期挂钩函数
-
TheThethe
Hooks
部分指定在生命周期事件挂钩期间运行的函数。PreTraffic
指定在BeforeAllowTraffic
钩。PostTraffic
指定在AfterAllowTraffic
钩。 - 权限 Lambda 要调用另一个 Lambda 功能
-
指定
lambda:InvokeFunction
权限授予 AWS SAM 调用A的应用程序权限 Lambda 功能。这是在这种情况下CodeDeployHook_beforeAllowTraffic
和CodeDeployHook_afterAllowTraffic
功能调用部署 Lambda 验证测试期间的功能。