Create DELETE API

In this step, we will add the DELETE method to allow the frontend to delete books by ID via the book_delete Lambda function.

The DELETE endpoint uses a path parameter (/books/{id}), which is already defined by the BookDeleteApiResource resource. Like the previous POST step, adding a new method requires re-creating the API deployment in two phases.

We also add a BookApiDeleteOptions resource (MOCK integration) to support browser preflight CORS requests for this endpoint.

Phase 1 — Remove old Deployment

  1. Open the template.yaml file in the fcaj-book-shop folder.

  2. Comment out the BookApiDeployment block and the DeploymentId line inside BookApiStage:

    # BookApiDeployment:
    #   Type: AWS::ApiGateway::Deployment
    #   Properties:
    #     RestApiId: !Ref BookApi
    #   DependsOn:
    #     - BookApiGet
    #     - BookApiCreate
    
    BookApiStage:
      Type: AWS::ApiGateway::Stage
      Properties:
        RestApiId: !Ref BookApi
        StageName: !Ref stage
        # DeploymentId: !Ref BookApiDeployment
    

    Comment BookApiDeployment

  3. Run the commands to validate, build, and deploy:

    sam validate
    sam build
    

    SAM Validate & Build - Phase 1

    sam deploy
    

    The changeset shows:

    • Modify BookApiStage
    • Delete BookApiDeployment

    Enter y to confirm.

    SAM Deploy - Changeset Phase 1

  4. Wait for the deployment to complete.

    Deploy Complete - Phase 1

Phase 2 — Add DELETE Method and Redeploy

  1. Add the BookApiDelete method resource. Note that ResourceId references BookDeleteApiResource (the /{id} path), not BookApiResource:

    BookApiDelete:
      Type: AWS::ApiGateway::Method
      Properties:
        HttpMethod: DELETE
        RestApiId: !Ref BookApi
        ResourceId: !Ref BookDeleteApiResource
        AuthorizationType: NONE
        Integration:
          Type: AWS_PROXY
          IntegrationHttpMethod: POST # For Lambda integrations, you must set the integration method to POST
          Uri: !Sub >-
            arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${BookDelete.Arn}/invocations
        MethodResponses:
          - StatusCode: "200"
            ResponseParameters:
              method.response.header.Access-Control-Allow-Origin: true
              method.response.header.Access-Control-Allow-Methods: true
              method.response.header.Access-Control-Allow-Headers: true
    

    BookApiDelete resource

  2. Add BookApiDeleteOptions (MOCK integration for CORS preflight) and BookApiDeleteInvokePermission:

    BookApiDeleteOptions:
      Type: AWS::ApiGateway::Method
      Properties:
        HttpMethod: OPTIONS
        RestApiId: !Ref BookApi
        ResourceId: !Ref BookDeleteApiResource
        AuthorizationType: NONE
        Integration:
          Type: MOCK
          RequestTemplates:
            application/json: '{"statusCode": 200}'
          IntegrationResponses:
            - StatusCode: "200"
              ResponseParameters:
                method.response.header.Access-Control-Allow-Origin: "'*'"
                method.response.header.Access-Control-Allow-Methods: "'GET,POST,OPTIONS,DELETE'"
                method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
        MethodResponses:
          - StatusCode: "200"
            ResponseParameters:
              method.response.header.Access-Control-Allow-Origin: true
              method.response.header.Access-Control-Allow-Methods: true
              method.response.header.Access-Control-Allow-Headers: true
    
    BookApiDeleteInvokePermission:
      Type: AWS::Lambda::Permission
      Properties:
        FunctionName: !Ref BookDelete
        Action: lambda:InvokeFunction
        Principal: apigateway.amazonaws.com
        SourceAccount: !Ref "AWS::AccountId"
    

    BookApiDeleteOptions and BookApiDeleteInvokePermission

Why add an OPTIONS method?: Browsers send a preflight OPTIONS request before a cross-origin DELETE request (CORS policy). Without this MOCK integration, the browser will block the DELETE call entirely before it even reaches your Lambda. The BookApiDeleteOptions provides a static response that grants the browser permission to proceed.

  1. Uncomment the BookApiDeployment block and DeploymentId. Update DependsOn to include all three methodsBookApiGet, BookApiCreate, and BookApiDelete:

    BookApiDeployment:
      Type: AWS::ApiGateway::Deployment
      Properties:
        RestApiId: !Ref BookApi
      DependsOn:
        - BookApiGet
        - BookApiCreate
        - BookApiDelete
    
    BookApiStage:
      Type: AWS::ApiGateway::Stage
      Properties:
        RestApiId: !Ref BookApi
        StageName: !Ref stage
        DeploymentId: !Ref BookApiDeployment
    

    Uncomment BookApiDeployment with all three DependsOn

  2. Run the commands again to build and deploy:

    sam validate
    sam build
    

    SAM Validate & Build - Phase 2

    sam deploy
    

    The changeset shows new resources being added:

    • Add BookApiDelete (AWS::ApiGateway::Method)
    • Add BookApiDeleteOptions (AWS::ApiGateway::Method)
    • Add BookApiDeployment (AWS::ApiGateway::Deployment)
    • Add BookApiDeleteInvokePermission (AWS::Lambda::Permission)
    • Modify BookApiStage (AWS::ApiGateway::Stage)

    Enter y to confirm.

    SAM Deploy - Changeset Phase 2

  3. Wait for the deployment to complete. You should see Successfully created/updated stack - fcaj-book-shop in ap-southeast-1.

    Deploy Complete - Phase 2

Verify on AWS Console

  1. Open the AWS API Gateway console. Click on the fcaj-serverless-api REST API.

    API Gateway - fcaj-serverless-api

  2. At the fcaj-serverless-api page:

    • Click Resources in the left menu.
    • Expand /books/{id} → select DELETE.
    • Click the Lambda integration tooltip — confirm the integrated function is book_delete.

    Resources - DELETE /books/{id} - Lambda integration book_delete

  3. Click Stages in the left menu.

    • Expand staging/books/{id} → select DELETE.
    • Copy and save the Invoke URL.

    Example: https://1bupjnr42d.execute-api.ap-southeast-1.amazonaws.com/staging/books/{id}

    Stages - staging/DELETE /books/{id} - Invoke URL