Creating the 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. Create our path-partlambdagreetingusing the apigateway sub-command create-resource.
  4. Create a model schema for our JSON, using the apigateway sub-command create-model:
aws apigateway create-model 
--rest-api-id dqnqdyb3z2
--name 'greetingRequestModel'
--description 'Greeting Request Model'
--content-type 'application/json'
--schema '{
"$schema": "http://json-schema.org/draft-04/schema#",
"title": "greetingModel",
"type": "object",
"properties": {
"user" : {"type": "object",
"properties": {
"name" : {"type" : "string"}
}
},
"greeting" : {"type": "object",
"properties" : {
"time" : {"type" : "string"}
}
}
},
"required" : ["user", "greeting"]
}'
--profile admin

  1. Create a request validator for our JSON, using the apigateway sub-command create-request-validator:
aws apigateway create-request-validator 
--rest-api-id dqnqdyb3z2
--name greetingRequestValidator
--validate-request-body
--profile admin
  1. Execute the aws apigateway put-method command with our request model and request validator IDs, as follows:
aws apigateway put-method 
--rest-api-id dqnqdyb3z2
--resource-id ffknxp
--http-method POST
--authorization-type "NONE"
--request-models application/json=greetingRequestModel
--request-validator-id 549e4h
--region us-east-1
--profile admin
  1. Set a response status code for our POST method by using the apigateway sub-command put-method-response.
  2. Set up the integration type, request-template, and the passthrough behavior with the apigateway sub-command put-integration.
  3. Set up the response mapping, using the apigateway sub-command put-integration-response.
  4. Deploy our API into a stage, dev, using the apigateway sub-command create-deployment.
  5. Give permission for the API method to invoke the Lambda function, 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
3.21.93.20