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 with the code files:

  1. Use the aws apigateway command to create a REST API in API Gateway, 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. Execute the aws apigateway put-method command with the http-method as POST, as follows:
aws apigateway put-method 
--rest-api-id 7uwav24q1f
--resource-id s6rij6
--http-method POST
--authorization-type "NONE"
--region us-east-1
--profile admin
  1. Execute aws apigateway put-method-response with the status-code as 200 for the http-method POST, as follows:
aws apigateway put-method-response 
--rest-api-id 7uwav24q1f
--resource-id s6rij6
--http-method POST
--status-code 200
--region us-east-1
--profile admin
  1. Execute the aws apigateway put-integration command with the http-method as POST, the type as AWS, and a Lambda URI, as per the required format:
aws apigateway put-integration 
--rest-api-id 7uwav24q1f
--resource-id s6rij6
--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'
--region us-east-1
--profile admin
We have omitted the request-templates property, and we are now passing parameters within the body of our POST request. 

The default pass through behavior is to pass the request body to the Lambda as is (if no matching templates are defined), as you can see in the response of put-integration, as follows:

  1. Execute the aws apigateway put-integration-response command for the http-method POST, with a selection-pattern of "":
aws apigateway put-integration-response 
--rest-api-id 7uwav24q1f
--resource-id s6rij6
--http-method POST
--status-code 200
--region us-east-1
--selection-pattern ""
--profile admin
  1. Deploy our API to a stage, dev, 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-api-gateway
--statement-id apigateway-st-2
--action lambda:InvokeFunction
--principal apigateway.amazonaws.com
--source-arn "arn:aws:execute-api:us-east-1:<account_id>:7uwav24q1f/*/POST/lambdagreeting"
--profile admin
..................Content has been hidden....................

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