How to do it...

We will create our first REST API with API gateway by using CLI commands. Remember to replace the various IDs (rest-api-id, parent-id, and so on) in each command with your own, based on the previous step's result. As you will see in the next recipe, with CloudFormation templates, we do not have to manually specify IDs, unlike with the CLI commands:

  1. Create a REST API in the us-east-1 region, as follows:
aws apigateway create-rest-api 
--name 'API Gateway Hello World'
--region us-east-1
--profile admin
The ID of the REST API from the response is used as the value for rest-api-id in later commands. Replace c82tpsb7ka in rest of the commands with the ID of the REST API you receive in this step.
  1. Retrieve the ID of the root resource (/) of the REST API:
aws apigateway get-resources 
--rest-api-id c82tpsb7ka
--region us-east-1
--profile admin
The ID of the root resource (/) from the response is used as the value for the parent-id of the sub-resource.
  1. Create a child resource under the root resource (/):
aws apigateway create-resource 
--rest-api-id c82tpsb7ka
--region us-east-1
--parent-id rosgmsjlb5
--path-part greeting
--profile admin
The ID of the new resource (greeting) from the response is used as the parent-id for the path param resource that we will create next. replace rosgmsjlb5 in next command with the ID of the resource you receive in this step.
  1. Create a path param called '{name}' under the parent resource greeting, as follows:
aws apigateway create-resource 
--rest-api-id c82tpsb7ka
--region us-east-1
--parent-id oaslzo
--path-part '{name}'
--profile admin

The ID of the new resource from the response is used as the resource-id for later commands.  Replace oaslzo in next command with the ID of the resource you receive in this step.
  1. Add an HTTP method, GET, on the resource '{name}':
aws apigateway put-method 
--rest-api-id c82tpsb7ka
--resource-id lyb17y
--http-method GET
--authorization-type "NONE"
--region us-east-1
--profile admin
We set the authorization-type as "NONE", so that everyone can access the API. We will look at authorization in a different chapter.
  1. Set up a response status code of 200, as follows:
aws apigateway put-method-response 
--rest-api-id c82tpsb7ka
--resource-id lyb17y
--http-method GET
--status-code 200
--region us-east-1
--profile admin
  1. Set up a MOCK integration, as follows:
aws apigateway put-integration 
--rest-api-id c82tpsb7ka
--resource-id lyb17y
--http-method GET
--type MOCK
--integration-http-method GET
--request-templates '{ "application/json": "{"statusCode": 200}" }'
--region us-east-1
--profile admin
The request-templates parameter hold a map where key is the content-type sent by the caller and value is a velocity template. When specifying a velocity template on the command line, it may have to be escaped as per operating system and the terminal used. This applies to other commands within this chapter that accept a velocity template such as the response-templates property.

Code examples within the book follows the AWS documentation style and is tested primarily on Mac operating system. It should also work on most Unix based operating systems such as Linux. For alternative solutions you may refer to the code files repository. Please refer to the heading Alternative Solutions in the repository's readme file for more details.
  1. Create a custom integration response by using the path param value, as follows:
aws apigateway put-integration-response 
--rest-api-id c82tpsb7ka
--resource-id lyb17y
--http-method GET
--status-code 200
--selection-pattern ""
--response-templates '{"application/json": "{"message": "Hello $input.params('"'"'name'"'"')"}"}'
--region us-east-1
--profile admin
If you are using a Windows Machine, please refer to the code files repository for alternative solutions where velocity template is escaped properly so that it can be executed on a Windows machine as well.
  1. Deploy our API into a stage called dev, as follows:
aws apigateway create-deployment 
--rest-api-id c82tpsb7ka
--region us-east-1
--stage-name dev
--stage-description 'Dev stage'
--description 'First deployment'
--profile admin
  1. Test the API by invoking the API URL (as follows) after replacing the rest-api-id (c82tpsb7ka) with your REST API ID:

https://c82tpsb7ka.execute-api.us-east-1.amazonaws.com/dev/greeting/Heartin

You should get a response that looks as follows:

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

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