How it works...

First, we will explain the AWS CLI API gateway sub-commands used in this recipe, as follows:

  • The create-rest-api command creates a REST API. We also need to specify a name and a region. An API setup with API gateway is region-specific. 
  • The get-resources command retrieves the root resource identifier of the REST API, which is the path /. You need the ID of this resource to create sub-resources.
  • The create-resource command appends a child resource under a parent resource. We created a child, greeting, under the path /, and then created a child for the path param '{name}' under the path greeting, using the corresponding parent IDs. The path-part parameter specifies the path of the resource to add (for example, greeting, {name}).
  • The put-method command specifies the HTTP verb. We added a GET verb on our resource 'greeting/{name}'. The authorization-type specified was 'NONE', so everyone can access the resource. You could use IAM roles and policies, a custom Lambda authorizer, or an Amazon Cognito user pool instead. There is a chapter dedicated to security.
  • The put-method-response command is used to set up the method response. We set up a response status code of 200 for successful GET requests. If you do not set up a method response and test, you will get an error, as the output mapping refers to an invalid method response.
  • The put-integration command sets up an integration for the API. The following integration types are currently supported: AWS, AWS_PROXY, HTTP, HTTP_PROXY, and MOCKWe need to specify the request-templates property map, where the key is a content-type header (for example, application/json) and the value is a velocity template that will be applied to a request payload with that particular content-type header. 
  • The put-integration-response command represents a put integration. The status-code property is used to map the integration response to an existing method response (specified by the put-method-response). The response-templates specifies the put integration response's templates. We used $input.params to use the value of the path param. 
  • The create-deployment command deploys our API to a stage. Stages allow you to manage different versions of the API (for example, dev, test, prod, and so on). The endpoint URL of the generated API will be of the format https://<rest-api-id>.execute-api.<region>.amazonaws.com/<stage>/<path>.

Having a decent understanding of the HTTP and REST basics can help you to design good REST APIs. If you are familiar with the HTTP and REST basics, you can skip the remainder of this section.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.218.157