View a markdown version of this page

使用部署简介 AWS SAM - AWS Serverless Application Model

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

使用部署简介 AWS SAM

使用 AWS Serverless Application Model 命令行界面 (AWS SAM CLI) sam deploy 命令将您的无服务器应用程序部署到 AWS Cloud。

重要

模板可以定义 IAM 角色和其他资源 sam deploy 使用您的证书创建。在部署任何未创建的项目之前,请查看其模板和配置文件。

先决条件

要使用 sam deploy,请完成以下操作安装 AWS SAM CLI。

我们建议您在使用 sam deploy 之前初步了解以下主题:

使用 sam deploy 部署应用程序

首次部署无服务器应用程序时,请使用 --guided 选项。 AWS SAM CLI 会指导您完成交互式流程,以配置应用程序的部署设置。

使用交互式流程部署应用程序
  1. 转到项目的根目录。这和你的位置一样 AWS SAM 模板。

    $ cd sam-app
  2. 运行以下命令:

    $ sam deploy --guided
  3. 在交互式流程中, AWS SAM CLI 会为您提供用于配置应用程序部署设置的选项。

    方括号 ([ ]) 表示默认值。将答案留空以选择默认值。可从以下配置文件获得默认值:

    • ~/.aws/config — 你的将军 AWS 账户设置。

    • ~/.aws/credentials — 你的 AWS 账户凭证。

    • <project>/samconfig.toml - 项目的配置文件。

    通过回答 AWS SAM CLI 提示来提供值。例如,可以输入 y(表示是)、n(表示否)或字符串值。

    AWS SAM CLI 会将您的回答写入到项目的 samconfig.toml 文件中。对于后续部署,您可以使用 sam deploy 和这些配置的值进行部署。要重新配置这些值,请再次使用 sam deploy --guided 或直接修改配置文件。

    下面是一个示例输出:

    sam-app $ sam deploy --guided Configuring SAM deploy ====================== Looking for config file [samconfig.toml] : Found Reading default arguments : Success Setting default arguments for 'sam deploy' ========================================= Stack Name [sam-app]: ENTER AWS Region [us-west-2]: ENTER #Shows you resources changes to be deployed and require a 'Y' to initiate deploy Confirm changes before deploy [Y/n]: ENTER #SAM needs permission to be able to create roles to connect to the resources in your template Allow SAM CLI IAM role creation [Y/n]: ENTER #Preserves the state of previously provisioned resources when an operation fails Disable rollback [y/N]: ENTER HelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y Save arguments to configuration file [Y/n]: ENTER SAM configuration file [samconfig.toml]: ENTER SAM configuration environment [default]: ENTER
  4. 接下来, AWS SAM CLI 将您的应用程序部署到 AWS Cloud。在部署过程中,命令提示符中会显示进度。以下是部署的主要阶段:

    • 适用于具有以下条件的应用程序 AWS Lambda 事件函数打包为 .zip 文件存档, AWS SAM CLI 压缩包并将其上传到亚马逊简单存储服务 (Amazon S3) 存储桶。如有必要, AWS SAM CLI 会创建新的存储桶。

    • 对于将 Lambda 函数打包为容器映像的应用程序, AWS SAM CLI 会将映像上传到 Amazon Elastic Container Registry (Amazon ECR)。如有必要, AWS SAM CLI 会创建新的存储库。

    • 这个 AWS SAM CLI 创建一个 AWS CloudFormation 更改设置并将您的应用程序部署到 CloudFormation 作为堆栈。

    • 这个 AWS SAM CLI 修改您已部署的 AWS SAM 使用新模板 CodeUri 您的 Lambda 函数的值。

    下面是 AWS SAM CLI 部署输出的一个示例:

    Looking for resources needed for deployment: Managed S3 bucket: aws-sam-cli-managed-default-samclisam-s3-demo-bucket-1a4x26zbcdkqr A different default S3 bucket can be set in samconfig.toml and auto resolution of buckets turned off by setting resolve_s3=False Parameter "stack_name=sam-app" in [default.deploy.parameters] is defined as a global parameter [default.global.parameters]. This parameter will be only saved under [default.global.parameters] in /Users/.../sam-app/samconfig.toml. Saved arguments to config file Running 'sam deploy' for future deployments will use the parameters saved above. The above parameters can be changed by modifying samconfig.toml Learn more about samconfig.toml syntax at https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html Uploading to sam-app-zip/da3c598813f1c2151579b73ad788cac8 262144 / 619839 (42.29%)Uploading to sam-app-zip/da3c598813f1c2151579b73ad788cac8 524288 / 619839 (84.58%)Uploading to sam-app-zip/da3c598813f1c2151579b73ad788cac8 619839 / 619839 (100.00%) Deploying with following values =============================== Stack name : sam-app Region : us-west-2 Confirm changeset : True Disable rollback : False Deployment s3 bucket : aws-sam-cli-managed-default-samclisam-s3-demo-bucket-1a4x26zbcdkqr Capabilities : ["CAPABILITY_IAM"] Parameter overrides : {} Signing Profiles : {} Initiating deployment ===================== Uploading to sam-app-zip/be84c20f868068e4dc4a2c11966edf2d.template 1212 / 1212 (100.00%) Waiting for changeset to be created.. CloudFormation stack changeset ------------------------------------------------------------------------------------------------- Operation LogicalResourceId ResourceType Replacement ------------------------------------------------------------------------------------------------- + Add HelloWorldFunctionHell AWS::Lambda::Permissio N/A oWorldPermissionProd n + Add HelloWorldFunctionRole AWS::IAM::Role N/A + Add HelloWorldFunction AWS::Lambda::Function N/A + Add ServerlessRestApiDeplo AWS::ApiGateway::Deplo N/A yment47fc2d5f9d yment + Add ServerlessRestApiProdS AWS::ApiGateway::Stage N/A tage + Add ServerlessRestApi AWS::ApiGateway::RestA N/A pi ------------------------------------------------------------------------------------------------- Changeset created successfully. arn:aws:cloudformation:us-west-2:012345678910:changeSet/samcli-deploy1680559234/d9f58a77-98bc-41cd-b9f4-433a5a450d7a Previewing CloudFormation changeset before deployment ====================================================== Deploy this changeset? [y/N]: y 2023-04-03 12:00:50 - Waiting for stack create/update to complete CloudFormation events from stack operations (refresh every 5.0 seconds) ------------------------------------------------------------------------------------------------- ResourceStatus ResourceType LogicalResourceId ResourceStatusReason ------------------------------------------------------------------------------------------------- CREATE_IN_PROGRESS AWS::IAM::Role HelloWorldFunctionRole - CREATE_IN_PROGRESS AWS::IAM::Role HelloWorldFunctionRole Resource creation Initiated CREATE_COMPLETE AWS::IAM::Role HelloWorldFunctionRole - 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::RestA ServerlessRestApi - pi CREATE_IN_PROGRESS AWS::ApiGateway::RestA ServerlessRestApi Resource creation pi Initiated CREATE_COMPLETE AWS::ApiGateway::RestA ServerlessRestApi - pi CREATE_IN_PROGRESS AWS::Lambda::Permissio HelloWorldFunctionHell - n oWorldPermissionProd CREATE_IN_PROGRESS AWS::ApiGateway::Deplo ServerlessRestApiDeplo - yment yment47fc2d5f9d CREATE_IN_PROGRESS AWS::Lambda::Permissio HelloWorldFunctionHell Resource creation n oWorldPermissionProd Initiated CREATE_IN_PROGRESS AWS::ApiGateway::Deplo ServerlessRestApiDeplo Resource creation yment yment47fc2d5f9d Initiated CREATE_COMPLETE AWS::ApiGateway::Deplo ServerlessRestApiDeplo - yment yment47fc2d5f9d CREATE_IN_PROGRESS AWS::ApiGateway::Stage ServerlessRestApiProdS - tage CREATE_IN_PROGRESS AWS::ApiGateway::Stage ServerlessRestApiProdS Resource creation tage Initiated CREATE_COMPLETE AWS::ApiGateway::Stage ServerlessRestApiProdS - tage CREATE_COMPLETE AWS::Lambda::Permissio HelloWorldFunctionHell - n oWorldPermissionProd CREATE_COMPLETE AWS::CloudFormation::S sam-app-zip - tack ------------------------------------------------------------------------------------------------- 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-zip- HelloWorldFunctionRole-11ZOGSCG28H0M Key HelloWorldApi Description API Gateway endpoint URL for Prod stage for Hello World function Value https://njzfhdmls0.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-XPqNX4TBu7qn ------------------------------------------------------------------------------------------------- Successfully created/updated stack - sam-app-zip in us-west-2
  5. 要查看您部署的应用程序,请执行以下操作:

    1. 打开 CloudFormation 直接使用 URL 控制台 https://console.aws.amazon.com/cloudformation。

    2. 选择堆栈。

    3. 通过应用程序名称识别堆栈并选择它。

