SAM 部署

SAM 提供了 deployment 的指令,並且在 deployment 的 options 中有一個 guided,可以透過回答問題的方式,幫助另一次部署的人快速部署。記得在 deploy 之前,要先執行過 sam build

另外 SAM 使用的是 AWS CLI 的 Credentials,所以要先確認這些 Credential 是可以使用的。

 1$ sam deploy --guided
 2
 3Configuring SAM deploy
 4======================
 5
 6	Looking for config file [samconfig.toml] :  Found
 7	Reading default arguments  :  Success
 8
 9	Setting default arguments for 'sam deploy'
10	=========================================
11	Stack Name [sam-app]:
12	AWS Region [ap-northeast-1]:
13	#Shows you resources changes to be deployed and require a 'Y' to initiate deploy
14	Confirm changes before deploy [Y/n]: Y
15	#SAM needs permission to be able to create roles to connect to the resources in your template
16	Allow SAM CLI IAM role creation [Y/n]: Y
17	#Preserves the state of previously provisioned resources when an operation fails
18	Disable rollback [y/N]: y
19	HelloWorldFunction has no authentication. Is this okay? [y/N]: y
20	Save arguments to configuration file [Y/n]: Y
21	SAM configuration file [samconfig.toml]:
22	SAM configuration environment [default]:
23
24	Looking for resources needed for deployment:
25
26	Managed S3 bucket: aws-sam-cli-managed-default-samclisourcebucket-1syrjc2soglaa
27	A different default S3 bucket can be set in samconfig.toml and auto resolution of buckets turned off by setting resolve_s3=False
28
29        Parameter "stack_name=sam-app" in [default.deploy.parameters] is defined as a global parameter [default.global.parameters].
30        This parameter will be only saved under [default.global.parameters] in /Users/allen/sam-app/samconfig.toml.
31
32	Saved arguments to config file
33	Running 'sam deploy' for future deployments will use the parameters saved above.
34	The above parameters can be changed by modifying samconfig.toml
35	Learn more about samconfig.toml syntax at
36	https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-config.html
37
38	Uploading to sam-app/73ed78d2180973be557e5b20ff3918f7  598152 / 598152  (100.00%)
39
40	Deploying with following values
41	===============================
42	Stack name                   : sam-app
43	Region                       : ap-northeast-1
44	Confirm changeset            : True
45	Disable rollback             : True
46	Deployment s3 bucket         : aws-sam-cli-managed-default-samclisourcebucket-1syrjc2soglaa
47	Capabilities                 : ["CAPABILITY_IAM"]
48	Parameter overrides          : {}
49	Signing Profiles             : {}
50
51Initiating deployment
52=====================
53
54	Uploading to sam-app/fced19bb73698b56db25505260c97e96.template  1273 / 1273  (100.00%)
55
56
57Waiting for changeset to be created..
58
59CloudFormation stack changeset
60---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
61Operation                                          LogicalResourceId                                  ResourceType                                       Replacement
62---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
63+ Add                                              HelloWorldFunctionHelloWorldPermissionProd         AWS::Lambda::Permission                            N/A
64+ Add                                              HelloWorldFunctionRole                             AWS::IAM::Role                                     N/A
65+ Add                                              HelloWorldFunction                                 AWS::Lambda::Function                              N/A
66+ Add                                              ServerlessRestApiDeployment47fc2d5f9d              AWS::ApiGateway::Deployment                        N/A
67+ Add                                              ServerlessRestApiProdStage                         AWS::ApiGateway::Stage                             N/A
68+ Add                                              ServerlessRestApi                                  AWS::ApiGateway::RestApi                           N/A
69---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
70
71
72Changeset created successfully. arn:aws:cloudformation:ap-northeast-1:129824596365:changeSet/samcli-deploy1711380173/7c5b81e7-8642-4028-9362-80eb6fbe608d
73
74
75Previewing CloudFormation changeset before deployment
76======================================================
77Deploy this changeset? [y/N]:

在 Line 14 時,我們有勾選需要先審核過 Changeset 才可以繼續部署,所以在初始化一個 Stack後,CloudFormation 會顯示這次的部署會修改什麼 (line 59-69)。前面有加號代表是這次部署新增的,而減號的話代表是刪除的。由於這次第一次部署,所以所有的資源肯定都是新增的。

而在 AWS CloudFormation UI 上,也可以看到艾倫的 Stack 卡在 REVIEW_IN_PROGRESS

cloudformation_review

