Creating a REST API with AWS CLI commands

Let's first create the REST API by using AWS CLI commands. We will not show how to use the commands that we already discussed in previous recipes. However, the complete commands will be available with the code files:

  1. Create a REST API with the name API Gateway With Lambda, using the apigateway sub-command create-rest-api.
  2. Get the parent resource ID passing the REST API by using the apigateway sub-command get-resources.
  3. Create a resource, lambdagreeting, under the root resource (/) by using the sub-command create-resource.
  4. Create a path parameter, {name}, under the parent resource lambdagreeting, using the apigateway sub-command create-resource.
  5. Create an http-method GET with the authorization type 'NONE', using the apigateway sub-command put method. Although it is not required for this recipe, we will add the property request-parameters, in order to specify that the path parameter is required:
aws apigateway put-method 
--rest-api-id 19sh9qhri2
--resource-id n9iev1
--http-method GET
--authorization-type "NONE"
--request-parameters method.request.path.name=true
--region us-east-1
--profile admin
  1. Set up a response status code of 200 for the http-method GET, using the apigateway sub-command put-method-response.

  2. Set up an AWS integration, as follows:
aws apigateway put-integration 
--rest-api-id 19sh9qhri2
--resource-id n9iev1
--http-method GET
--type AWS
--integration-http-method POST
--uri 'arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:<account_id>:function:lambda-for-api-gateway/invocations'
--request-templates '{"application/json": "{"name": "$input.params('"'"'name'"'"')" , "time": "$input.params('"'"'time'"'"')"}"}'
--region us-east-1
--profile admin
The URI format for AWS Lambda integration is arn:aws:apigateway:<region>:lambda:path/2015-03-31/functions/<lambda arn>/invocations.

The integration-http-method (for the API request to Lambda) is POST, but http-method (for the client/browser request to the API) is GETWe have also defined the request-templates option, to specify a mapping template for the JSON sent to Lambda. 

  1. Define the integration response with a selection-pattern, "", but without the response-templates option:
aws apigateway put-integration-response 
--rest-api-id 19sh9qhri2
--resource-id n9iev1
--http-method GET
--status-code 200
--region us-east-1
--selection-pattern ""
--profile admin
We are no longer using the response-templates option to specify a dummy response, as the response is generated at the backend Lambda.
  1. Deploy our Lambda to a stage, dev1, by using the apigateway sub-command create-deployment.
  1. Give a permission for the API to invoke the lambda, as follows:
aws lambda add-permission 
--function-name lambda-for-api-gateway
--statement-id apigateway-st-1
--action lambda:InvokeFunction
--principal apigateway.amazonaws.com
--source-arn "arn:aws:execute-api:us-east-1:<account_id>:tyu4dw36th/dev/GET/lambdagreeting/{name}"
--profile admin
  1. Invoke the API from a browser with a path param value (Heartin):
https://tyu4dw36th.execute-api.us-east-1.amazonaws.com/dev/lambdagreeting/Heartin
Remember to replace the REST API ID (tyu4dw36thwith your REST API ID. 

The preceding URL should show the following output:

  1. Invoke the API from a browser with path and query params, as follows:
https://tyu4dw36th.execute-api.us-east-1.amazonaws.com/dev1/lambdagreeting/Heartin?time=Morning

The preceding code will provide the following output:

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

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