1. Introduction
Monitoring a service is not just about knowing whether it is available. When an issue occurs, operators need to understand what failed, where the request spent its time, and whether an infrastructure change contributed to the problem.
This project implements an end-to-end observability setup for a serverless API using Amazon CloudWatch, AWS X-Ray and AWS CloudTrail.
CloudWatch provides metrics, logs, dashboards and alarms. X-Ray helps inspect sampled request traces across API Gateway and Lambda. CloudTrail records AWS account management activity and delivers trail files to S3.
Project: AWS Community Builders – Article 12
Region:ap-south-2\
Infrastructure as Code: Terraform
Workload: Amazon API Gateway REST API and Python 3.12 AWS Lambda
Observability: CloudWatch Logs, Metrics, Dashboard, Alarms, AWS X-Ray and CloudTrail
Repository Code: AWS Community Builders RepositoryImplementation status: Infrastructure deployed successfully. The verification script completed successfully.
2. What problem are we solving?
Imagine you are responsible for a serverless application. Users report failed or slow requests, but the available information is spread across different services.
For example:
- API requests start returning HTTP 5XX responses.
- Lambda invocations fail, but the reason is not immediately visible from aggregate metrics.
- Application logs contain exceptions, but do not show the full request path.
- It is difficult to determine where request processing time is spent.
- An infrastructure change may have occurred around the same time as the incident.
The problem we are solving: How can we bring application metrics, logs, distributed traces and AWS management events together to investigate service behaviour and infrastructure changes?
3. Objectives
- Provision an API Gateway REST API and Lambda function using Terraform.
- Generate both successful requests and a controlled application error.
- Collect structured Lambda logs and API Gateway access logs in CloudWatch.
- Create a CloudWatch dashboard for key API Gateway and Lambda metrics.
- Configure CloudWatch alarms for Lambda errors and API Gateway 5XX responses.
- Enable X-Ray tracing for API Gateway and Lambda.
- Configure a CloudTrail trail for regional management events.
- Deliver CloudTrail log files to an encrypted S3 bucket.
- Verify the deployed resources and capture implementation evidence.
4. Architecture
Architecture: Client → API Gateway REST API → AWS Lambda (Python 3.12). API Gateway access logs and Lambda application logs are sent to CloudWatch Logs. API Gateway and Lambda metrics are displayed in a CloudWatch dashboard and evaluated by CloudWatch alarms. X-Ray provides sampled request traces across API Gateway and Lambda. CloudTrail records regional management events and delivers log files to S3.
5. Services used
| Service | Purpose |
|---|---|
| Amazon API Gateway | Exposes the sample REST API |
| AWS Lambda | Runs the Python application |
| Amazon CloudWatch Logs | Stores Lambda and API Gateway logs |
| Amazon CloudWatch Metrics | Stores service metrics |
| CloudWatch Alarms | Evaluates error thresholds |
| CloudWatch Dashboard | Displays API and Lambda metrics |
| AWS X-Ray | Provides sampled request tracing |
| AWS CloudTrail | Records AWS management events |
| Amazon S3 | Stores CloudTrail log files |
| IAM | Grants services the required permissions |
| Terraform | Provisions and configures the infrastructure |
6. Infrastructure design
Terraform provisions an API Gateway REST API with a prod stage and a Python 3.12 Lambda function. The API includes a normal request path and an /error path that deliberately raises an exception for testing.
The Lambda function emits structured log events, including:
request_receivedrequest_completedintentional_test_error
CloudWatch log groups are configured for Lambda application logs and API Gateway access logs, with seven-day retention. A dashboard displays Lambda invocation, error and duration metrics, along with API Gateway request, 5XX and latency metrics.
The project also creates two CloudWatch alarms:
- Lambda errors.
- API Gateway 5XX responses.
X-Ray tracing is enabled for the API Gateway stage and active tracing is enabled for Lambda.
CloudTrail is configured as a single-region trail for management events. Its S3 destination has server-side encryption, public access blocking and a lifecycle rule.
7. Deploy
From the project root:
chmod +x scripts/*.sh
./scripts/deploy.sh
The deployment script runs the Terraform workflow. Review the Terraform plan and confirm the target AWS account and region before approving the deployment.
To retrieve the API endpoint:
cd terraform
terraform output -raw api_invoke_url
Deployment evidence:
8. Generate test requests
Retrieve the API URL and invoke the normal and error routes:
cd terraform
API_URL="$(terraform output -raw api_invoke_url)"
curl -i "${API_URL}"
curl -i "${API_URL}error"
The normal route should return a successful response. The /error route deliberately raises an exception inside Lambda so that the error can be inspected in logs and metrics.
Generate a small amount of additional traffic for dashboard and trace inspection:
for i in {1..10}; do
curl -s "${API_URL}"
echo
done
9. Validate CloudWatch logs and dashboard
Open CloudWatch in the deployment region.
The Lambda log group is:
/aws/lambda/article-12-observability-function
Inspect a recent log stream and verify the structured events for successful and failed requests. The API Gateway access log group can also be inspected to review request-level access information.
Screenshot 1 — successful Lambda logs
Screenshot 2 — Lambda error logs
Open CloudWatch → Dashboards and select:
article-12-observability-dashboard
The dashboard displays the configured Lambda and API Gateway metrics. Select an appropriate time range and allow time for metric datapoints to arrive.
Screenshot 3 — CloudWatch dashboard
10. Validate CloudWatch alarms
The project creates the following alarms:
article-12-observability-lambda-errors
article-12-observability-api-5xx
The Lambda alarm evaluates the configured function error metric, while the API Gateway alarm evaluates the configured 5XX metric.
An OK state means the evaluated metric is not currently breaching the configured threshold. ALARM indicates that the threshold condition has been met. INSUFFICIENT_DATA means there are not enough datapoints to evaluate the alarm.
Screenshot 4 — CloudWatch alarms
11. Validate distributed tracing with AWS X-Ray
Open the X-Ray console in the deployment region and select the service map. After generating requests, inspect the API Gateway and Lambda components.
The service map provides a high-level view of the traced request path. Opening a trace provides timing and segment details that can help identify where processing time was spent.
Screenshot 5 — X-Ray service map
Screenshot 6 — X-Ray trace details
X-Ray uses sampling, so not every request is guaranteed to appear as a trace. If traces are not immediately visible, check the selected region and time range, generate a few requests and allow time for telemetry to arrive.
12. Validate CloudTrail and S3 delivery
Open CloudTrail → Event history in the deployment region. Review recent management events recorded by the trail. The exact event names depend on the AWS operations performed.
Screenshot 7 — CloudTrail Event History
Retrieve the configured S3 bucket name:
cd terraform
terraform output -raw cloudtrail_bucket_name
Open the bucket in S3 and inspect the CloudTrail log prefix:
cloudtrail/AWSLogs/<AWS_ACCOUNT_ID>/CloudTrail/ap-south-2/
CloudTrail delivery is asynchronous, so log files may take time to appear.
Screenshot 8 — CloudTrail logs delivered to S3
This implementation records management events. It does not enable S3 object-level data events.
13. Implementation status and verification
The implementation and evidence collection have been completed.
The scripts/verify.sh script completed successfully. Its successful execution should not be interpreted as proof that every dashboard widget has datapoints or that every request has a retained X-Ray trace.
14. Security and cost considerations
This is a demonstration API and should not be treated as a production-ready public endpoint. The sample does not implement application authentication or authorization. Do not expose sensitive operations or data through it.
The /error route is included only to generate a controlled failure signal. Protect or remove such a route before adapting the application for production.
Potential charges may arise from API Gateway requests, Lambda execution, CloudWatch metrics and log ingestion/storage, X-Ray tracing, CloudTrail and S3. Costs depend on region and usage. Review current AWS pricing and the account's billing dashboard.
15. Cleanup
After collecting the required evidence, run:
./scripts/destroy.sh
Review the script before execution. It asks for confirmation of the CloudTrail bucket before emptying it and destroying the Terraform-managed resources. Confirm that the bucket belongs to this project and contains no data that must be retained.
16. Conclusion
This project demonstrates how CloudWatch, X-Ray and CloudTrail provide complementary views of a serverless application. CloudWatch helps identify service behaviour through metrics and logs, X-Ray helps inspect sampled request paths, and CloudTrail provides an audit history of AWS management activity.
Combining these signals gives operators a stronger foundation for investigating application errors, latency and infrastructure changes.










Top comments (0)