Chuẩn bị

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.

  1. Mở template.yaml trong thư mục fcj-book-shop.

  2. 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, binaryMediaTypegetOrPostPathPart.

      apiType:
        Type: String
        Default: REGIONAL
      
      binaryMediaType:
        Type: String
        Default: multipart/form-data
      
      getOrPostPathPart:
        Type: String
        Default: books
      
      deletePathPart:
        Type: String
        Default: "{id}"
      

      PrepRestApi

    • 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
      

      PrepRestApi

  3. Chạy lệnh sau để triển khai SAM.

    sam build
    sam validate
    sam deploy
    

    PrepRestApi

  4. Mở AWS API Gateway console.

    • Nhấp vào fcj-serverless-api REST api. PrepRestApi
    • Tại trang tài nguyên fcj-serverless-api.
      • Nhấp vào Resources.
      • Chọn /books.
      • Kiểm tra thông tin Resource details. PrepRestApi
      • Chọn /{id}.
      • Kiểm tra thông tin Resource details. PrepRestApi
      • Nhấp vào API settings.
      • Kiểm tra loại phương tiện multipart/form-data tại Binary media types. PrepRestApi

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.