在部署之前验证更改

你可以配置 AWS SAM CLI 显示你的 CloudFormation 在部署之前更改设置并要求确认。

在部署之前确认更改
  1. 使用 sam deploy --guided 进行部署时,在部署之前输入 Y 确认更改。

    #Shows you resources changes to be deployed and require a 'Y' to initiate deploy
    Confirm changes before deploy [Y/n]: Y

    或者,可以使用以下方法修改 samconfig.toml 文件:

    [default.deploy]
    [default.deploy.parameters]
    confirm_changeset = true
  2. 在部署过程中, AWS SAM CLI 会要求您在部署之前确认更改。以下是示例:

    Waiting for changeset to be created.. CloudFormation stack changeset ------------------------------------------------------------------------------------------------- Operation LogicalResourceId ResourceType Replacement ------------------------------------------------------------------------------------------------- + Add HelloWorldFunctionHell AWS::Lambda::Permissio N/A oWorldPermissionProd n + Add HelloWorldFunctionRole AWS::IAM::Role N/A + Add HelloWorldFunction AWS::Lambda::Function N/A + Add ServerlessRestApiDeplo AWS::ApiGateway::Deplo N/A yment47fc2d5f9d yment + Add ServerlessRestApiProdS AWS::ApiGateway::Stage N/A tage + Add ServerlessRestApi AWS::ApiGateway::RestA N/A pi ------------------------------------------------------------------------------------------------- Changeset created successfully. arn:aws:cloudformation:us-west-2:012345678910:changeSet/samcli-deploy1680559234/d9f58a77-98bc-41cd-b9f4-433a5a450d7a Previewing CloudFormation changeset before deployment ====================================================== Deploy this changeset? [y/N]: y

