Trong bước này, chúng ta sẽ thêm phương thức POST vào REST API để frontend có thể tạo sách mới thông qua hàm Lambda book_create.
Việc thêm một method mới yêu cầu tạo lại API deployment. Vì BookApiDeployment không thể được cập nhật trực tiếp trong khi một stage đang phụ thuộc vào nó, chúng ta phải thực hiện theo hai giai đoạn:
Mở tệp template.yaml trong thư mục fcaj-book-shop.
Comment out khối BookApiDeployment, và comment cả dòng DeploymentId trong BookApiStage, để CloudFormation có thể xóa deployment cũ một cách sạch sẽ:
# BookApiDeployment:
# Type: AWS::ApiGateway::Deployment
# Properties:
# RestApiId: !Ref BookApi
# DependsOn:
# - BookApiGet
BookApiStage:
Type: AWS::ApiGateway::Stage
Properties:
RestApiId: !Ref BookApi
StageName: !Ref stage
# DeploymentId: !Ref BookApiDeployment

Chạy các lệnh sau để validate, build và deploy:
sam validate
sam build

sam deploy
Changeset hiển thị:
BookApiStage (AWS::ApiGateway::Stage)BookApiDeployment (AWS::ApiGateway::Deployment)Nhập y để xác nhận.

Chờ quá trình triển khai hoàn tất.

Tại sao cần hai giai đoạn deploy?: BookApiDeployment của API Gateway là một snapshot của tất cả các method được đăng ký tại thời điểm tạo. Bạn không thể chỉ thêm method mới và cập nhật deployment hiện có — bạn cần tạo một deployment mới hoàn toàn để capture method mới. CloudFormation xử lý điều này bằng cách xóa deployment cũ trước (Giai đoạn 1), sau đó tạo mới bao gồm cả GET lẫn POST (Giai đoạn 2).
Thêm method BookApiCreate và permission BookApiCreateInvokePermission sau BookApiGetInvokePermission:
BookApiCreate:
Type: AWS::ApiGateway::Method
Properties:
HttpMethod: POST
RestApiId: !Ref BookApi
ResourceId: !Ref BookApiResource
AuthorizationType: NONE
Integration:
Type: AWS_PROXY
IntegrationHttpMethod: POST # For Lambda integrations, you must set the integration method to POST
Uri: !Sub >-
arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${BookCreate.Arn}/invocations
IntegrationResponses:
- StatusCode: "200"
ResponseParameters:
method.response.header.Access-Control-Allow-Origin: "'*'"
method.response.header.Access-Control-Allow-Methods: "'GET,POST,OPTIONS'"
method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
MethodResponses:
- StatusCode: "200"
ResponseParameters:
method.response.header.Access-Control-Allow-Origin: "'*'"
method.response.header.Access-Control-Allow-Methods: "'GET,POST,OPTIONS'"
method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
BookApiCreateInvokePermission:
Type: AWS::Lambda::Permission
Properties:
FunctionName: !Ref BookCreate
Action: lambda:InvokeFunction
Principal: apigateway.amazonaws.com
SourceAccount: !Ref "AWS::AccountId"

Bây giờ bỏ comment khối BookApiDeployment và dòng DeploymentId trong BookApiStage. Cập nhật DependsOn để bao gồm cả BookApiGet lẫn BookApiCreate:
BookApiDeployment:
Type: AWS::ApiGateway::Deployment
Properties:
RestApiId: !Ref BookApi
DependsOn:
- BookApiGet
- BookApiCreate
BookApiStage:
Type: AWS::ApiGateway::Stage
Properties:
RestApiId: !Ref BookApi
StageName: !Ref stage
DeploymentId: !Ref BookApiDeployment

Chạy lại các lệnh để build và deploy:
sam validate
sam build

sam deploy
Changeset hiển thị các tài nguyên mới:
BookApiCreate (AWS::ApiGateway::Method)BookApiDeployment (AWS::ApiGateway::Deployment)BookApiCreateInvokePermission (AWS::Lambda::Permission)BookApiStage (AWS::ApiGateway::Stage)Nhập y để xác nhận.

Chờ quá trình triển khai hoàn tất. Bạn sẽ thấy Successfully created/updated stack - fcaj-book-shop in ap-southeast-1.

Mở AWS API Gateway console. Nhấp vào fcaj-serverless-api REST API.

Tại trang fcaj-serverless-api:
/books.
Nhấp vào Stages trong menu bên trái.
/books.Ví dụ:
https://1bupjnr42d.execute-api.ap-southeast-1.amazonaws.com/staging/books

Invoke URL của GET và POST là giống nhau — cả hai đều trỏ đến …/staging/books. HTTP method (GET hay POST) trong request sẽ quyết định hàm Lambda nào được kích hoạt: GET gọi books_list, còn POST gọi book_create.