Trong bước này, chúng ta thực hiện một số bước chuẩn bị để tạo các REST API sau này.
Mở template.yaml trong thư mục fcj-book-shop.
Thêm đoạn mã sau vào cuối tệp.
Đầu tiên, chúng ta sẽ tạo các tham số apiType, binaryMediaType và getOrPostPathPart.
apiType:
Type: String
Default: REGIONAL
binaryMediaType:
Type: String
Default: multipart/form-data
getOrPostPathPart:
Type: String
Default: books
deletePathPart:
Type: String
Default: "{id}"
Tiếp theo, chúng ta sẽ tạo BookApi RestApi và BookApiResource Resource.
BookApi:
Type: AWS::ApiGateway::RestApi
Properties:
Name: fcj-serverless-api
EndpointConfiguration:
Types:
- !Ref apiType
BinaryMediaTypes:
- !Ref binaryMediaType
BookApiResource:
Type: AWS::ApiGateway::Resource
Properties:
RestApiId: !Ref BookApi
ParentId: !GetAtt BookApi.RootResourceId
PathPart: !Ref getOrPostPathPart
BookDeleteApiResource:
Type: AWS::ApiGateway::Resource
Properties:
RestApiId: !Ref BookApi
ParentId: !Ref BookApiResource
PathPart: !Ref deletePathPart
Chạy lệnh sau để triển khai SAM.
sam build
sam validate
sam deploy
Vậy là chúng ta đã hoàn thành một số bước chuẩn bị. Tiếp theo, chúng ta sẽ tạo các API GET, POST và DELETE.