在部署过程中指定其他参数

您可以在部署时指定要配置的其他参数值。若要这样做,只需在部署过程中修改 AWS SAM 模板并配置参数值。

指定其他参数
  1. 修改 Parameters 你的部分 AWS SAM 模板。以下是示例:

    AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 ... Globals: ... Parameters: DomainName: Type: String Default: example Description: Domain name
  2. 运行 sam deploy --guided。下面是一个示例输出:

    sam-app $ sam deploy --guided Configuring SAM deploy ====================== Looking for config file [samconfig.toml] : Found Reading default arguments : Success Setting default arguments for 'sam deploy' ========================================= Stack Name [sam-app-zip]: ENTER AWS Region [us-west-2]: ENTER Parameter DomainName [example]: ENTER

为您的 Lambda 事件函数配置代码签名

您可以在部署时配置 Lambda 函数的代码签名。你可以通过修改你的 AWS SAM 在部署期间模板和配置代码签名。

配置代码签名
  1. 请指定 CodeSigningConfigArn 在你的 AWS SAM 模板。以下是示例:

    AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 ... Resources: HelloWorldFunction: Type: AWS::Serverless::Function Properties: CodeUri: hello_world/ Handler: app.lambda_handler Runtime: python3.7 CodeSigningConfigArn: arn:aws:lambda:us-east-1:111122223333:code-signing-config:csc-12e12345db1234567
  2. 运行 sam deploy --guided。 AWS SAM CLI 会提示您配置代码签名。下面是一个示例输出:

    #Found code signing configurations in your function definitions
    Do you want to sign your code? [Y/n]: ENTER
    #Please provide signing profile details for the following functions & layers
    #Signing profile details for function 'HelloWorld'
    Signing Profile Name: 
    Signing Profile Owner Account ID (optional):
    #Signing profile details for layer 'MyLayer', which is used by functions {'HelloWorld'}
    Signing Profile Name: 
    Signing Profile Owner Account ID (optional):

构建和部署 Lambda Web 函数

你可以设置 CodeUri 的一个 AWS::Serverless::WebFunction 到本地目录。这个 AWS SAM CLI 使用以下方法构建代码 sam build 并在你运行时将其上传到 Amazon S3 sam deploy, sam sync,或 sam package。

