Deploying an API Using Amazon's CLI

Woman on ComputerAmazon's API Gateway is a fully managed service that allows developers to easily create, publish, maintain, monitor, and secure custom APIs. This service handles all the tasks involved in accepting and processing a vast number of concurrent API calls while integrating with existing HTTP endpoints, Lambda functions, and other AWS services. While the hand-coded deployment of an API with frameworks can be an exhaustive task, AWS makes it simple, cost-effective, and easy to maintain. In this post, we will look at how to create, configure, and deploy an API using Amazon's Command Line Interface.

Before we get started, it is important to note some of the benefits and use cases for why we would exploit the Command Line Interface instead of using the AWS console. The CLI is a single tool that can control and manage multiple AWS services. This provides developers with a standardized method of running services. In addition, by using the CLI, developers are able to create scripts to automate the process of creating and deploying services. This offers users a consistent environment across multiple accounts. An example use case of this would be creating a product that requires a development account as well as a production account and being able to deploy an API effortlessly and consistently across both accounts.

Installing the CLI

Before we begin, we will need our access key and our secret access key from our AWS account to install and configure the command line interface. We can create these keys by signing in to the IAM dashboard, selecting "Manage Security Credentials" and "Create New Access Key".

Locate and Download your Access Key and Secret Key

Once we have our access keys, we can open up our terminal and enter the following command:

$ sudo pip install awscli

*Note: We will be completing this demo using the mac terminal. To install the AWS CLI on a PC, use the command >pip install awscli

After the CLI is installed we need to associate the CLI with our specific AWS account using the keys we grabbed earlier.


$ aws configure

AWS Access Key ID [None]: EXAMPLEACCESSKEY

AWS Secret Access Key [None]: EXAMPLESECRETKEY

Default region name [None]: us-east-1

Default output format [None]:ENTER

Create the API

Once we have installed the CLI, we can go ahead and get started setting up our API Gateway. For this demo, we will use the name "myAPI" for the API we are creating.


$ aws apigateway create-rest-api \

--name myAPI

This command will create a new API in our API Gateway service and return an ID that we will use to configure our API. Keep this ID on hand for the rest of the tutorial as we will need it to reference our specific API. We will also need to grab the root id using the get-resources command.


$aws apigateway get-resources\

--rest-api-id api-id

After this, we want to create a resource for our API. We will name our resource "APIDemo". We will keep track of the ID that is returned for our resource to add CRUD methods.


$ aws apigateway create-resource \

--rest-api-id api-id \

--parent-id root-id\

--path-part APIDemo\

Now we want to create a POST method for our resource. To do this, you need the API Id and the resource id. We will set our authorization type to None for now.


$ aws apigateway put-method \

--rest-api-id api-id \

--resource-id resource-id \

--http-method POST \

--authorization-type NONE

By the end of this section we should have an API (myAPI), a resource within myAPI (APIDemo), and a single POST method within that resource.

Configure the API

Since we have our API, our resource, and a method created, we can go ahead and configure the settings for the POST method. We will need to identify a destination for the POST method. This could be an HTTP endpoint, mock endpoint, lambda function, or other AWS service. For this demo, we will use a mock endpoint.


$ aws apigateway put-integration \

--rest-api-id api-id \

--resource-id resource-id \

--http-method POST \

--type MOCK \

--integration-http-method POST

Next, we want to tell the Mock endpoint what we want it to do. We are going to give it a status code of 200 for successful calls and we will identify that we want our response models to be empty JSON models.


$ 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\"}"

Deploy the API

To deploy our configured API, we need to decide on a name for this stage of development and provide that along with the API-Id. We will name our stage "dev".


$ aws apigateway create-deployment \

--rest-api-id api-id \

--stage-name dev

Test the API

If everything is configured correctly, the following command should return with a status of 200.


$aws apigateway test-invoke-method\

--rest-api-id api-id \

--resource-id resource-id\

--http-method POST\

--path-with-query-string ""\

--body "{"statusCode":200}"

In addition, you can check your API configuration in the API Gateway portal. It should list a POST method under the resource "APIDemo" with a Mock Endpoint and an HTTP Status of 200.

Final API Gateway

Conclusion

In conclusion, we have assembled a very simple API with a POST method and a mock endpoint using the Amazon command line interface in a short amount of time. The CLI is a powerful tool that can be used to further build out the API by adding more methods, security, and different endpoints.

Further Resources: