Creating the API using 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 by using the apigateway sub-command create-rest-api.
  2. Get the root resource (/) of our API by using the apigateway sub-command get-resources.
  3. Create our path-partlambdagreeting, by using the apigateway sub-command create-resource.
  4. Create a POST method by using the apigateway sub-command put-method.
  5. Set a response status code for our POST method by using the apigateway sub-command put-method-response.
  6. Execute the aws apigateway put-integration command with the request mapping template, as follows:
aws apigateway put-integration 
--rest-api-id y3yftanqp7
--resource-id e4w7ka
--http-method POST
--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.json('"'"'$.user.name'"'"'), "time" : $input.json('"'"'$.greeting.time'"'"')}" }'
--passthrough-behavior WHEN_NO_TEMPLATES
--region us-east-1
--profile admin

Aside from the request-template property, we also set the value of passthrough-behavior with WHEN_NO_TEMPLATES
  1. Execute the aws apigateway put-integration-response command with the response mapping template, as follows:
aws apigateway put-integration-response 
--rest-api-id y3yftanqp7
--resource-id e4w7ka
--http-method POST
--status-code 200
--region us-east-1
--selection-pattern ""
--response-templates '{ "application/json" : "{ "greeting" : $input.json('"'"'$.message'"'"')}" }'
--profile admin
  1. Deploy our API into a stage by using the apigateway sub-command create-deployment.
  2. Give permission for the API method to invoke the Lambda function by using the command aws lambda add-permission.
..................Content has been hidden....................

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