Create DELETE API

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

  2. Add the following script at the end of the file that creates the DELETE method.

    • Firstly, we need to refresh to create a new deployment version for POST Api in a few next steps. Comment BookApiDeployment block.

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

      CreateDeleteAPI

    • Run the following command to deploy SAM.

      sam build
      sam validate
      sam deploy
      

      CreateDeleteAPI

    • Next, we will create BookApiDelete and BookApiDeleteInvokePermission.

      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
      
      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"
      

      CreateDeleteAPI

    • Then, we uncomment the codeblock that we commented above.

      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
      

      CreateDeleteAPI

  3. Run the following command to deploy SAM.

    sam build
    sam validate
    sam deploy
    

    CreateDeleteAPI

  4. Open AWS API Gateway console.

    • Click fcj-serverless-api REST api. CreateDeleteAPI
    • At fcj-serverless-api resources page.
      • Click Resources.
      • Select DELETE.
      • Click Lambda integration and check the book_delete function. CreateDeleteAPI
      • Click Stages.
      • Select DELETE.
      • Copy and save the Invoke URL. CreateDeleteAPI