Hello world via the CLI

Next, we're going to extend the API we built in the previous section. We're going to use the command line entirely to create a new method, deploy it to a dev stage, and fire off some test requests. If you haven't created your API key and usage plan yet, go back to the last section and do that before progressing.

Using your AWS CLI, let's create a new POST method under the resource we've already created. Substitute your IDs into the following command:

aws apigateway put-method 
--rest-api-id ${api-id}
--resource-id ${resource-id}
--http-method POST
--authorization-type NONE
--api-key-required

This new method should also be connected to the same Lambda function that we're using for our backend. Notice here that we have to issue another command to add the integration. The console steps do this as part of the method setup:

aws apigateway put-integration 
--rest-api-id ${api-id}
--resource-id ${resource-id}
--http-method POST
--type AWS
--integration-http-method POST
--uri arn:aws:apigateway:${region}:lambda:path/2015-03-31/functions/arn:aws:lambda:${region}:${account-number}:function:hello-world-python/invocations

The URI option is the location of the endpoint for event source integration. This is a unique value and is used to relate the API Gateway event source to your Lambda function. The date in the path refers to the Lambda service API version.

Now, we need to add our method response as follows, replacing the ${} placeholder with your values:

aws apigateway put-method-response 
--rest-api-id ${api-id}
--resource-id ${resource-id}
--http-method POST
--status-code 200
--response-models application/json=Empty

And then we have the integration response:

 aws apigateway put-integration-response  
--rest-api-id ${api-id}
--resource-id ${resource-id}
--http-method POST
--status-code 200
--response-templates application/json=""

Deploy the API again to our dev stage:

aws apigateway create-deployment 
--rest-api-id ${api-id}
--stage-name dev

We also have to add a permission statement to Lambda to allow API Gateway to invoke our function. This was done for us when using the console, which is why we didn't have to do this step in the previous section:

 aws lambda add-permission 
--function-name hello-world-python
--statement-id hello-world-api-allow
--principal apigateway.amazonaws.com
--action lambda:InvokeFunction
--source-arn "arn:aws:execute-api:${region}:${account-id}:${api-id}/*/POST/hellos"

This time, to test our new method, we're going to use a command-line utility called httpie and pass in a JSON request body. You can also use any other HTTP client—cURL is another popular one:

Example request to our API using httpie and bash —the request and response are shown

And that's it! Congratulations on creating your first completely serverless API using API Gateway and Lambda. 

You can see from the CLI steps that the console is actually doing a lot of the setup for us and this makes for a more seamless experience. In a later chapter, we'll introduce Serverless Framework to make it even easier to develop our API Gateway setup alongside our function code.

In this section, we have progressed our understanding by creating our own APIs using the console and the CLI. When you've comfortable with what we have built, move on to the Summary and Questions sections.

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

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