REST API creation in AWS - Time & Space Complexity
When creating a REST API on AWS, it's important to understand how the time to set up and respond grows as you add more resources or requests.
We want to know how the number of API calls or operations changes as the API grows.
Analyze the time complexity of the following operation sequence.
// Create REST API
aws apigateway create-rest-api --name MyApi
// Create resource for each path
aws apigateway create-resource --rest-api-id apiId --parent-id rootId --path-part resourceName
// Create method for each resource
aws apigateway put-method --rest-api-id apiId --resource-id resourceId --http-method GET --authorization-type NONE
// Deploy API
aws apigateway create-deployment --rest-api-id apiId --stage-name prod
This sequence creates a REST API, adds resources and methods for each path, and deploys it.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Creating resources and methods for each API path.
- How many times: Once for the API, then once per resource path added.
Each new API path requires creating a resource and a method, so the number of operations grows as you add more paths.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | About 21 (1 API + 10 resources + 10 methods) |
| 100 | About 201 (1 API + 100 resources + 100 methods) |
| 1000 | About 2001 (1 API + 1000 resources + 1000 methods) |
Pattern observation: The number of operations grows roughly in a straight line as you add more API paths.
Time Complexity: O(n)
This means the time to create the API grows directly in proportion to the number of paths you add.
[X] Wrong: "Creating the API once means adding many paths is still fast and constant time."
[OK] Correct: Each path requires separate resource and method creation, so the total work grows with the number of paths.
Understanding how API creation scales helps you design efficient cloud solutions and shows you can think about growth, which is valuable in real projects.
"What if we batch resource and method creation in fewer API calls? How would the time complexity change?"
Practice
Solution
Step 1: Understand REST API purpose
REST APIs enable communication between apps using simple commands over the internet.Step 2: Identify AWS REST API role
In AWS, REST APIs define resources (paths) and methods (actions) to allow this communication.Final Answer:
To allow different applications to communicate over the internet using defined paths and actions. -> Option BQuick Check:
REST API = communication interface [OK]
- Confusing REST API with data storage services
- Thinking REST API runs virtual machines
- Mixing REST API with user management
Solution
Step 1: Identify AWS services for APIs
Amazon API Gateway is designed to create, publish, and manage REST APIs.Step 2: Differentiate other services
Amazon S3 stores files, Lambda runs code, EC2 runs servers, but API Gateway manages APIs.Final Answer:
Amazon API Gateway -> Option AQuick Check:
API Gateway = REST API service [OK]
- Choosing Lambda as the API creator
- Confusing S3 with API service
- Selecting EC2 for API management
/books with a GET method using MOCK integration. What happens when a client sends a GET request to /books?Solution
Step 1: Understand MOCK integration
MOCK integration returns a fixed response defined in API Gateway without backend calls.Step 2: Analyze GET method on /books
Since GET uses MOCK, the client receives the mock response directly.Final Answer:
The API returns a predefined mock response without calling any backend. -> Option DQuick Check:
MOCK integration = fixed response [OK]
- Assuming MOCK calls database or Lambda
- Thinking MOCK integration causes errors
- Confusing MOCK with real backend integration
/orders in API Gateway but get a 403 Forbidden error when testing. What is the most likely cause?Solution
Step 1: Understand 403 Forbidden in API Gateway
403 usually means permission denied to invoke backend or access resource.Step 2: Check method permissions
POST method likely lacks permission to call backend service, causing 403 error.Final Answer:
The method does not have permission to invoke the backend service. -> Option CQuick Check:
403 = permission denied [OK]
- Assuming API Gateway disallows POST
- Blaming client path spelling without checking
- Thinking MOCK integration causes 403
/users that supports GET and POST methods. You start with MOCK integration for both methods to test. Later, you want GET to fetch data from a Lambda function and POST to store data in DynamoDB via Lambda. What is the best approach to achieve this?Solution
Step 1: Start with MOCK integration for testing
MOCK lets you test API behavior without backend, a good first step.Step 2: Update methods to Lambda integration
Later, replace MOCK with Lambda integration for GET and POST to connect real backend logic.Step 3: Ensure permissions and backend setup
Configure Lambda permissions and DynamoDB access properly for smooth operation.Final Answer:
Create the API with MOCK integration first, then update GET and POST methods to use Lambda integration with proper permissions. -> Option AQuick Check:
Start MOCK, then switch to Lambda integration [OK]
- Trying to use MOCK and Lambda in separate APIs
- Keeping MOCK integration permanently
- Connecting POST directly to DynamoDB without Lambda
Ready to prove it?
Pass the test and this pattern moves unknown
