本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
使用 sam sync
Amazon Serverless Application Model命令行界面 (Amazon SAMCLI) sam sync
命令提供了用于将本地应用程序更改快速同步到的选项Amazon Web Services 云。sam sync
在开发应用程序时使用:
-
自动检测本地更改并将其同步到Amazon Web Services 云。
-
自定义要同步到的本地更改。Amazon Web Services 云
-
在云端为应用程序做好测试和验证准备。
借sam sync
助,您可以创建快速开发工作流程,缩短将本地更改同步到云端进行测试和验证所需的时间。
注意
建议在开发环境中使用该sam sync
命令。对于生产环境,我们建议使用sam deploy
或配置持续集成和交付 (CI/CD) 管道。要了解更多信息,请参阅 部署无服务器应用程序。
该sam sync
命令是其中的一部分Amazon SAMAccelerate。 Amazon SAMAccelerate提供了一些工具,您可以使用这些工具来加快开发和测试中无服务器应用程序的Amazon Web Services 云体验。
主题
自动检测本地更改并将其同步到 Amazon Web Services 云
运行sam sync
时--watch
选择开始将您的应用程序同步到。Amazon Web Services 云这会执行以下操作:
-
构建应用程序-此过程与使用
sam build
命令类似。 -
部署您的应用程序-Amazon CloudFormation 使用您的默认设置将您的应用程序Amazon SAMCLI部署到。使用以下默认值:
-
Amazon可在您的
.aws
用户文件夹中找到凭据和常规配置设置。 -
应用程序部署设置可在应用程序
samconfig.toml
的文件中找到。
如果找不到默认值,则Amazon SAMCLI会通知您并退出同步过程。
-
-
注意本地更改 — Amazon SAM CLI 保持运行状态,并监视应用程序的本地更改。这就是该
--watch
选项所提供的。默认情况下,此选项可能处于开启状态。有关默认值,请参阅应用程序的
samconfig.toml
文件。以下是 文件示例:... [default.sync] [default.sync.parameters] watch = true ...
-
将@@ 本地更改同步到 Amazon Web Services 云 — 当您进行本地更改时,会Amazon Web Services 云通过最快的可用方法Amazon SAMCLI检测这些更改并将其同步到。根据变更的类型,可能会出现以下情况:
-
如果您更新的资源支持Amazon服务 API,则Amazon SAMCLI会使用它来部署您的更改。这会导致快速同步以更新您的资源Amazon Web Services 云。
-
如果您更新的资源不支持Amazon服务 API,则Amazon SAMCLI会执行Amazon CloudFormation部署。这会更新您的整个应用程序Amazon Web Services 云。虽然速度不快,但它确实使您不必手动启动部署。
-
由于该sam sync
命令会自动更新您的应用程序Amazon Web Services 云,因此建议仅在开发环境中使用该命令。当你跑步时sam sync
,系统会要求你确认:
**The sync command should only be used against a development stack**.
Confirm that you are synchronizing a development stack.
Enter Y to proceed with the command, or enter N to cancel:
[Y/n]: ENTER
自定义将哪些本地更改同步到 Amazon Web Services 云
提供选项以自定义将哪些本地更改同步到中。Amazon Web Services 云这可以加快在云中查看本地更改以进行测试和验证所需的时间。
例如,提供仅同步代码更改(例如Amazon Lambda函数代码)的--code
选项。在开发过程中,如果您特别关注 Lambda 代码,这会将您的更改快速发送到云端进行测试和验证。以下是示例:
$
sam sync --code --watch
要仅同步特定 Lambda 函数或层的代码更改,请使用选项。--resource-id
以下是示例:
$
sam sync --code --resource-id
HelloWorldFunction
--resource-idHelloWorldLayer
为云端应用程序做好测试和验证准备
该sam sync
命令会自动在中找到可用来更新应用程序的最快方法。Amazon Web Services 云这可以加快您的开发和云测试工作流程。通过利用Amazon服务 API,您可以快速开发、同步和测试支持的资源。有关动手示例,请参阅完整Amazon SAM研讨会中的模块 6-Amazon SAM 加速
sam sync 命令的选项
以下是可用于修改sam sync
命令的一些主要选项。有关所有选项的列表,请参阅sam sync。
执行一次性Amazon CloudFormation部署
使用该--no-watch
选项关闭自动同步。以下是示例:
$
sam sync --no-watch
Amazon SAMCLI将执行一次性Amazon CloudFormation部署。此命令将sam build
和sam deploy
命令执行的操作组合在一起。
跳过初始Amazon CloudFormation部署
您可以自定义每次运行时sam sync
是否需要Amazon CloudFormation部署。
-
规定
--no-skip-deploy-sync
每次运行时sam sync
都需要Amazon CloudFormation部署。这样可以确保您的本地基础设施与之同步Amazon CloudFormation,从而防止漂移。使用此选项确实会增加开发和测试工作流程的时间。 -
提供
--skip-deploy-sync
使Amazon CloudFormation部署成为可选部署。Amazon SAMCLI会将您的本地Amazon SAM模板与已部署Amazon CloudFormation的模板进行比较,如果未检测到更改,则会跳过初始Amazon CloudFormation部署。在将本地更改同步到时,跳过Amazon CloudFormation部署可以节省时间。Amazon Web Services 云如果未检测到任何更改,则在以下情况下仍Amazon SAMCLI会执行Amazon CloudFormation部署:
-
如果自上次Amazon CloudFormation部署以来已经 7 天或更长时间了。
-
如果检测到大量 Lambda 函数代码更改,则使Amazon CloudFormation部署成为更新应用程序的最快方法。
-
以下是示例:
$
sam sync --skip-deploy-sync
同步嵌套堆栈中的资源
同步嵌套堆栈中的资源
-
使用提供根堆栈
--stack-name
。 -
使用以下格式标识嵌套堆栈中的资源:
。nestedStackId/resourceId
-
使用在嵌套堆栈中提供资源
--resource-id
。以下是示例:
$
sam sync --code --stack-name
sam-app
--resource-idmyNestedStack/HelloWorldFunction
有关创建嵌套应用程序的更多信息,请参阅使用嵌套应用程序。
指定要更新的特定Amazon CloudFormation堆栈
要指定要更新的特定Amazon CloudFormation堆栈,请提供--stack-name
选项。以下是示例:
$
sam sync --stack-name
dev-sam-app
问题排查
要排除故障 Amazon SAMCLI,请参阅Amazon SAMCLI故障排除。
示例
使用 sam sync 更新 Hello World 应用程序
在此示例中,我们首先初始化示例 Hello World 应用程序。要了解有关此应用程序的更多信息,请参阅教程:部署 Hello World 应用程序。
运行sam sync
开始构建和部署过程。
$
sam sync
The SAM CLI will use the AWS Lambda, Amazon API Gateway, and AWS StepFunctions APIs to upload your code without performing a CloudFormation deployment. This will cause drift in your CloudFormation stack. **The sync command should only be used against a development stack**. Confirm that you are synchronizing a development stack. Enter Y to proceed with the command, or enter N to cancel: [Y/n]: Queued infra sync. Waiting for in progress code syncs to complete... Starting infra sync. Manifest file is changed (new hash: 3298f13049d19cffaa37ca931dd4d421) or dependency folder (.aws-sam/deps/0663e6fe-a888-4efb-b908-e2344261e9c7) is missing for (HelloWorldFunction), downloading dependencies and copying/building source Building codeuri: /Users/.../Demo/sync/sam-app/hello_world runtime: python3.9 metadata: {} architecture: x86_64 functions: HelloWorldFunction Running PythonPipBuilder:CleanUp Running PythonPipBuilder:ResolveDependencies Running PythonPipBuilder:CopySource Build Succeeded Successfully packaged artifacts and wrote output template to file /var/folders/45/5ct135bx3fn2551_ptl5g6_80000gr/T/tmpx_5t4u3f. Execute the following command to deploy the packaged template sam deploy --template-file /var/folders/45/5ct135bx3fn2551_ptl5g6_80000gr/T/tmpx_5t4u3f --stack-name <YOUR STACK NAME> Deploying with following values =============================== Stack name : sam-app Region : us-west-2 Disable rollback : False Deployment s3 bucket : aws-sam-cli-managed-default-samclisourcebucket-1a4x26zbcdkqr Capabilities : ["CAPABILITY_NAMED_IAM", "CAPABILITY_AUTO_EXPAND"] Parameter overrides : {} Signing Profiles : null Initiating deployment ===================== 2023-03-17 11:17:19 - Waiting for stack create/update to complete CloudFormation events from stack operations (refresh every 0.5 seconds) --------------------------------------------------------------------------------------------------------------------------------------------- ResourceStatus ResourceType LogicalResourceId ResourceStatusReason --------------------------------------------------------------------------------------------------------------------------------------------- CREATE_IN_PROGRESS AWS::CloudFormation::Stack sam-app Transformation succeeded CREATE_IN_PROGRESS AWS::CloudFormation::Stack AwsSamAutoDependencyLayerNestedSt - ack CREATE_IN_PROGRESS AWS::IAM::Role HelloWorldFunctionRole - CREATE_IN_PROGRESS AWS::IAM::Role HelloWorldFunctionRole Resource creation Initiated CREATE_IN_PROGRESS AWS::CloudFormation::Stack AwsSamAutoDependencyLayerNestedSt Resource creation Initiated ack CREATE_COMPLETE AWS::IAM::Role HelloWorldFunctionRole - CREATE_COMPLETE AWS::CloudFormation::Stack AwsSamAutoDependencyLayerNestedSt - ack CREATE_IN_PROGRESS AWS::Lambda::Function HelloWorldFunction - CREATE_IN_PROGRESS AWS::Lambda::Function HelloWorldFunction Resource creation Initiated CREATE_COMPLETE AWS::Lambda::Function HelloWorldFunction - CREATE_IN_PROGRESS AWS::ApiGateway::RestApi ServerlessRestApi - CREATE_IN_PROGRESS AWS::ApiGateway::RestApi ServerlessRestApi Resource creation Initiated CREATE_COMPLETE AWS::ApiGateway::RestApi ServerlessRestApi - CREATE_IN_PROGRESS AWS::ApiGateway::Deployment ServerlessRestApiDeployment47fc2d - 5f9d CREATE_IN_PROGRESS AWS::Lambda::Permission HelloWorldFunctionHelloWorldPermi - ssionProd CREATE_IN_PROGRESS AWS::Lambda::Permission HelloWorldFunctionHelloWorldPermi Resource creation Initiated ssionProd CREATE_IN_PROGRESS AWS::ApiGateway::Deployment ServerlessRestApiDeployment47fc2d Resource creation Initiated 5f9d CREATE_COMPLETE AWS::ApiGateway::Deployment ServerlessRestApiDeployment47fc2d - 5f9d CREATE_IN_PROGRESS AWS::ApiGateway::Stage ServerlessRestApiProdStage - CREATE_IN_PROGRESS AWS::ApiGateway::Stage ServerlessRestApiProdStage Resource creation Initiated CREATE_COMPLETE AWS::ApiGateway::Stage ServerlessRestApiProdStage - CREATE_COMPLETE AWS::Lambda::Permission HelloWorldFunctionHelloWorldPermi - ssionProd CREATE_COMPLETE AWS::CloudFormation::Stack sam-app - --------------------------------------------------------------------------------------------------------------------------------------------- CloudFormation outputs from deployed stack ---------------------------------------------------------------------------------------------------------------------------------------------- Outputs ---------------------------------------------------------------------------------------------------------------------------------------------- Key HelloWorldFunctionIamRole Description Implicit IAM Role created for Hello World function Value arn:aws:iam::012345678910:role/sam-app-HelloWorldFunctionRole-BUFVMO2PJIYF Key HelloWorldApi Description API Gateway endpoint URL for Prod stage for Hello World function Value https://pcrx5gdaof.execute-api.us-west-2.amazonaws.com/Prod/hello/ Key HelloWorldFunction Description Hello World Lambda Function ARN Value arn:aws:lambda:us-west-2:012345678910:function:sam-app-HelloWorldFunction-2PlN6TPTQoco ---------------------------------------------------------------------------------------------------------------------------------------------- Stack creation succeeded. Sync infra completed. Infra sync completed. CodeTrigger not created as CodeUri or DefinitionUri is missing for ServerlessRestApi.
部署完成后,我们会修改HelloWorldFunction
代码。会Amazon SAMCLI检测到此更改,并将我们的应用程序同步到。Amazon Web Services 云由于Amazon Lambda支持Amazon服务 API,因此可以执行快速同步。
Syncing Lambda Function HelloWorldFunction...
Manifest is not changed for (HelloWorldFunction), running incremental build
Building codeuri: /Users/.../Demo/sync/sam-app/hello_world runtime: python3.9 metadata: {} architecture: x86_64 functions: HelloWorldFunction
Running PythonPipBuilder:CopySource
Finished syncing Lambda Function HelloWorldFunction.
接下来,我们在应用程序的Amazon SAM模板中修改我们的 API 端点。我们改/hello
为/helloworld
。
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 ... Resources: HelloWorldFunction: ... Properties: ... Events: HelloWorld: Type: Api Properties: Path:
/helloworld
Method: get
由于 Amazon API Gateway 资源不支持Amazon服务 API,因此Amazon SAMCLI会自动执行Amazon CloudFormation部署。以下是输出示例:
Queued infra sync. Waiting for in progress code syncs to complete... Starting infra sync. Manifest is not changed for (HelloWorldFunction), running incremental build Building codeuri: /Users/.../Demo/sync/sam-app/hello_world runtime: python3.9 metadata: {} architecture: x86_64 functions: HelloWorldFunction Running PythonPipBuilder:CopySource Build Succeeded Successfully packaged artifacts and wrote output template to file /var/folders/45/5ct135bx3fn2551_ptl5g6_80000gr/T/tmpuabo0jb9. Execute the following command to deploy the packaged template sam deploy --template-file /var/folders/45/5ct135bx3fn2551_ptl5g6_80000gr/T/tmpuabo0jb9 --stack-name <YOUR STACK NAME> Deploying with following values =============================== Stack name : sam-app Region : us-west-2 Disable rollback : False Deployment s3 bucket : aws-sam-cli-managed-default-samclisourcebucket-1a4x26zbcdkqr Capabilities : ["CAPABILITY_NAMED_IAM", "CAPABILITY_AUTO_EXPAND"] Parameter overrides : {} Signing Profiles : null Initiating deployment ===================== 2023-03-17 14:41:18 - Waiting for stack create/update to complete CloudFormation events from stack operations (refresh every 0.5 seconds) --------------------------------------------------------------------------------------------------------------------------------------------- ResourceStatus ResourceType LogicalResourceId ResourceStatusReason --------------------------------------------------------------------------------------------------------------------------------------------- UPDATE_IN_PROGRESS AWS::CloudFormation::Stack sam-app Transformation succeeded UPDATE_IN_PROGRESS AWS::CloudFormation::Stack AwsSamAutoDependencyLayerNestedSt - ack UPDATE_COMPLETE AWS::CloudFormation::Stack AwsSamAutoDependencyLayerNestedSt - ack UPDATE_IN_PROGRESS AWS::ApiGateway::RestApi ServerlessRestApi - UPDATE_COMPLETE AWS::ApiGateway::RestApi ServerlessRestApi - CREATE_IN_PROGRESS AWS::ApiGateway::Deployment ServerlessRestApiDeployment8cf30e - d3cd UPDATE_IN_PROGRESS AWS::Lambda::Permission HelloWorldFunctionHelloWorldPermi Requested update requires the ssionProd creation of a new physical resource; hence creating one. UPDATE_IN_PROGRESS AWS::Lambda::Permission HelloWorldFunctionHelloWorldPermi Resource creation Initiated ssionProd CREATE_IN_PROGRESS AWS::ApiGateway::Deployment ServerlessRestApiDeployment8cf30e Resource creation Initiated d3cd CREATE_COMPLETE AWS::ApiGateway::Deployment ServerlessRestApiDeployment8cf30e - d3cd UPDATE_IN_PROGRESS AWS::ApiGateway::Stage ServerlessRestApiProdStage - UPDATE_COMPLETE AWS::ApiGateway::Stage ServerlessRestApiProdStage - UPDATE_COMPLETE AWS::Lambda::Permission HelloWorldFunctionHelloWorldPermi - ssionProd UPDATE_COMPLETE_CLEANUP_IN_PROGRE AWS::CloudFormation::Stack sam-app - SS DELETE_IN_PROGRESS AWS::Lambda::Permission HelloWorldFunctionHelloWorldPermi - ssionProd DELETE_IN_PROGRESS AWS::ApiGateway::Deployment ServerlessRestApiDeployment47fc2d - 5f9d DELETE_COMPLETE AWS::ApiGateway::Deployment ServerlessRestApiDeployment47fc2d - 5f9d UPDATE_COMPLETE AWS::CloudFormation::Stack AwsSamAutoDependencyLayerNestedSt - ack DELETE_COMPLETE AWS::Lambda::Permission HelloWorldFunctionHelloWorldPermi - ssionProd UPDATE_COMPLETE AWS::CloudFormation::Stack sam-app - --------------------------------------------------------------------------------------------------------------------------------------------- CloudFormation outputs from deployed stack ---------------------------------------------------------------------------------------------------------------------------------------------- Outputs ---------------------------------------------------------------------------------------------------------------------------------------------- Key HelloWorldFunctionIamRole Description Implicit IAM Role created for Hello World function Value arn:aws:iam::012345678910:role/sam-app-HelloWorldFunctionRole-BUFVMO2PJIYF Key HelloWorldApi Description API Gateway endpoint URL for Prod stage for Hello World function Value https://pcrx5gdaof.execute-api.us-west-2.amazonaws.com/Prod/hello/ Key HelloWorldFunction Description Hello World Lambda Function ARN Value arn:aws:lambda:us-west-2:012345678910:function:sam-app-HelloWorldFunction-2PlN6TPTQoco ---------------------------------------------------------------------------------------------------------------------------------------------- Stack update succeeded. Sync infra completed. Infra sync completed.
了解更多信息
有关所有sam sync
选项的说明,请参见sam sync。