Scaling Serverless Architectures: Implementing Scalable API Solutions
Implement solutions that are scalable to meet business requirements
Scaling Serverless Architectures: Implementing Scalable API Solutions
This lab focuses on Task Statement 3.2: Implement solutions that are scalable to meet business requirements from the AWS Certified DevOps Engineer - Professional (DOP-C02) exam. You will build a loosely coupled, serverless architecture using Amazon API Gateway and AWS Lambda, then explore how to configure and monitor scaling behaviors.
[!WARNING] Remember to run the teardown commands at the end of the lab to avoid ongoing charges.
Prerequisites
- An AWS Account with administrative access.
- AWS CLI installed and configured with appropriate credentials.
- Basic familiarity with Python (for the Lambda function code).
- Terminal/Command line access.
Learning Objectives
- Deploy a serverless REST API using Amazon API Gateway and AWS Lambda.
- Configure Lambda Concurrency to manage scaling limits and protect downstream resources.
- Implement API Gateway Throttling to manage request rates and meet business SLAs.
- Verify scaling behavior through active testing.
Architecture Overview
Step-by-Step Instructions
Step 1: Create an IAM Execution Role for Lambda
Before creating the function, we need a role that allows Lambda to write logs to CloudWatch.
# 1. Create the trust policy file
echo '{"Version": "2012-10-17","Statement": [{"Effect": "Allow","Principal": {"Service": "lambda.amazonaws.com"},"Action": "sts:AssumeRole"}]}' > trust-policy.json
# 2. Create the IAM Role
aws iam create-role --role-name brainybee-lab-lambda-role --assume-role-policy-document file://trust-policy.json
# 3. Attach the basic execution policy
aws iam attach-role-policy --role-name brainybee-lab-lambda-role --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole▶Console alternative
Navigate to IAM > Roles > Create role. Select Lambda as the service. Add the AWSLambdaBasicExecutionRole managed policy. Name it brainybee-lab-lambda-role.
Step 2: Create the Lambda Function
We will create a simple Python function that simulates a processing delay to observe scaling behavior.
# 1. Create the function code
echo 'import json
import time
def lambda_handler(event, context):
time.sleep(0.5) # Simulate work
return {"statusCode": 200, "body": json.dumps("Scaling Success!")}' > lambda_function.py
# 2. Package the code
zip function.zip lambda_function.py
# 3. Deploy the function (Replace <ACCOUNT_ID> with your actual ID)
aws lambda create-function --function-name brainybee-scaling-func \
--zip-file fileb://function.zip --handler lambda_function.lambda_handler --runtime python3.9 \
--role arn:aws:iam::<YOUR_ACCOUNT_ID>:role/brainybee-lab-lambda-roleStep 3: Create the API Gateway
Now we create a REST API to expose our Lambda function via an HTTP endpoint.
# 1. Create the REST API
aws apigateway create-rest-api --name "ScalingLabAPI"
# 2. Note the "id" from the output. We will call this <API_ID>.
# Get the Root Resource ID
aws apigateway get-resources --rest-api-id <API_ID>
# 3. Create a resource and method (using Root Resource ID <ROOT_ID>)
aws apigateway put-method --rest-api-id <API_ID> --resource-id <ROOT_ID> --http-method GET --authorization-type "NONE"
# 4. Set the Lambda integration
aws apigateway put-integration --rest-api-id <API_ID> --resource-id <ROOT_ID> --http-method GET --type AWS_PROXY \
--integration-http-method POST --uri arn:aws:apigateway:<YOUR_REGION>:lambda:path/2015-03-31/functions/arn:aws:lambda:<YOUR_REGION>:<YOUR_ACCOUNT_ID>:function:brainybee-scaling-func/invocationsStep 4: Configure Scaling Limits (Reserved Concurrency)
To ensure this function doesn't consume all account-wide concurrency, we will set a limit.
aws lambda put-function-concurrency --function-name brainybee-scaling-func --reserved-concurrent-executions 5[!NOTE] By setting reserved concurrency to 5, we guarantee this function can always scale to 5 instances, but never more, protecting our backend if this were a database-heavy task.
Checkpoints
- Deployment Check: Run
aws lambda get-function --function-name brainybee-scaling-func. You should see the configuration and the concurrency limit of 5. - API Check: Use the AWS Console to "Test" the API Gateway method. It should return a 200 OK with "Scaling Success!".
- Permissions Check: Ensure you have added the Lambda permission to allow API Gateway to invoke it:
bash
aws lambda add-permission --function-name brainybee-scaling-func --statement-id apigateway-test \ --action lambda:InvokeFunction --principal apigateway.amazonaws.com
Concept Review
When implementing scalable solutions, DevOps engineers must balance Throughput (requests per second) against Latency (time per request).
| Feature | Purpose in Scaling | Metric to Watch |
|---|---|---|
| Reserved Concurrency | Prevents one function from starving others. | ConcurrentExecutions |
| Provisioned Concurrency | Eliminates "Cold Starts" for low-latency needs. | ProvisionedConcurrencyUtilization |
| API Throttling | Protects API from spikes at the entry point. | 429 Too Many Requests |
Troubleshooting
| Error | Likely Cause | Fix |
|---|---|---|
403 Forbidden | Missing Lambda permissions. | Run the add-permission command in the Checkpoints section. |
429 Too Many Requests | Hit API Gateway or Lambda scaling limits. | Increase Reserved Concurrency or check API Stage settings. |
504 Gateway Timeout | Lambda took longer than 29s (API limit). | Optimize Lambda code or use asynchronous patterns. |
Stretch Challenge
Implement Provisioned Concurrency: Update the Lambda function to use Provisioned Concurrency of 2. This requires creating a function Alias first. Research how this differs from Reserved Concurrency in terms of "Cold Start" performance.
Cost Estimate
| Service | Usage | Estimated Monthly Cost (Free Tier) |
|---|---|---|
| AWS Lambda | 1 Million Requests/mo | $0.00 (Free Tier Forever) |
| API Gateway | 1 Million Requests/mo | $0.00 (12 months free) |
| CloudWatch Logs | 5GB Ingestion | $0.00 (Free Tier) |
Clean-Up / Teardown
[!IMPORTANT] Always delete resources to keep your account clean and avoid unexpected costs.
# 1. Delete the API Gateway
aws apigateway delete-rest-api --rest-api-id <API_ID>
# 2. Delete the Lambda Function
aws lambda delete-function --function-name brainybee-scaling-func
# 3. Detach and delete the IAM Role
aws iam detach-role-policy --role-name brainybee-lab-lambda-role --policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
aws iam delete-role --role-name brainybee-lab-lambda-role
# 4. Remove local files
rm lambda_function.py function.zip trust-policy.json