惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog RSS Feed
量子位
Y
Y Combinator Blog
大猫的无限游戏
大猫的无限游戏
B
Blog
U
Unit 42
C
Check Point Blog
I
InfoQ
aimingoo的专栏
aimingoo的专栏
雷峰网
雷峰网
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 【当耐特】
人人都是产品经理
人人都是产品经理
The Cloudflare Blog
H
Help Net Security
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
H
Hackread – Cybersecurity News, Data Breaches, AI and More
J
Java Code Geeks
Microsoft Azure Blog
Microsoft Azure Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
云风的 BLOG
云风的 BLOG
宝玉的分享
宝玉的分享
爱范儿
爱范儿

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
Athena Queries Broke the Bank: How We Saved $1200 by Fixi...
Dinesh_gowth · 2026-05-20 · via DEV Community

Dinesh_gowtham

This week, our team discovered that our Amazon Athena queries were costing us $1200 more than expected. The reason was not the queries themselves, but how we were logging and optimizing them. Here's our story of finding the hidden costs and implementing a fix using Node.js and TypeScript.

The Surprising Cost of Athena Queries

We started by investigating our Athena queries, which were running using the @aws-sdk/client-athena package. We noticed that our queries were scanning entire tables, even when the queries only required a small subset of the data. This was due to unpartitioned large tables, which can be a bill disaster.

import { StartQueryExecutionCommand } from '@aws-sdk/client-athena';

const athenaClient = new AthenaClient({ region: 'us-west-2' });
const query = 'SELECT * FROM my_table';
const params = {
  QueryString: query,
  QueryExecutionContext: {
    Database: 'my_database',
  },
  ResultConfiguration: {
    OutputLocation: 's3://my-bucket/athena-results/',
  },
};

const command = new StartQueryExecutionCommand(params);
athenaClient.send(command).then((data) => {
  console.log(data);
}).catch((err) => {
  console.error(err);
});

Enter fullscreen mode Exit fullscreen mode

The error message Your query has exceeded the maximum allowed scan size should be a red flag. It indicates that your query is scanning too much data and can lead to high costs.

Logging and Optimization: Where We Went Wrong

We realized that our logging and optimization strategies were also contributing to the high costs. We were using the @aws-sdk/client-cloudwatch package to log our queries, but we were not properly configuring the log retention period. This led to a massive accumulation of logs and high costs.

import { PutLogEventsCommand } from '@aws-sdk/client-cloudwatch';

const cloudWatchClient = new CloudWatchClient({ region: 'us-west-2' });
const logGroupName = 'my-log-group';
const logStreamName = 'my-log-stream';
const logEvents = [
  {
    Message: 'This is a log message',
    Timestamp: Date.now(),
  },
];

const command = new PutLogEventsCommand({
  LogGroupName: logGroupName,
  LogStreamName: logStreamName,
  LogEvents: logEvents,
});
cloudWatchClient.send(command).then((data) => {
  console.log(data);
}).catch((err) => {
  console.error(err);
});

Enter fullscreen mode Exit fullscreen mode

Be careful with the Metric resolution in CloudWatch. 1-second metrics can cost 3x more than 1-minute metrics. Make sure to choose the right resolution for your use case.

Implementing the Fix with Node.js and TypeScript

To fix the issues, we started by optimizing our Athena queries using the async/await syntax and the satisfies operator for type safety. We also implemented a proper logging strategy using CloudWatch, with a configured log retention period.

import { StartQueryExecutionCommand } from '@aws-sdk/client-athena';
import { PutLogEventsCommand } from '@aws-sdk/client-cloudwatch';

async function executeQuery(query: string) {
  const athenaClient = new AthenaClient({ region: 'us-west-2' });
  const params = {
    QueryString: query,
    QueryExecutionContext: {
      Database: 'my_database',
    },
    ResultConfiguration: {
      OutputLocation: 's3://my-bucket/athena-results/',
    },
  };

  try {
    const command = new StartQueryExecutionCommand(params);
    const data = await athenaClient.send(command);
    console.log(data);
  } catch (err) {
    console.error(err);
  }
}

async function logMessage(message: string) {
  const cloudWatchClient = new CloudWatchClient({ region: 'us-west-2' });
  const logGroupName = 'my-log-group';
  const logStreamName = 'my-log-stream';
  const logEvents = [
    {
      Message: message,
      Timestamp: Date.now(),
    },
  ];

  try {
    const command = new PutLogEventsCommand({
      LogGroupName: logGroupName,
      LogStreamName: logStreamName,
      LogEvents: logEvents,
    });
    const data = await cloudWatchClient.send(command);
    console.log(data);
  } catch (err) {
    console.error(err);
  }
}

executeQuery('SELECT * FROM my_table').then(() => {
  logMessage('Query executed successfully');
});

Enter fullscreen mode Exit fullscreen mode

When using async/await, make sure to handle errors properly. The error message Error: Cannot read properties of undefined (reading 'send') can occur if you're not handling errors correctly.

CloudWatch Logs: The Hidden Cost We Overlooked

We also discovered that our CloudWatch logs were not being properly retained, leading to a significant accumulation of logs and high costs. We implemented a log retention period of 30 days to mitigate this issue.

import { CreateLogGroupCommand } from '@aws-sdk/client-cloudwatch';

const cloudWatchClient = new CloudWatchClient({ region: 'us-west-2' });
const logGroupName = 'my-log-group';
const retentionInDays = 30;

const command = new CreateLogGroupCommand({
  LogGroupName: logGroupName,
});

cloudWatchClient.send(command).then((data) => {
  console.log(data);
}).catch((err) => {
  console.error(err);
});

async function putRetentionPolicy() {
  const cloudWatchClient = new CloudWatchClient({ region: 'us-west-2' });
  const logGroupName = 'my-log-group';
  const retentionInDays = 30;

  try {
    const command = new PutRetentionPolicyCommand({
      LogGroupName: logGroupName,
      RetentionInDays: retentionInDays,
    });
    const data = await cloudWatchClient.send(command);
    console.log(data);
  } catch (err) {
    console.error(err);
  }
}

putRetentionPolicy();

Enter fullscreen mode Exit fullscreen mode

The error message The log group '/aws/lambda/my-function' does not exist can occur if you're trying to put a retention policy on a log group that does not exist.

The Takeaway

Here are the key takeaways from our experience:

  • Make sure to optimize your Athena queries to scan only the necessary data.
  • Implement a proper logging strategy using CloudWatch, with a configured log retention period.
  • Use the async/await syntax and the satisfies operator for type safety when executing Athena queries.
  • Be mindful of the Metric resolution in CloudWatch and choose the right resolution for your use case.
  • Handle errors properly when using async/await to avoid unexpected behavior.
  • Implement a log retention period to mitigate the accumulation of logs and high costs. Remember, it depends on the specifics of your use case, with costs varying by 20-50% based on the region, query complexity, and data size, and it depends on the metrics resolution, with 1-second metrics costing 3x more than 1-minute metrics.

Transparency notice

This article was generated by Me (Dinesh).
The topic was scouted from live AWS and Node.js ecosystem signals, and the content —
including all code examples — was written autonomously with human editing.

Published: 2026-05-20 · Primary focus: Athena

All code blocks are intended to be correct and runnable, but please verify them
against the official AWS SDK v3 docs
before using in production.

Find an error? Drop a comment — corrections are always welcome.