重要

因为 Web 函数的修订只要存在就从 Amazon S3 读取代码, sam deploy 和 sam sync 将 Web 函数代码上传到应用程序堆栈的 Amazon S3 存储桶,而不是将其上传到托管存储桶 AWS SAM CLI 用于其他文物。

建筑物

sam build 构建 Web 函数的方式与构建 Web 函数的方式相同 Node.js Lambda 事件函数,有以下区别:

  • 部署包始终是 .zip 专为... 构建的文件存档 x86_64 建筑。

  • 该版本始终在部署包中保留依赖关系,因为 Web Functions 不能使用图层。这也适用于 sam sync,否则它会将依赖关系移至自动创建的层。

要将代码与 esbuild 捆绑在一起,请设置 BuildMethod: esbuild 在资源中 Metadata。这个 EntryPoint 文件是 esbuild 的入口点,也是构建的模板的 EntryPoint 指向捆绑的文件。例如,src/server.ts 改为 server.js。有关您可以设置的编译属性,请参见 大厦 Node.js 带有 esbuild 的 Lambda 事件函数 AWS SAM。

Resources: MyWebFunction: Type: AWS::Serverless::WebFunction Properties: Runtime: nodejs24.x AuthType: IamAuth CodeUri: web/ EntryPoint: src/server.ts Metadata: BuildMethod: esbuild BuildProperties: Minify: true

代码上传到哪里

命令 Web 函数代码已上传到
sam deploy --guided 或 sam deploy --resolve-s3 堆栈的代码桶。和 --guided,这是您在提示符下输入的堆栈名称。
sam sync 堆栈的代码桶。
sam deploy, sam sync,或 sam package 和 --s3-bucket 您指定的存储桶。请参阅使用自己的存储桶。
sam package --resolve-s3 不支持。 sam package 没有可为其创建代码存储桶的堆栈,因此会返回错误。使用 sam deploy,或者通过 --s3-bucket。

一个 CodeUri 这已经表明Amazon S3尚未上传并保留其位置。同一模板中的 Lambda 事件函数和其他项目仍使用托管存储桶或您指定的存储桶。

堆栈的代码桶

首次使用包含本地代码的 Web 函数部署堆栈时, AWS SAM CLI 创建一个 CloudFormation 堆栈命名 stack-name-hash-WebFunctionCode。它包含一个 Amazon S3 存储桶,该存储桶:

  • 启用了版本控制,因此每个修订版本都可以固定其使用的对象版本。

  • 使用 Amazon S3 托管密钥进行服务器端加密 (SSE-S3)。

  • 阻止所有公共访问,并拒绝不使用 HTTPS 的请求。

  • 允许 lambda.amazonaws.com 打电话 s3:GetObject 和 s3:GetObjectVersion 仅适用于您账户中的 Web 函数修订版。

堆栈导出存储桶名称,打包后的模板将其导入:

CodeUri: Bucket: !ImportValue stack-name-hash-WebFunctionCode-Bucket Key: object-key Version: object-version-id

由于这种进口, CloudFormation 当任何堆栈(包括嵌套堆栈)从中读取代码时,都不会删除代码存储桶堆栈。

使用自己的存储桶

当你通过时 --s3-bucket,该存储桶必须满足中的要求 AWS::Serverless::WebFunction。在它上传任何东西之前, AWS SAM CLI 检查存储桶:

  • 如果存储桶未启用版本控制,则该命令将失败。

  • 如果 --kms-key-id 是一个 AWS 管理密钥,命令失败。使用密钥策略允许的客户管理的密钥 lambda.amazonaws.com 打电话 kms:Decrypt。

  • 如果存储桶策略或密钥策略似乎不允许 Lambda 读取代码,则该命令会打印警告并继续。

没有 --kms-key-id, AWS SAM CLI 上传代码和 SSE-S3,即使存储桶的默认加密使用 AWS KMS。

使用 sam 同步

对 Web 函数代码的每次更改都会创建一个新的修订版,所以 sam sync --watch 不看 Web 函数代码。它在启动时会打印一条注释,代码更改将在下次基础设施同步时部署。

Deleting

sam delete 删除堆栈,然后清空代码存储桶并删除代码桶堆栈。

