Preparations

In this step, we add the necessary parameters and API Gateway resource definitions to template.yaml, then deploy the SAM stack so that the REST API skeleton is ready before we attach individual HTTP methods in the following sections.

1. Add Parameters to template.yaml

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

  2. Inside the Parameters: section, append the four new parameters shown below. These values will be referenced by the API Gateway resources we create next.

    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: new Parameters block highlighted

2. Add API Gateway Resources

  1. Inside the Resources: section (below the existing Lambda and S3 resources), add the following three resources:

    • BookApi — creates the REST API named fcj-serverless-api with a Regional endpoint and multipart/form-data binary media type support.
    • BookApiResource — creates the /books path resource under the API root.
    • BookDeleteApiResource — creates the /{id} path resource under /books, used for DELETE operations.
    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 highlighted

3. Build and Deploy

  1. Run the following commands in your terminal to validate, build, and deploy the updated stack:

    sam validate
    sam build
    sam deploy
    
    • sam validate checks the template for syntax errors.
    • sam build packages the Lambda functions and resolves dependencies.

    Terminal - sam validate + sam build: Build Succeeded

    • sam deploy uploads the template and shows the CloudFormation changeset. Confirm with y when prompted.
    • The changeset will add three new resources: BookApi, BookApiResource, and BookDeleteApiResource.

    Terminal - sam deploy: changeset showing + Add BookApi, BookApiResource, BookDeleteApiResource

    • Wait for the CloudFormation events to reach UPDATE_COMPLETE. The output should read “Successfully created/updated stack - fcaj-book-shop in ap-southeast-1”.

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

4. Verify in the API Gateway Console

  1. Open the Amazon API Gateway console.

    • You should see fcaj-serverless-api listed as a REST API (Regional endpoint).

    API Gateway - APIs list: fcaj-serverless-api (REST, Regional)

  2. Click fcaj-serverless-apiResources in the left sidebar.

    • Select /books in the resource tree.
    • Verify the Resource details panel shows Path: /books.

    API Gateway - Resources: /books selected, Path=/books, Resource ID=lbj98g

    • Select /{id} in the resource tree.
    • Verify the Resource details panel shows Path: /books/{id}.

    API Gateway - Resources: /{id} selected, Path=/books/{id}, Resource ID=reg5nx

  3. Click API settings in the left sidebar.

    • Under Binary media types, confirm that multipart/form-data is listed. This is required for image file uploads via the POST API.

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

The preparation steps are complete. The API skeleton (/books and /books/{id} resources) is now deployed. In the next sections, we will attach GET, POST, and DELETE methods to these resources.