本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
如何在部署時 AWS SAM 上傳本機檔案
當您將應用程式部署到 時 AWS 雲端, AWS CloudFormation 需要先將本機檔案上傳至可存取 AWS 的服務,例如 Amazon Simple Storage Service (Amazon S3)。這包含您的 AWS SAM 範本參考的本機檔案。為了符合此要求, AWS SAM CLI 當您使用 sam deploy
或 sam package
命令時, 會執行下列動作:
-
自動將本機檔案上傳至可存取 AWS 的服務。
-
自動更新您的應用程式範本,以參考新的檔案路徑。
示範:使用 AWS SAM CLI 上傳 Lambda 函數程式碼
在此示範中,我們使用 Lambda 函數的 .zip 套件類型初始化範例 Hello World 應用程式。我們使用 AWS SAM CLI 自動將 Lambda 函數程式碼上傳至 Amazon S3,並在應用程式範本中參考其新路徑。
首先,我們會執行 sam init
來初始化 Hello World 應用程式。
$
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]:y
Would you like to enable X-Ray tracing on the function(s) in your application? [y/N]:ENTER
Would you like to enable monitoring using CloudWatch Application Insights? For more info, please view https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/cloudwatch-application-insights.html [y/N]:ENTER
Project name [sam-app]:demo
----------------------- Generating application: ----------------------- Name: demo Runtime: python3.9 Architectures: x86_64 Dependency Manager: pip Application Template: hello-world Output Directory: . Configuration file: demo/samconfig.toml ...
我們的 Lambda 函數程式碼在專案的hello_world
子目錄中組織。
demo ├── README.md ├── hello_world │ ├── __init__.py │ ├── app.py │ └── requirements.txt ├── template.yaml └── tests
在我們的 AWS SAM 範本中,我們使用 CodeUri
屬性參考 Lambda 函數程式碼的本機路徑。
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 ... Resources: HelloWorldFunction: Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction Properties: CodeUri: hello_world/ Handler: app.lambda_handler Runtime: python3.9 ...
接下來,我們會執行 sam build
來建置應用程式並準備部署。
$
sam build
Starting Build use cache Manifest file is changed (new hash: 3298f13049d19cffaa37ca931dd4d421) or dependency folder (.aws-sam/deps/7896875f-9bcc-4350-8adb-2c1d543627a1) is missing for (HelloWorldFunction), downloading dependencies and copying/building source Building codeuri: /Users/.../demo/hello_world runtime: python3.9 metadata: {} architecture: x86_64 functions: HelloWorldFunction Running PythonPipBuilder:CleanUp Running PythonPipBuilder:ResolveDependencies Running PythonPipBuilder:CopySource Running PythonPipBuilder:CopySource Build Succeeded Built Artifacts : .aws-sam/build Built Template : .aws-sam/build/template.yaml ...
接下來,我們會執行 sam deploy --guided
來部署應用程式。
$
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 [demo]: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]:n
#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: ... 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 File with same data already exists at demo/da3c598813f1c2151579b73ad788cac8, skipping upload Deploying with following values =============================== Stack name : demo Region : us-west-2 Confirm changeset : False Disable rollback : False Deployment s3 bucket : aws-sam-cli-managed-default-samcliamzn-s3-demo-source-bucket-1a4x26zbcdkqr Capabilities : ["CAPABILITY_IAM"] Parameter overrides : {} Signing Profiles : {} Initiating deployment ===================== ... 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 ... ------------------------------------------------------------------------------------------------- Changeset created successfully. arn:aws:cloudformation:us-west-2:012345678910:changeSet/samcli-deploy1680906292/1164338d-72e7-4593-a372-f2b3e67f542f 2023-04-07 12:24:58 - 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 ... ------------------------------------------------------------------------------------------------- CloudFormation outputs from deployed stack ------------------------------------------------------------------------------------------------- Outputs ------------------------------------------------------------------------------------------------- Key HelloWorldFunctionIamRole Description Implicit IAM Role created for Hello World function Value arn:aws:iam::012345678910:role/demo-HelloWorldFunctionRole-VQ4CU7UY7S2K Key HelloWorldApi Description API Gateway endpoint URL for Prod stage for Hello World function Value https://satnon55e9.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:demo- HelloWorldFunction-G14inKTmSQvK ------------------------------------------------------------------------------------------------- Successfully created/updated stack - demo in us-west-2
在部署期間, AWS SAM CLI 會自動將 Lambda 函數程式碼上傳至 Amazon S3,並更新我們的範本。我們在 AWS CloudFormation 主控台中修改的範本會反映 Amazon S3 儲存貯體路徑。
AWSTemplateFormatVersion: '2010-09-09' Transform: AWS::Serverless-2016-10-31 ... Resources: HelloWorldFunction: Type: AWS::Serverless::Function Properties: CodeUri: s3://aws-sam-cli-managed-default-samcliamzn-s3-demo-source-bucket-1a4x26zbcdkqr/demo/da3c598813f1c2151579b73ad788cac8 Handler: app.lambda_handler ...
支援的使用案例
的 AWS SAM CLI 可以自動促進許多檔案類型、 AWS CloudFormation 資源類型和 AWS CloudFormation 巨集的此程序。
檔案類型
應用程式檔案和 Docker 支援 映像。
AWS CloudFormation 資源類型
以下是支援的資源類型及其屬性的清單:
資源 | 屬性 |
---|---|
AWS::ApiGateway::RestApi |
BodyS3Location |
AWS::ApiGatewayV2::Api |
BodyS3Location |
AWS::AppSync:FunctionConfiguration |
|
AWS::AppSync::GraphQLSchema |
DefinitionS3Location |
AWS::AppSync::Resolver |
|
AWS::CloudFormation::ModuleVersion |
ModulePackage |
AWS::CloudFormation::ResourceVersion |
SchemaHandlerPackage |
AWS::ECR::Repository |
RepositoryName |
AWS::ElasticBeanstalk::ApplicationVersion |
SourceBundle |
AWS::Glue::Job |
Command.ScriptLocation |
AWS::Lambda::Function |
|
AWS::Lambda::LayerVersion |
Content |
AWS::Serverless::Api |
DefinitionUri |
AWS::Serverless::Function |
|
AWS::Serverless::GraphQLApi |
|
AWS::Serverless::HttpApi |
DefinitionUri |
AWS::Serverless::LayerVersion |
ContentUri |
AWS::Serverless::StateMachine |
DefinitionUri |
AWS::StepFunctions::StateMachine |
DefinitionS3Location |
AWS CloudFormation 巨集
支援使用AWS::Include
轉換巨集參考的檔案。
進一步了解
若要進一步了解AWS::Include
轉換,請參閱 AWS CloudFormation 使用者指南中的 AWS::Include 轉換。
若要查看 AWS SAM 範本中使用AWS::Include
轉換的範例,請參閱 Serverless Land 的API閘道HTTPAPI到SQS