Creating the Proxy API with CLI commands

First, let's 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 in the code files:

  1. Create a REST API in API Gateway, using the apigateway sub-command create-rest-api.
  2. Get the root resource (/) of our API, using the apigateway sub-command get-resources.
  3. Add a greedy path param, '{proxy+}', for the proxy resource:
aws apigateway create-resource 
--rest-api-id qacob6w4v7
--region us-east-1
--parent-id xitaiyjnuf
--path-part '{proxy+}'
--profile admin
This resource will match any sub-resources for the parent /.
  1. Use the ANY method over the proxy resource, in order to match any HTTP method, as follows:
aws apigateway put-method 
--rest-api-id qacob6w4v7
--resource-id k7zima
--http-method ANY
--authorization-type "NONE"
--region us-east-1
--profile admin
  1. Add a response code for the ANY method, as follows:
aws apigateway put-method-response 
--rest-api-id qacob6w4v7
--resource-id k7zima
--http-method ANY
--status-code 200
--region us-east-1
--profile admin
  1. Execute put-integration with the AWS_PROXY integration type, as follows:
aws apigateway put-integration 
--rest-api-id qacob6w4v7
--resource-id k7zima
--http-method ANY
--type AWS_PROXY
--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-proxy-integration/invocations'
--region us-east-1
--profile admin
  1. Execute put-integration-response for the ANY method, as follows:
aws apigateway put-integration-response 
--rest-api-id qacob6w4v7
--resource-id k7zima
--http-method ANY
--status-code 200
--region us-east-1
--selection-pattern ""
--profile admin
  1. Create a deployment with the dev stage, using the apigateway sub-command create-deployment.
  2. Give permission for the API to invoke the Lambda, as follows:
aws lambda add-permission 
--function-name lambda-for-proxy-integration
--statement-id apigateway-st-3
--action lambda:InvokeFunction
--principal apigateway.amazonaws.com
--source-arn "arn:aws:execute-api:us-east-1:<account_id>:qacob6w4v7/*/*/{proxy+}"
--profile admin
  1. Execute the API from a REST client (for example, Postman), as shown in the following screenshot:

You should get a response similar to the following:

I sent a POST request from the Postman client, but a GET request from a browser will also work in this case, as the API's proxy resource has an ANY method, which can accept any HTTP method. Also, remember to replace the IDs with your own.

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

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