如果 Web 函数的寿命超过了堆栈的使用寿命,则其修订版仍会从存储桶中读取代码。当 Web 函数或包含它的嵌套堆栈存在时,就会发生这种情况 DeletionPolicy: Retain 或者列在 --retain-resources。在这种情况下, sam delete 保留代码桶堆栈,打开其终止保护,并列出保留的 Web 函数。当您不再需要它们时,请关闭终止保护,清空存储桶并删除堆栈。

如果更早 sam delete 删除了堆栈但没有删除代码桶堆栈,运行 sam delete 再次将其删除。

限制

  • 包含本地代码的 Web 函数无法由生成 Fn::ForEach。在外面定义 Fn::ForEach,或者将其设置为 CodeUri 转到亚马逊 S3 中已经存在的代码。有关更多信息,请参阅 CloudFormation 语言扩展支持。

  • 这个 sam local 命令不运行 Web 函数。因为 Web 函数是一个 HTTP 服务器,所以你可以像其他任何函数一样在本地运行和测试它 Node.js 服务器,例如 npm start 要么 node server.js。如果您的服务器从读取其主机和端口 AWS_LAMBDA_HTTP_ENDPOINT 环境变量,在运行时设置该变量。

最佳实践

  • 使用 sam deploy 时, AWS SAM CLI 会部署位于 .aws-sam 目录中的应用程序的构建构件。如果您更改了应用程序的原始文件,请在部署之前运行 sam build 以更新 .aws-sam 目录。

  • 首次部署应用程序时,使用 sam deploy --guided 来配置部署设置。对于后续部署,您可以使用 sam deploy 和配置的设置进行部署。

sam deploy 的选项

以下是 sam deploy 的常用选项:有关全部选项的列表,请参阅 sam deploy。

使用有指导的交互式流程部署应用程序

使用 --guided 选项通过交互式流程配置应用程序的部署设置。以下是示例:

$ sam deploy --guided

应用程序的部署设置保存在项目的 samconfig.toml 文件中。要了解更多信息,请参阅配置项目设置。

故障排除

要进行故障排除 AWS SAM CLI,参见 AWS SAM CLI 故障排除。

示例

部署包含打包为 .zip 文件存档的 Lambda 事件函数的 Hello World 应用程序

有关示例,请参阅 Hello World 应用程序教程中的步骤 3:将您的应用程序部署到 AWS Cloud。

部署包含打包为容器镜像的 Lambda 事件函数的 Hello World 应用程序

首先,使用 sam init 创建 Hello World 应用程序。在交互式流程中,选择 Python3.9 运行时和 Image 包类型。

$ sam init ... Which template source would you like to use? 1 - AWS Quick Start Templates 2 - Custom Template Location Choice: 1 Choose an AWS Quick Start application template 1 - Hello World Example 2 - Multi-step workflow ... Template: 1 Use the most popular runtime and package type? (Python and zip) [y/N]: ENTER Which runtime would you like to use? 1 - aot.dotnet7 (provided.al2) ... 15 - nodejs12.x 16 - python3.9 17 - python3.8 ... Runtime: 16 What package type would you like to use? 1 - Zip 2 - Image Package type: 2 Based on your selections, the only dependency manager available is pip. We will proceed copying the template using pip. ... Project name [sam-app]: ENTER ----------------------- Generating application: ----------------------- Name: sam-app Base Image: amazon/python3.9-base Architectures: x86_64 Dependency Manager: pip Output Directory: . Configuration file: sam-app/samconfig.toml Next steps can be found in the README file at sam-app/README.md ...

然后,运行 cd 以进入项目的根目录并运行 sam build。 AWS SAM CLI 会使用 Docker 在本地构建 Lambda 函数。

