Amazon API Gateway to Amazon CloudWatch Logs

Client → Amazon API Gateway REST API → Amazon CloudWatch Logs

Create a REST API that proxies requests directly to Amazon CloudWatch Logs to put and get log events, without a Lambda function.

This pattern creates an Amazon API Gateway REST API that calls Amazon CloudWatch Logs directly through an AWS service integration, with no AWS Lambda function. POST /putLog maps to the PutLogEvents action and writes an event to a log stream, and POST /getLog maps to GetLogEvents and reads the events back.
API Gateway assumes an IAM role that allows only logs:PutLogEvents and logs:GetLogEvents on the one log stream the stack creates. The log group and stream are fixed in the mapping templates, so callers can't reach other logs, and the templates encode the caller's ts and message with $input.json, so a crafted value can't break the JSON request.
The API has no authorization, so anyone with the URL can write to and read the log stream. Use it for testing, and add IAM authorization, an Amazon Cognito user pool or a Lambda authorizer before any other use.

< Back to all patterns

GitHub icon Download this pattern (.zip)

GitHub icon View this pattern on GitHub


Clone repo

git clone https://github.com/aws-samples/serverless-patterns/cd serverless-patterns/apigw-cloudwatch-logs/sam

Deploy

sam deploy --guided


Testing

Send POST requests with a ts and message to the PutLogUrl output, then read them back from GetLogUrl. See the README in the GitHub repo for the commands.

Cleanup

sam delete

Created by:

Sphoorti Poojary

Sphoorti Poojary

Sphoorti is a Technical Account Manager at Amazon Web Services. She helps customers build and operate serverless applications on AWS.