How to Create REST API in AWS API Gateway
To create a REST API in
AWS API Gateway, define the API with resources and methods, then deploy it to a stage. Use the AWS Management Console or AWS CLI to configure endpoints, integrate with backend services, and enable deployment.Syntax
Creating a REST API in API Gateway involves these main parts:
- API Name: A unique name for your API.
- Resources: Paths like
/usersor/ordersrepresenting endpoints. - Methods: HTTP methods like
GET,POST, etc., attached to resources. - Integration: Connects methods to backend services like AWS Lambda or HTTP endpoints.
- Deployment: Publish your API to a stage like
devorprodto make it accessible.
bash
aws apigateway create-rest-api --name "MyAPI" aws apigateway get-resources --rest-api-id <api-id> aws apigateway create-resource --rest-api-id <api-id> --parent-id <root-id> --path-part users aws apigateway put-method --rest-api-id <api-id> --resource-id <resource-id> --http-method GET --authorization-type NONE aws apigateway put-integration --rest-api-id <api-id> --resource-id <resource-id> --http-method GET --type MOCK aws apigateway create-deployment --rest-api-id <api-id> --stage-name dev
Example
This example creates a simple REST API with a /hello resource and a GET method that returns a mock response. It then deploys the API to the dev stage.
bash
aws apigateway create-rest-api --name "HelloAPI" # Get root resource ID ROOT_ID=$(aws apigateway get-resources --rest-api-id <api-id> --query 'items[?path==`/`].id' --output text) # Create /hello resource RESOURCE_ID=$(aws apigateway create-resource --rest-api-id <api-id> --parent-id $ROOT_ID --path-part hello --query 'id' --output text) # Create GET method on /hello aws apigateway put-method --rest-api-id <api-id> --resource-id $RESOURCE_ID --http-method GET --authorization-type NONE # Set MOCK integration for GET method aws apigateway put-integration --rest-api-id <api-id> --resource-id $RESOURCE_ID --http-method GET --type MOCK --request-templates '{"application/json":"{\"statusCode\": 200}"}' # Set method response aws apigateway put-method-response --rest-api-id <api-id> --resource-id $RESOURCE_ID --http-method GET --status-code 200 --response-models '{"application/json":"Empty"}' # Set integration response aws apigateway put-integration-response --rest-api-id <api-id> --resource-id $RESOURCE_ID --http-method GET --status-code 200 --response-templates '{"application/json":"{\"message\": \"Hello from API Gateway!\"}"}' # Deploy API aws apigateway create-deployment --rest-api-id <api-id> --stage-name dev
Output
Created REST API with ID: <api-id>
Created resource /hello with ID: <resource-id>
Method GET created on /hello
Integration MOCK set for GET
Deployment created for stage dev
Common Pitfalls
Common mistakes when creating REST APIs in API Gateway include:
- Not deploying the API after changes, so updates are not live.
- Forgetting to set method responses and integration responses, causing errors.
- Using incorrect resource or method IDs in commands.
- Not setting authorization properly, leading to unauthorized errors.
- Confusing REST API with HTTP API, which have different features.
bash
Wrong way (missing deployment): aws apigateway put-method --rest-api-id <api-id> --resource-id <resource-id> --http-method GET --authorization-type NONE # No deployment command here, so changes won't be live Right way (with deployment): aws apigateway put-method --rest-api-id <api-id> --resource-id <resource-id> --http-method GET --authorization-type NONE aws apigateway create-deployment --rest-api-id <api-id> --stage-name dev
Quick Reference
Remember these key steps to create a REST API in API Gateway:
- Create the REST API with a name.
- Add resources representing URL paths.
- Attach HTTP methods to resources.
- Integrate methods with backend services or mocks.
- Define method and integration responses.
- Deploy the API to a stage to make it accessible.
Key Takeaways
Create resources and methods before deploying your REST API in API Gateway.
Always deploy your API after making changes to make them live.
Set method and integration responses to avoid runtime errors.
Use the AWS CLI or Console to manage API Gateway configurations.
Test your API endpoint after deployment to verify it works as expected.