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:
Open the template.yaml file in the fcaj-book-shop folder.
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

Run the following commands to validate, build, and deploy:
sam validate
sam build

sam deploy
The changeset shows:
BookApiStage (AWS::ApiGateway::Stage)BookApiDeployment (AWS::ApiGateway::Deployment)Enter y to confirm.

Wait for the deployment to complete.

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

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

Run the following commands again to build and deploy:
sam validate
sam build

sam deploy
The changeset shows new resources being added:
BookApiCreate (AWS::ApiGateway::Method)BookApiDeployment (AWS::ApiGateway::Deployment)BookApiCreateInvokePermission (AWS::Lambda::Permission)BookApiStage (AWS::ApiGateway::Stage)Enter y to confirm.

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

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

At the fcaj-serverless-api page:
/books resource.
Click Stages in the left menu.
/books resource path.Example:
https://1bupjnr42d.execute-api.ap-southeast-1.amazonaws.com/staging/books

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.