Create GET API

  1. Open template.yaml in fcj-book-shop folder
  2. Add the following script at the end of the file creating a REST API and GET method
      BookApi:
        Type: AWS::Serverless::Api
        Name: fcj-serverless-api
        Properties:
          StageName: staging
          Cors: "'*'"      # enable CORS for API
          DefinitionBody:
            openapi: 3.0.1
            info:
              description: "This is the APIs for book shop web app"
              version: "1.0.0"
              title: "API Gateway REST API to Lambda"
            paths:
              /books:
                get:
                  responses:
                    "200":
                      description: 200 response
                      headers:
                        Access-Control-Allow-Origin:
                          type: string
                  x-amazon-apigateway-integration:
                    uri:
                      Fn::Sub: "arn:aws:apigateway:${AWS::Region}:lambda:path/2015-03-31/functions/${BooksList.Arn}/invocations"
                    responses:
                      default:
                        statusCode: 200
                        responseParameters:
                          method.response.header.Access-Control-Allow-Origin: "'*'"
                    passthroughBehavior: when_no_match
                    httpMethod: POST #always POST
                    type: aws_proxy
    
    CreateGetAPI
  • Add the following script at the end of the BooksList function
          Events:
            ListBook:
              Type: Api
              Properties:
                Path: /books/
                Method: get
                RestApiId:
                  Ref: BookApi
    
    CreateGetAPI
  1. Run the following command to deploy SAM

    sam build
    sam deploy
    

    CreateGetAPI

    Enter “y” if asked “BooksList may not have authorization defined, Is this okay? [y/N]: "

  2. Open Lambda console, click books_list function

    • Click API Gateway CreateGetAPI
  3. Show API Gateway being interacted with function

    • Click this API Gateway CreateGetAPI
  4. Display resources and GET method CreateGetAPI

  5. Click Stages on the left menu

    • Click staging
    • Click GET
    • Record InvokeURL of GET method CreateGetAPI