AWS REST API

Glitch
5 min readJan 8, 2023

--

Another good usage of AWS is API .So let's explore by creating one …

Amazon Web Services (AWS) offers a set of APIs (Application Programming Interfaces) that allow developers to access and interact with various AWS services. These APIs enable developers to programmatically perform tasks such as creating and managing Amazon EC2 instances, storing and retrieving data from Amazon S3, and more.

AWS APIs use a request-response model, where a client sends a request to the API and the API returns a response. The API’s request and response parameters are defined in a metadata document called the API Gateway API Model Schema (formerly known as the Swagger specification). This document specifies the API’s resources, methods, request parameters, and response parameters.

There are 3 ways to create AWS API:

AWS Management Console

AWS CLI (Command Line Interface)

AWS SDKs

Using AWS Management Console

Here is an example of how to create a REST API in AWS using the AWS Management Console:

  1. Go to the AWS Management Console and sign in to your AWS account.
  2. In the Services menu, go to the API Gateway section.
  3. Click the “Create API” button.
  4. Select “New API” and give your API a name and description. For this example, let’s call the API “Example API” and give it a description of “This is an example API.”
  5. Define the resources and methods for your API. For this example, let’s create a resource called “customers” with a GET method that retrieves a list of customers from a database. To create the resource and method, do the following:
  6. Click the “Actions” dropdown menu and select “Create Resource.” Give the resource the name of “customers” and click “Create Resource.”
  7. Click the “Actions” dropdown menu again and select “Create Method.” Select the GET method and click the checkmark to save the method.
  8. Set up the request and response parameters for the method. To do this, click on the GET method in the resources tree and then click the “Integration Request” button in the Method Execution panel. From here, you can define the request parameters and mapping templates for the method. For this example, let’s set up a request parameter called “id” that will be used to retrieve a specific customer from the database.
  9. Repeat steps 6–8 for each resource and method you want to include in your API.
  10. Deploy the API to make it available to your clients. To do this, click the “Actions” dropdown menu and select “Deploy API.” Select the deployment stage (e.g., test, production) and click “Deploy.”

You can then test the API by using a tool like Postman to send a GET request to the API’s URL. For example, if the API’s URL is https://abc123.execute-api.us-east-1.amazonaws.com/test/customers, you could send a GET request to https://abc123.execute-api.us-east-1.amazonaws.com/test/customers?id=123 to retrieve the customer with an ID of 123.

Using AWS CLI:

To create a REST API in AWS using the AWS CLI, you can use the create-rest-api command. Here is an example of how to create a REST API in AWS using the AWS CLI:

  1. First, install the AWS CLI and configure it with your AWS access keys.
  2. Next, create a file called create-api.json that contains the following JSON configuration for your API:
{
"name": "Example API",
"description": "This is an example API",
"endpointConfiguration": {
"types": [
"REGIONAL"
]
}
}
  1. Run the create-rest-api command, specifying the create-api.json file as the input:
aws apigateway create-rest-api --cli-input-json file://create-api.json

This will create a new REST API with the name and description specified in the create-api.json file.

To define the resources and methods for the API, you can use the create-resource and put-method commands. For example, to create a resource called "customers" with a GET method, you can use the following commands:

aws apigateway create-resource --rest-api-id <api-id> --parent-id <api-id> --path-part customers
aws apigateway put-method --rest-api-id <api-id> --resource-id <resource-id> --http-method GET --authorization-type NONE

Replace <api-id> and <resource-id> with the ID of your API and the ID of the "customers" resource, respectively.

To set up the request and response parameters for the method, you can use the put-integration and put-method-response commands. For example:

aws apigateway put-integration --rest-api-id <api-id> --resource-id <resource-id> --http-method GET --type HTTP --uri <backend-uri>
aws apigateway put-method-response --rest-api-id <api-id> --resource-id <resource-id> --http-method GET --status-code 200

Replace <backend-uri> with the URI of your backend service.

Finally, to deploy the API and make it available to your clients, you can use the create-deployment command:

aws apigateway create-deployment --rest-api-id <api-id> --stage-name test

This will create a deployment of the API in the “test” stage.

Using AWS SDK

To create a REST API in AWS using an AWS SDK, you will need to use the AWS SDK for your programming language of choice. Here is an example of how to create a REST API in AWS using the AWS SDK for Python:

  1. First, install the AWS SDK for Python using pip:
pip install boto3
  1. Next, import the boto3 library and create a client for the API Gateway service:
import boto3
client = boto3.client('apigateway')
  1. Use the create_rest_api method to create a new REST API:
response = client.create_rest_api(
name='Example API',
description='This is an example API',
endpointConfiguration={
'types': [
'REGIONAL'
]
}
)
api_id = response['id']

This will create a new REST API with the name and description specified. The api_id variable will contain the ID of the API.

To define the resources and methods for the API, you can use the create_resource and put_method methods. For example, to create a resource called "customers" with a GET method, you can use the following code:

response = client.create_resource(
restApiId=api_id,
parentId=api_id,
pathPart='customers'
)
resource_id = response['id']client.put_method(
restApiId=api_id,
resourceId=resource_id,
httpMethod='GET',
authorizationType='NONE'
)

To set up the request and response parameters for the method, you can use the put_integration and put_method_response methods. For example:

client.put_integration(
restApiId=api_id,
resourceId=resource_id,
httpMethod='GET',
type='HTTP',
uri='<backend-uri>'
)
client.put_method_response(
restApiId=api_id,
resourceId=resource_id,
httpMethod='GET',
statusCode='200'
)

Replace <backend-uri> with the URI of your backend service.

Finally, to deploy the API and make it available to your clients, you can use the create_deployment method:

client.create_deployment(
restApiId=api_id,
stageName='test'
)

This will create a deployment of the API in the “test” stage.

Ta-da !! that covers API creation using AWS .Happy Coding !!

--

--

No responses yet