Create POST API

In this step, we will add the POST method to our REST API so that the frontend can create new books via the book_create Lambda function.

Adding a new method requires re-creating the API deployment. Since BookApiDeployment cannot be updated in-place while a stage depends on it, we must do this in two phases:

  • Phase 1: Comment out the deployment, deploy to detach the stage from it, letting CloudFormation delete the old deployment.
  • Phase 2: Add the POST method, uncomment the deployment (now with both GET + POST as dependencies), and deploy again.

Phase 1 — Remove old Deployment

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

  2. Comment out the BookApiDeployment block, and also comment the DeploymentId line inside BookApiStage, so CloudFormation can delete the old deployment cleanly:

    # 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
    

    Comment BookApiDeployment

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

    sam validate
    sam build
    

    SAM Validate & Build - Phase 1

    sam deploy
    

    The changeset shows:

    • Modify BookApiStage (AWS::ApiGateway::Stage)
    • Delete BookApiDeployment (AWS::ApiGateway::Deployment)

    Enter y to confirm.

    SAM Deploy - Changeset Phase 1

  4. Wait for the deployment to complete.

    Deploy Complete - Phase 1

Why do we need two deploy phases?: API Gateway’s BookApiDeployment is a snapshot of all the methods registered at the time of creation. You cannot simply add a new method and update an existing deployment — you need to create a brand-new deployment that captures the new method. CloudFormation handles this by deleting the old deployment first (Phase 1), then creating a new one that includes both GET and POST (Phase 2).

Phase 2 — Add POST Method and Redeploy

  1. Add the BookApiCreate method and its invoke permission resource after BookApiGetInvokePermission:

    BookApiCreate:
      Type: AWS::ApiGateway::Method
      Properties:
        HttpMethod: POST
        RestApiId: !Ref BookApi
        ResourceId: !Ref BookApiResource
        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/${BookCreate.Arn}/invocations
          IntegrationResponses:
            - StatusCode: "200"
              ResponseParameters:
                method.response.header.Access-Control-Allow-Origin: "'*'"
                method.response.header.Access-Control-Allow-Methods: "'GET,POST,OPTIONS'"
                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: "'*'"
              method.response.header.Access-Control-Allow-Methods: "'GET,POST,OPTIONS'"
              method.response.header.Access-Control-Allow-Headers: "'Content-Type,X-Amz-Date,Authorization,X-Api-Key,X-Amz-Security-Token'"
    
    BookApiCreateInvokePermission:
      Type: AWS::Lambda::Permission
      Properties:
        FunctionName: !Ref BookCreate
        Action: lambda:InvokeFunction
        Principal: apigateway.amazonaws.com
        SourceAccount: !Ref "AWS::AccountId"
    

    BookApiCreate and BookApiCreateInvokePermission

  2. Now uncomment the BookApiDeployment block and the DeploymentId inside BookApiStage. Update DependsOn to include both BookApiGet and BookApiCreate:

    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
    

    Uncomment BookApiDeployment with both DependsOn

  3. Run the following 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 BookApiCreate (AWS::ApiGateway::Method)
    • Add BookApiDeployment (AWS::ApiGateway::Deployment)
    • Add BookApiCreateInvokePermission (AWS::Lambda::Permission)
    • Modify BookApiStage (AWS::ApiGateway::Stage)

    Enter y to confirm.

    SAM Deploy - Changeset Phase 2

  4. 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.
    • Select POST under the /books resource.
    • Click the Lambda integration tooltip — confirm the integrated function is book_create.

    Resources - POST /books - Lambda integration book_create

  3. Click Stages in the left menu.

    • Expand staging/books → select POST.
    • Copy and save the Invoke URL — this is the same base URL as the GET endpoint, since both methods share the same /books resource path.

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

    Stages - staging/POST - Invoke URL

The GET and POST Invoke URLs are the same — both point to …/staging/books. The HTTP method (GET vs POST) in the request determines which Lambda function is triggered: GET calls books_list, while POST calls book_create.