<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Shaileshms on Medium]]></title>
        <description><![CDATA[Stories by Shaileshms on Medium]]></description>
        <link>https://medium.com/@shaileshms0505?source=rss-5d890adc5cd0------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/0*Bj3RV7UvTs31aeji</url>
            <title>Stories by Shaileshms on Medium</title>
            <link>https://medium.com/@shaileshms0505?source=rss-5d890adc5cd0------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 21:12:41 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@shaileshms0505/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How We Ship AWS CloudWatch Logs to Grafana Loki for Essentially Nothing]]></title>
            <link>https://medium.com/@shaileshms0505/how-we-ship-aws-cloudwatch-logs-to-grafana-loki-for-essentially-nothing-3496d0f269a7?source=rss-5d890adc5cd0------2</link>
            <guid isPermaLink="false">https://medium.com/p/3496d0f269a7</guid>
            <dc:creator><![CDATA[Shaileshms]]></dc:creator>
            <pubDate>Thu, 09 Jul 2026 15:50:03 GMT</pubDate>
            <atom:updated>2026-07-09T15:50:03.645Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*iIDP9Nn6KF4YCtzuxIqKOA.png" /></figure><p>The requirement was simple: everything in Grafana.</p><p>Lambda, Step Functions, Batch, Bedrock AgentCore — all logging to CloudWatch by default. But every time something broke, we were hunting through the AWS console instead of using the Grafana dashboards and alerts we’d already built. We needed a bridge that wouldn’t become its own maintenance burden.</p><p>Here’s what we built, including the dead ends.</p><h3>The Problem with the Obvious Solutions</h3><p>Before settling on our approach, we ruled out two common options.</p><p><strong>An EC2 instance running Grafana Alloy</strong> would cost $17–53/month, require patching and monitoring, introduce a 30-second polling delay, and — this surprised us — Alloy doesn’t actually have a native CloudWatch Logs pull component. loki.source.cloudwatch doesn&#39;t exist in Alloy v1.17. The push-based approach is simply the right pattern for AWS, and Alloy isn&#39;t built for it.</p><p><strong>A custom Lambda forwarder</strong> — writing a Node.js function to receive CloudWatch events and push to Loki — works, but it means owning and maintaining custom code for a problem that’s already been solved upstream.</p><p>What we actually wanted was something battle-tested, purpose-built, and zero-maintenance. That turned out to be two things: lambda-promtail for CloudWatch-based log sources, and <strong>FireLens</strong> (Fluent Bit as a sidecar) for Batch jobs on Fargate. Same destination, different paths — for reasons that weren&#39;t obvious until we got into it.</p><h3>The Architecture</h3><pre>Lambda / Step Functions / AgentCore<br>         │<br>         │  (CloudWatch Subscription Filter — event-driven)<br>         ▼<br>lambda-promtail (Lambda Function)<br>         │<br>         │  HTTPS POST, batched and compressed<br>         ▼<br>Loki → Grafana<br></pre><pre>Batch Jobs (Fargate)<br>         │<br>         │  stdout → FireLens log driver<br>         ▼<br>Fluent Bit sidecar (log-router container)<br>         │<br>         │  HTTPS POST (Loki output plugin)<br>         ▼<br>Loki → Grafana</pre><p>Two pipelines, one destination. Both inherently parallel and self-scaling. No coordination required.</p><h3>Pipeline 1: Lambda Has No Log Driver</h3><p>The first challenge surfaced immediately with Lambda. Unlike ECS services or Fargate tasks, Lambda functions have no log driver concept — there’s no sidecar, no Fluent Bit, nowhere to plug in a forwarder at the container level. CloudWatch is simply where Lambda logs go. Full stop.</p><p>So the question became: how do you get logs <em>out</em> of CloudWatch without keeping polling infrastructure running 24/7?</p><p>The answer is CloudWatch <strong>Subscription Filters</strong> — a native AWS feature that forwards log events to a destination in real-time. No polling. No cron. When a log event is written, CloudWatch batches it (typically 10–100 events) and invokes the destination Lambda within seconds. The filter itself is free; you pay only for the Lambda invocations it triggers.</p><p>lambda-promtail is Grafana&#39;s purpose-built Lambda function for exactly this. It receives each batch, decompresses it, attaches labels, and pushes to Loki. Batching, compression, retries — all handled. Nothing to write, nothing to maintain.</p><h3>Setup: Three Commands</h3><pre>git clone https://github.com/grafana/lambda-promtail.git<br>cd lambda-promtail<br>terraform init</pre><pre>terraform apply \<br>  -var &quot;write_address=https://your-loki-endpoint/loki/api/v1/push&quot; \<br>  -var &quot;username=your-username&quot; \<br>  -var &quot;password=your-password&quot; \<br>  -var &#39;log_group_names=[<br>    &quot;/aws/lambda/your-function-1&quot;,<br>    &quot;/aws/lambda/your-function-2&quot;,<br>    &quot;/aws/states/your-pipeline&quot;,<br>    &quot;/aws/bedrock-agentcore/runtimes/your-agent-runtime&quot;<br>  ]&#39; \<br>  -var &quot;extra_labels=team,your-team,env,uat&quot; \<br>  -var &quot;keep_stream=false&quot; \<br>  -var &quot;batch_size=131072&quot;</pre><p>Terraform creates the Lambda function, an IAM role, and a subscription filter on each log group. Adding more log groups later means appending to the list and re-running terraform apply.</p><h3>One Setting That Really Matters</h3><p>Set keep_stream=false. Every Lambda invocation creates a new CloudWatch log stream. If you include the stream name as a Loki label, you&#39;ll accumulate thousands of high-cardinality label values over time and degrade query performance. Let Loki use the log group as the primary identifier instead.</p><h3>Labels in Loki</h3><p>Every log entry gets these automatically:</p><p>Label Example __aws_cloudwatch_log_group /aws/lambda/your-function-1 __aws_log_type cloudwatch team your-team env uat</p><h3>Pipeline 2: Batch Looked Easier — Until It Wasn’t</h3><p>For Batch jobs running on Fargate, the situation looked simpler at first. Unlike Lambda, Fargate containers <em>do</em> have a log driver. We assumed we could set awsfirelens in the job definition console and be done with it.</p><p>Open the AWS Batch console, navigate to job definitions, create a revision, go to “Linux and logging settings,” open the Log driver dropdown — and you get: awslogs and splunk.</p><p>No awsfirelens. The console simply doesn&#39;t expose it.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*HExLZS1_sWuo4me8Ig5MQA.png" /></figure><p>The path forward is to define the job with <em>two containers</em> directly via aws batch register-job-definition using raw JSON — bypassing the console entirely. A log-router sidecar runs AWS Fluent Bit, and your app container points its log driver at awsfirelens. When registered this way, the console shows the job definition correctly with both containers visible, and Fluent Bit routes all app stdout directly to Loki over HTTPS — no CloudWatch in the middle.</p><h3>The Job Definition</h3><pre>{<br>  &quot;taskProperties&quot;: [<br>    {<br>      &quot;containers&quot;: [<br>        {<br>          &quot;name&quot;: &quot;log-router&quot;,<br>          &quot;image&quot;: &quot;public.ecr.aws/aws-observability/aws-for-fluent-bit:stable&quot;,<br>          &quot;essential&quot;: true,<br>          &quot;firelensConfiguration&quot;: { &quot;type&quot;: &quot;fluentbit&quot; },<br>          &quot;logConfiguration&quot;: {<br>            &quot;logDriver&quot;: &quot;awslogs&quot;,<br>            &quot;options&quot;: {<br>              &quot;awslogs-group&quot;: &quot;/aws/batch/your-job-firelens&quot;,<br>              &quot;awslogs-region&quot;: &quot;us-east-1&quot;,<br>              &quot;awslogs-stream-prefix&quot;: &quot;firelens&quot;<br>            }<br>          }<br>        },<br>        {<br>          &quot;name&quot;: &quot;your-app&quot;,<br>          &quot;image&quot;: &quot;your-ecr-repo/your-app:latest&quot;,<br>          &quot;essential&quot;: true,<br>          &quot;logConfiguration&quot;: {<br>            &quot;logDriver&quot;: &quot;awsfirelens&quot;,<br>            &quot;options&quot;: {<br>              &quot;Name&quot;: &quot;loki&quot;,<br>              &quot;Host&quot;: &quot;your-loki-endpoint&quot;,<br>              &quot;Port&quot;: &quot;443&quot;,<br>              &quot;Tls&quot;: &quot;On&quot;,<br>              &quot;Labels&quot;: &quot;{job=\&quot;batch-your-job\&quot;, environment=\&quot;uat\&quot;}&quot;,<br>              &quot;HTTPUser&quot;: &quot;your-username&quot;,<br>              &quot;HTTPPasswd&quot;: &quot;your-password&quot;,<br>              &quot;LineFormat&quot;: &quot;json&quot;<br>            }<br>          }<br>        }<br>      ]<br>    }<br>  ]<br>}</pre><p>Register it with:</p><pre>aws batch register-job-definition \<br>  --job-definition-name your-job-firelens \<br>  --type container \<br>  --ecs-properties file://job-definition.json</pre><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*qZ4jXUwkM_W6CrKU9Owxag.png" /></figure><p>So the question became: how do you get logs out of CloudWatch without keeping polling infrastructure running 24/7?</p><p>If you try to express the FireLens configuration in Terraform directly, you’ll likely hit:</p><pre>ClientException: When awsfirelens log driver is specified in log configuration,<br>a firelens configuration object must be configured in one of the containers</pre><p>The root cause: FirelensConfiguration didn&#39;t exist in the AWS SDK version pinned by earlier Terraform provider releases. The provider was silently dropping it. The fix landed in aws-sdk-go-v2/service/batch@v1.52.0, which was picked up by the Terraform AWS provider at <strong>v5.94.0</strong>.</p><p>If you’re on an older provider version, the shell script approach is the reliable path. The full community investigation tracking this is at <a href="https://github.com/aws/containers-roadmap/issues/2573">containers-roadmap #2573</a>.</p><h3>Debugging FireLens</h3><p>Fluent Bit’s own logs go to CloudWatch at /aws/batch/your-job-firelens. Look for:</p><pre>[output:loki:loki.1] configured</pre><p>That line means the Loki connection is healthy. If you see connection timeouts, check that a NAT Gateway exists in your VPC and your Security Group allows port 443 outbound.</p><h3>The Step Functions Wrinkle</h3><p>There’s one more gotcha that only appears when you wire Batch into a Step Functions workflow.</p><p>The natural instinct is to use the .sync integration — arn:aws:states:::batch:submitJob.sync — which submits a Batch job and waits for it to complete before advancing. Clean, simple, exactly what Step Functions is designed for.</p><p>Except: .sync doesn&#39;t work correctly with multi-container ECS-type Batch jobs. With two containers running, the sync integration can fail to resolve job completion status correctly, leaving your execution stuck.</p><p>The workaround is to drop .sync and implement your own polling loop in the state machine: submit the job with the standard (fire-and-forget) resource, then use a Wait state followed by a DescribeJobs Lambda call, check the returned status, and loop until the job reaches SUCCEEDED or FAILED. More states to build, but it&#39;s deterministic and works reliably across all job types.</p><pre>{<br>  &quot;SubmitBatchJob&quot;: {<br>    &quot;Type&quot;: &quot;Task&quot;,<br>    &quot;Resource&quot;: &quot;arn:aws:states:::batch:submitJob&quot;,<br>    &quot;Next&quot;: &quot;WaitForJob&quot;<br>  },<br>  &quot;WaitForJob&quot;: {<br>    &quot;Type&quot;: &quot;Wait&quot;,<br>    &quot;Seconds&quot;: 30,<br>    &quot;Next&quot;: &quot;CheckJobStatus&quot;<br>  },<br>  &quot;CheckJobStatus&quot;: {<br>    &quot;Type&quot;: &quot;Task&quot;,<br>    &quot;Resource&quot;: &quot;arn:aws:lambda:us-east-1:your-account:function:check-batch-status&quot;,<br>    &quot;Next&quot;: &quot;IsJobComplete&quot;<br>  },<br>  &quot;IsJobComplete&quot;: {<br>    &quot;Type&quot;: &quot;Choice&quot;,<br>    &quot;Choices&quot;: [<br>      { &quot;Variable&quot;: &quot;$.status&quot;, &quot;StringEquals&quot;: &quot;SUCCEEDED&quot;, &quot;Next&quot;: &quot;JobSucceeded&quot; },<br>      { &quot;Variable&quot;: &quot;$.status&quot;, &quot;StringEquals&quot;: &quot;FAILED&quot;, &quot;Next&quot;: &quot;JobFailed&quot; }<br>    ],<br>    &quot;Default&quot;: &quot;WaitForJob&quot;<br>  }<br>}</pre><h3>Querying in Grafana</h3><p>Once logs are flowing, LogQL makes the rest straightforward:</p><pre># All logs from the team<br>{team=&quot;your-team&quot;}</pre><pre># Lambda errors only<br>{__aws_cloudwatch_log_group=&quot;/aws/lambda/your-function&quot;} |= &quot;ERROR&quot;</pre><pre># Batch job output<br>{job=&quot;batch-your-job&quot;}</pre><pre># All AgentCore runtimes<br>{__aws_cloudwatch_log_group=~&quot;/aws/bedrock-agentcore/.*&quot;}</pre><pre># Error rate over time<br>count_over_time({env=&quot;uat&quot;} |= &quot;ERROR&quot; [5m])</pre><h3>Things We Wish We’d Known</h3><p><strong>Lambda has no log driver.</strong> This sounds obvious in retrospect, but it took a moment to internalize that the entire Lambda → Loki path has to go through CloudWatch first, because Lambda gives you no other hook. Subscription filters are the right abstraction — don’t fight it.</p><p><strong>awsfirelens is invisible in the Batch console.</strong> The dropdown only shows awslogs and splunk. You have to register job definitions via the API with raw JSON to get FireLens working. The console will display the result correctly once it&#39;s registered — it just can&#39;t create it.</p><p><strong>Terraform AWS provider must be ≥ v5.94.0</strong> for FirelensConfiguration to work in Batch ECS task definitions. Earlier versions silently drop the field. If you&#39;re hitting the &quot;firelens configuration object must be configured&quot; error, check your provider version first.</p><p><strong>.sync breaks with multi-container Batch jobs.</strong> Use a polling loop in your Step Functions state machine instead. It&#39;s more verbose but reliable.</p><p><strong>Terraform state is the subscription filter footgun.</strong> When adding new log groups to lambda-promtail, always include all existing ones in log_group_names. Omit one and Terraform will destroy its subscription filter on the next apply.</p><p><strong>Multi-region means multiple deployments.</strong> Subscription filters are regional. If you have workloads in us-east-1 and eu-west-1, you need a lambda-promtail deployment in each.</p><p><strong>Subscription filters are free.</strong> AWS charges only for the Lambda invocations they trigger, not for the filters themselves. Nine filters cost exactly the same as one.</p><h3>Where We Landed</h3><p>Nine log sources. Zero servers to maintain. Thirty minutes of actual setup time, plus a few hours of working through the Batch and Step Functions quirks. Effectively free.</p><pre>├── Lambda (6 functions)        → Subscription Filter → lambda-promtail → Loki<br>├── Step Functions (1 pipeline) → Subscription Filter → lambda-promtail → Loki<br>├── Batch jobs (Fargate)        → FireLens (Fluent Bit sidecar)          → Loki<br>└── Bedrock AgentCore           → Subscription Filter → lambda-promtail → Loki</pre><p>The clean path is lambda-promtail for anything that writes to CloudWatch, and FireLens for Fargate workloads where you control the container definition. The friction points — the missing console option, the Terraform provider version, the .sync incompatibility — are all real, but each has a clear workaround once you know it exists.</p><p>If you’re running a serverless stack on AWS and want your logs in Grafana without paying for always-on infrastructure, this is the path.</p><p><em>Tools used: </em><a href="https://github.com/grafana/lambda-promtail"><em>lambda-promtail</em></a><em>, </em><a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/using_firelens.html"><em>AWS FireLens</em></a><em>, Terraform, Grafana Loki, CloudWatch Subscription Filters</em></p><p><em>Related: </em><a href="https://github.com/aws/containers-roadmap/issues/2573"><em>containers-roadmap #2573 — awsfirelens Log Driver Support in AWS Batch with Fargate</em></a></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=3496d0f269a7" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>