log to specific logstream in cloudwatch from lambda
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
AWS Lambda automatically sends print output and runtime logs to CloudWatch Logs, but it does not let you choose the default log stream name for those entries. If you need records to land in a specific log stream, you must call the CloudWatch Logs API yourself. That is possible, but it adds extra permissions, API calls, and failure cases to the function.
What Lambda Controls by Default
A normal Lambda function logs into the standard Lambda log group, which is usually named /aws/lambda/<function-name>. AWS then creates log streams based on the execution environment.
A basic example:
That output goes to a stream chosen by Lambda. You cannot make logger.info write to a handpicked stream name inside the default mechanism.
When Manual Stream Selection Is Justified
Writing to a specific stream can make sense when:
- multiple functions must write to one shared application stream
- an external process expects a known stream name
- you want application events separated from runtime logs
If none of those apply, stick with default Lambda logging. It is simpler and more reliable.
Writing to a Specific Stream with boto3
To target a specific stream, create or reuse a log group, create or reuse the stream, then call put_log_events.
This works, but it turns logging into application logic. If CloudWatch Logs becomes slow or misconfigured, your function now feels that failure directly.
Required IAM Permissions
The Lambda execution role must be allowed to manage the custom log destination. Typical permissions include:
- '
logs:CreateLogGroup' - '
logs:CreateLogStream' - '
logs:PutLogEvents'
If the role only has the default basic Lambda logging policy, a custom group or stream may still fail depending on the exact resource policy attached.
Understand the Tradeoff
Manual log-stream selection is more fragile than default logging because it introduces new dependencies into the request path:
- the group may not exist yet
- the stream may not exist yet
- the function may be throttled by CloudWatch Logs API limits
- IAM may block the write
- a naming decision can create too much contention in one stream
That is why many teams prefer to keep the default Lambda streams and solve routing with structured logs, metric filters, or subscriptions instead.
Structured Logs Are Often Better
If your real goal is searchable application logs, emit structured JSON and let CloudWatch or downstream tooling filter it.
This keeps the AWS-managed stream model intact while giving you much better queryability.
Common Pitfalls
- Expecting
printor the default Lambda logger to write into a custom stream name. - Forgetting to grant
logs:CreateLogStreamandlogs:PutLogEventsfor the custom destination. - Creating or checking the log stream on every invocation without considering latency and retries.
- Using one hot shared stream for high-concurrency traffic and turning logging into a bottleneck.
- Solving a search problem with custom streams when structured JSON logs would be simpler.
Summary
- Default Lambda logging goes to AWS-managed CloudWatch streams, not a stream name you choose.
- To write to a specific stream, call CloudWatch Logs directly from your code.
- Custom stream logging requires explicit group and stream management plus IAM permissions.
- Manual logging adds latency and operational complexity to the function.
- For many workloads, structured logs in the default Lambda streams are the better design.

