0
0
AwsHow-ToBeginner · 4 min read

How to Create HTTP API in AWS API Gateway

To create an HTTP API in AWS API Gateway, define the API with routes and integrations using the AWS Management Console, CLI, or Infrastructure as Code tools. The API handles HTTP requests and forwards them to backend services like Lambda or HTTP endpoints.
📐

Syntax

The basic syntax to create an HTTP API using AWS CLI involves the aws apigatewayv2 create-api command with parameters:

  • --name: The name of your API.
  • --protocol-type HTTP: Specifies the API type as HTTP API.
  • --target: The backend endpoint or Lambda function ARN to integrate.
bash
aws apigatewayv2 create-api --name MyHttpApi --protocol-type HTTP --target https://example.com/backend
💻

Example

This example shows how to create a simple HTTP API with one route that forwards GET requests to a Lambda function using AWS CLI.

bash
aws apigatewayv2 create-api --name MyHttpApi --protocol-type HTTP

aws apigatewayv2 create-integration --api-id <api-id> --integration-type AWS_PROXY --integration-uri arn:aws:lambda:us-east-1:123456789012:function:MyLambdaFunction

aws apigatewayv2 create-route --api-id <api-id> --route-key "GET /hello" --target integrations/<integration-id>

aws apigatewayv2 create-deployment --api-id <api-id>
Output
Creates HTTP API, integration, route, and deployment; API is ready to handle GET /hello requests.
⚠️

Common Pitfalls

Common mistakes when creating HTTP APIs include:

  • Not specifying --protocol-type HTTP, which defaults to REST API.
  • Forgetting to deploy the API after creating routes and integrations.
  • Using incorrect integration URIs or missing permissions for Lambda integrations.
  • Not setting the correct route key format (e.g., GET /path).
bash
Wrong: aws apigatewayv2 create-api --name MyApi
Right: aws apigatewayv2 create-api --name MyApi --protocol-type HTTP
📊

Quick Reference

Steps to create an HTTP API in AWS API Gateway:

  • Create API with --protocol-type HTTP.
  • Create integration pointing to backend (Lambda or HTTP endpoint).
  • Create route with method and path, linking to integration.
  • Deploy the API to make it live.

Key Takeaways

Always specify --protocol-type HTTP to create an HTTP API, not REST API.
Create integrations and routes before deploying the API.
Use correct route keys like 'GET /path' to define HTTP methods and paths.
Deploy the API after configuration to activate it.
Ensure backend permissions are set for Lambda or HTTP integrations.