當我們同意部署後,就可以看到 CloudFormation 正在部署,並且在最後會印出,我們在 template.yaml 所定義的 output (line 32 - 47)

 1Deploy this changeset? [y/N]: y
 2
 32023-09-15 23:33:09 - Waiting for stack create/update to complete
 4
 5CloudFormation events from stack operations (refresh every 5.0 seconds)
 6---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 7ResourceStatus                                     ResourceType                                       LogicalResourceId                                  ResourceStatusReason
 8---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 9CREATE_IN_PROGRESS                                 AWS::CloudFormation::Stack                         sam-app                                            User Initiated
10CREATE_IN_PROGRESS                                 AWS::IAM::Role                                     HelloWorldFunctionRole                             -
11CREATE_IN_PROGRESS                                 AWS::IAM::Role                                     HelloWorldFunctionRole                             Resource creation Initiated
12CREATE_COMPLETE                                    AWS::IAM::Role                                     HelloWorldFunctionRole                             -
13CREATE_IN_PROGRESS                                 AWS::Lambda::Function                              HelloWorldFunction                                 -
14CREATE_IN_PROGRESS                                 AWS::Lambda::Function                              HelloWorldFunction                                 Resource creation Initiated
15CREATE_IN_PROGRESS                                 AWS::Lambda::Function                              HelloWorldFunction                                 Eventual consistency check initiated
16CREATE_IN_PROGRESS                                 AWS::ApiGateway::RestApi                           ServerlessRestApi                                  -
17CREATE_IN_PROGRESS                                 AWS::ApiGateway::RestApi                           ServerlessRestApi                                  Resource creation Initiated
18CREATE_COMPLETE                                    AWS::ApiGateway::RestApi                           ServerlessRestApi                                  -
19CREATE_IN_PROGRESS                                 AWS::ApiGateway::Deployment                        ServerlessRestApiDeployment47fc2d5f9d              -
20CREATE_IN_PROGRESS                                 AWS::Lambda::Permission                            HelloWorldFunctionHelloWorldPermissionProd         -
21CREATE_IN_PROGRESS                                 AWS::Lambda::Permission                            HelloWorldFunctionHelloWorldPermissionProd         Resource creation Initiated
22CREATE_IN_PROGRESS                                 AWS::ApiGateway::Deployment                        ServerlessRestApiDeployment47fc2d5f9d              Resource creation Initiated
23CREATE_COMPLETE                                    AWS::Lambda::Function                              HelloWorldFunction                                 -
24CREATE_COMPLETE                                    AWS::Lambda::Permission                            HelloWorldFunctionHelloWorldPermissionProd         -
25CREATE_COMPLETE                                    AWS::ApiGateway::Deployment                        ServerlessRestApiDeployment47fc2d5f9d              -
26CREATE_IN_PROGRESS                                 AWS::ApiGateway::Stage                             ServerlessRestApiProdStage                         -
27CREATE_IN_PROGRESS                                 AWS::ApiGateway::Stage                             ServerlessRestApiProdStage                         Resource creation Initiated
28CREATE_COMPLETE                                    AWS::ApiGateway::Stage                             ServerlessRestApiProdStage                         -
29CREATE_COMPLETE                                    AWS::CloudFormation::Stack                         sam-app                                            -
30---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
31
32CloudFormation outputs from deployed stack
33---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
34Outputs
35---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
36Key                 HelloWorldFunctionIamRole
37Description         Implicit IAM Role created for Hello World function
38Value               arn:aws:iam::129824596365:role/sam-app-HelloWorldFunctionRole-WOf5YCP85EX0
39
40Key                 HelloWorldApi
41Description         API Gateway endpoint URL for Prod stage for Hello World function
42Value               https://t3faydf1rb.execute-api.ap-northeast-1.amazonaws.com/Prod/hello/
43
44Key                 HelloWorldFunction
45Description         Hello World Lambda Function ARN
46Value               arn:aws:lambda:ap-northeast-1:129824596365:function:sam-app-HelloWorldFunction-Qiut5gK4QT6I
47---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
48
49
50Successfully created/updated stack - sam-app in ap-northeast-1

cloudformation_complete

建立的 Resource

API Gateway

在 API Gateway 的頁面,我們可以看到 sam-demo 已經被建立好了

apigateway

點入 sam-demo 以後,在 resource 可以看到我們定義好的 Http method Get 的 Hello,並且後面已經串接好 Lambda Function

apigateway2

Lambda Function

在 Lambda 頁面,也以樣可以看到我們剛剛部署好的 sam-app-HelloWorldFunction-Qiut5gK4QT6I lambda

點入到 Lambda ,可以看到 Code Source Section,從左邊的 Folder 我們可以看到環境中有根據Package.json 所裝好的 Library node_modules。也可以同時看到我們的 Lambda Function Soruce code。

lambda2