Preparations

In this step, we do some preparations steps to create REST APIs later.

  1. Open template.yaml in fcj-book-shop folder.

  2. Add the following script at the end of the file.

    • Firstly, we will create apiType, binaryMediaType and getOrPostPathPart parameters.

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

      PrepRestApi

    • Next, we will create BookApi RestApi and 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. Run the following command to deploy SAM.

    sam build
    sam validate
    sam deploy
    

    PrepRestApi

  4. Open AWS API Gateway console.

    • Click fcj-serverless-api REST api. PrepRestApi
    • At fcj-serverless-api resources page.
      • Click Resources.
      • Select /books.
      • Check Resource details information. PrepRestApi
      • Select /{id}.
      • Check Resource details information. PrepRestApi
      • Click API settings.
      • Check multipart/form-data Media type at Binary media types. PrepRestApi

So we finish some preparation steps. Next, we will create GET, POST and DELETE api.