sam-app $ sam build Building codeuri: /Users/.../sam-app runtime: None metadata: {'Dockerfile': 'Dockerfile', 'DockerContext': '/Users/.../sam-app/hello_world', 'DockerTag': 'python3.9-v1'} architecture: x86_64 functions: HelloWorldFunction Building image for HelloWorldFunction function Setting DockerBuildArgs: {} for HelloWorldFunction function Step 1/5 : FROM public.ecr.aws/lambda/python:3.9 ---> 0a5e3da309aa Step 2/5 : COPY requirements.txt ./ ---> abc4e82e85f9 Step 3/5 : RUN python3.9 -m pip install -r requirements.txt -t . ---> [Warning] The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested ---> Running in 43845e7aa22d Collecting requests Downloading requests-2.28.2-py3-none-any.whl (62 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 62.8/62.8 KB 829.5 kB/s eta 0:00:00 Collecting idna<4,>=2.5 Downloading idna-3.4-py3-none-any.whl (61 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 61.5/61.5 KB 2.4 MB/s eta 0:00:00 Collecting charset-normalizer<4,>=2 Downloading charset_normalizer-3.1.0-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (199 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 199.2/199.2 KB 2.1 MB/s eta 0:00:00 Collecting certifi>=2017.4.17 Downloading certifi-2022.12.7-py3-none-any.whl (155 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 155.3/155.3 KB 10.2 MB/s eta 0:00:00 Collecting urllib3<1.27,>=1.21.1 Downloading urllib3-1.26.15-py2.py3-none-any.whl (140 kB) ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 140.9/140.9 KB 9.1 MB/s eta 0:00:00 Installing collected packages: urllib3, idna, charset-normalizer, certifi, requests Successfully installed certifi-2022.12.7 charset-normalizer-3.1.0 idna-3.4 requests-2.28.2 urllib3-1.26.15 Removing intermediate container 43845e7aa22d ---> cab8ace899ce Step 4/5 : COPY app.py ./ ---> 4146f3cd69f2 Step 5/5 : CMD ["app.lambda_handler"] ---> [Warning] The requested image's platform (linux/amd64) does not match the detected host platform (linux/arm64/v8) and no specific platform was requested ---> Running in f4131ddffb31 Removing intermediate container f4131ddffb31 ---> d2f5180b2154 Successfully built d2f5180b2154 Successfully tagged helloworldfunction:python3.9-v1 Build Succeeded Built Artifacts : .aws-sam/build Built Template : .aws-sam/build/template.yaml Commands you can use next ========================= [*] Validate SAM template: sam validate [*] Invoke Function: sam local invoke [*] Test Function in the Cloud: sam sync --stack-name {{stack-name}} --watch [*] Deploy: sam deploy --guided

然后,运行 sam deploy --guided 部署应用程序。 AWS SAM CLI 会指导我们配置部署设置。然后, AWS SAM CLI 将我们的应用程序部署到 AWS Cloud。

sam-app $ sam deploy --guided Configuring SAM deploy ====================== Looking for config file [samconfig.toml] : Found Reading default arguments : Success Setting default arguments for 'sam deploy' ========================================= Stack Name [sam-app]: ENTER AWS Region [us-west-2]: ENTER #Shows you resources changes to be deployed and require a 'Y' to initiate deploy Confirm changes before deploy [Y/n]: ENTER #SAM needs permission to be able to create roles to connect to the resources in your template Allow SAM CLI IAM role creation [Y/n]: ENTER #Preserves the state of previously provisioned resources when an operation fails Disable rollback [y/N]: ENTER HelloWorldFunction may not have authorization defined, Is this okay? [y/N]: y Save arguments to configuration file [Y/n]: ENTER SAM configuration file [samconfig.toml]: ENTER SAM configuration environment [default]: ENTER Looking for resources needed for deployment: Managed S3 bucket: aws-sam-cli-managed-default-samclisam-s3-demo-bucket-1a4x26zbcdkqr A different default S3 bucket can be set in samconfig.toml and auto resolution of buckets turned off by setting resolve_s3=False Parameter "stack_name=sam-app" in [default.deploy.parameters] is defined as a global parameter [default.global.parameters]. This parameter will be only saved under [default.global.parameters] in /Users/.../sam-app/samconfig.toml. Saved arguments to config file Running 'sam deploy' for future deployments will use the parameters saved above. The above parameters can be changed by modifying samconfig.toml Learn more about samconfig.toml syntax at https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html e95fc5e75742: Pushed d8df51e7bdd7: Pushed b1d0d7e0b34a: Pushed 0071317b94d8: Pushed d98f98baf147: Pushed 2d244e0816c6: Pushed eb2eeb1ebe42: Pushed a5ca065a3279: Pushed fe9e144829c9: Pushed helloworldfunction-d2f5180b2154-python3.9-v1: digest: sha256:cceb71401b47dc3007a7a1e1f2e0baf162999e0e6841d15954745ecc0c447533 size: 2206 Deploying with following values =============================== Stack name : sam-app Region : us-west-2 Confirm changeset : True Disable rollback : False Deployment image repository : { "HelloWorldFunction": "012345678910.dkr.ecr.us-west-2.amazonaws.com/samapp7427b055/helloworldfunction19d43fc4repo" } Deployment s3 bucket : aws-sam-cli-managed-default-samclisam-s3-demo-bucket-1a4x26zbcdkqr Capabilities : ["CAPABILITY_IAM"] Parameter overrides : {} Signing Profiles : {} Initiating deployment ===================== HelloWorldFunction may not have authorization defined. Uploading to sam-app/682ad27c7cf7a17c7f77a1688b0844f2.template 1328 / 1328 (100.00%) Waiting for changeset to be created.. CloudFormation stack changeset ------------------------------------------------------------------------------------------------- Operation LogicalResourceId ResourceType Replacement ------------------------------------------------------------------------------------------------- + Add HelloWorldFunctionHell AWS::Lambda::Permissio N/A oWorldPermissionProd n + Add HelloWorldFunctionRole AWS::IAM::Role N/A + Add HelloWorldFunction AWS::Lambda::Function N/A + Add ServerlessRestApiDeplo AWS::ApiGateway::Deplo N/A yment47fc2d5f9d yment + Add ServerlessRestApiProdS AWS::ApiGateway::Stage N/A tage + Add ServerlessRestApi AWS::ApiGateway::RestA N/A pi ------------------------------------------------------------------------------------------------- Changeset created successfully. arn:aws:cloudformation:us-west-2:012345678910:changeSet/samcli-deploy1680634124/0ffd4faf-2e2b-487e-b9e0-9116e8299ac4 Previewing CloudFormation changeset before deployment ====================================================== Deploy this changeset? [y/N]: y 2023-04-04 08:49:15 - Waiting for stack create/update to complete CloudFormation events from stack operations (refresh every 5.0 seconds) ------------------------------------------------------------------------------------------------- ResourceStatus ResourceType LogicalResourceId ResourceStatusReason ------------------------------------------------------------------------------------------------- CREATE_IN_PROGRESS AWS::CloudFormation::S sam-app User Initiated tack CREATE_IN_PROGRESS AWS::IAM::Role HelloWorldFunctionRole - CREATE_IN_PROGRESS AWS::IAM::Role HelloWorldFunctionRole Resource creation Initiated CREATE_COMPLETE AWS::IAM::Role HelloWorldFunctionRole - 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::RestA ServerlessRestApi - pi CREATE_IN_PROGRESS AWS::ApiGateway::RestA ServerlessRestApi Resource creation pi Initiated CREATE_COMPLETE AWS::ApiGateway::RestA ServerlessRestApi - pi CREATE_IN_PROGRESS AWS::Lambda::Permissio HelloWorldFunctionHell - n oWorldPermissionProd CREATE_IN_PROGRESS AWS::ApiGateway::Deplo ServerlessRestApiDeplo - yment yment47fc2d5f9d CREATE_IN_PROGRESS AWS::Lambda::Permissio HelloWorldFunctionHell Resource creation n oWorldPermissionProd Initiated CREATE_IN_PROGRESS AWS::ApiGateway::Deplo ServerlessRestApiDeplo Resource creation yment yment47fc2d5f9d Initiated CREATE_COMPLETE AWS::ApiGateway::Deplo ServerlessRestApiDeplo - yment yment47fc2d5f9d CREATE_IN_PROGRESS AWS::ApiGateway::Stage ServerlessRestApiProdS - tage CREATE_IN_PROGRESS AWS::ApiGateway::Stage ServerlessRestApiProdS Resource creation tage Initiated CREATE_COMPLETE AWS::ApiGateway::Stage ServerlessRestApiProdS - tage CREATE_COMPLETE AWS::Lambda::Permissio HelloWorldFunctionHell - n oWorldPermissionProd CREATE_COMPLETE AWS::CloudFormation::S sam-app - tack ------------------------------------------------------------------------------------------------- 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-JFML1JOKHJ71 Key HelloWorldApi Description API Gateway endpoint URL for Prod stage for Hello World function Value https://endlwiqqod.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- kyg6Y2iNRUPg ------------------------------------------------------------------------------------------------- Successfully created/updated stack - sam-app in us-west-2

了解详情

要了解有关使用的更多信息 AWS SAM CLI sam deploy 命令,请参见以下内容: