Chuẩn bị

Trong bước này, chúng ta thêm các tham số cần thiết và định nghĩa tài nguyên API Gateway vào template.yaml, sau đó triển khai SAM stack để khung REST API sẵn sàng trước khi gắn các HTTP method cụ thể ở các phần tiếp theo.

1. Thêm Parameters vào template.yaml

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

  2. Trong phần Parameters:, thêm bốn tham số mới như bên dưới. Các giá trị này sẽ được tham chiếu bởi các tài nguyên API Gateway mà chúng ta tạo tiếp theo.

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

    VSCode - template.yaml: khối Parameters mới được highlight

2. Thêm tài nguyên API Gateway

  1. Trong phần Resources: (bên dưới các tài nguyên Lambda và S3 hiện có), thêm ba tài nguyên sau:

    • BookApi — tạo REST API tên fcj-serverless-api với endpoint Regional và hỗ trợ binary media type multipart/form-data.
    • BookApiResource — tạo path resource /books dưới root của API.
    • BookDeleteApiResource — tạo path resource /{id} dưới /books, dùng cho các thao tác DELETE.
    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
    

    VSCode - template.yaml: BookApi, BookApiResource, BookDeleteApiResource được highlight

3. Build và Deploy

  1. Chạy các lệnh sau trong terminal để validate, build và deploy stack đã cập nhật:

    sam validate
    sam build
    sam deploy
    
    • sam validate kiểm tra template có lỗi cú pháp không.
    • sam build đóng gói các Lambda function và giải quyết dependencies.

    Terminal - sam validate + sam build: Build Succeeded

    • sam deploy tải lên template và hiển thị CloudFormation changeset. Xác nhận bằng y khi được hỏi.
    • Changeset sẽ thêm ba tài nguyên mới: BookApi, BookApiResourceBookDeleteApiResource.

    Terminal - sam deploy: changeset hiển thị + Add BookApi, BookApiResource, BookDeleteApiResource

    • Chờ CloudFormation events đạt trạng thái UPDATE_COMPLETE. Output sẽ hiển thị “Successfully created/updated stack - fcaj-book-shop in ap-southeast-1”.

    Terminal - CloudFormation events: CREATE_COMPLETE cho BookApi resources, UPDATE_COMPLETE cho stack

4. Xác minh trên API Gateway Console

  1. Mở Amazon API Gateway console.

    • Bạn sẽ thấy fcaj-serverless-api được liệt kê là REST API (Regional endpoint).

    API Gateway - Danh sách APIs: fcaj-serverless-api (REST, Regional)

  2. Nhấp vào fcaj-serverless-apiResources trong thanh sidebar bên trái.

    • Chọn /books trong cây tài nguyên.
    • Xác nhận panel Resource details hiển thị Path: /books.

    API Gateway - Resources: /books được chọn, Path=/books, Resource ID=lbj98g

    • Chọn /{id} trong cây tài nguyên.
    • Xác nhận panel Resource details hiển thị Path: /books/{id}.

    API Gateway - Resources: /{id} được chọn, Path=/books/{id}, Resource ID=reg5nx

  3. Nhấp vào API settings trong thanh sidebar bên trái.

    • Trong phần Binary media types, xác nhận multipart/form-data được liệt kê. Điều này là cần thiết để tải ảnh qua POST API.

    API Gateway - API settings: Binary media types = multipart/form-data

Các bước chuẩn bị đã hoàn tất. Khung API (/books/books/{id}) đã được triển khai. Trong các phần tiếp theo, chúng ta sẽ gắn các method GET, POST và DELETE vào các resource này.