Tutorials
How to Use Amazon Athena with DuckDB and TypeScript
Amazon Athena introduces a DuckDB-powered query engine. Learn how to configure a workgroup, execute SQL remotely with TypeScript, and retrieve results without managing a database server.
A SQL query written for DuckDB can run directly on a developer’s computer. For many analytical tasks, that is sufficient. But what happens when the dataset grows beyond what a single machine can comfortably process?
Moving the query to a cloud analytics service has traditionally meant adapting it to another SQL dialect.
On December 1, 2026, AWS announced a new Amazon Athena query engine powered by DuckDB 2.0. Developers can now execute DuckDB SQL through Athena’s managed infrastructure without running their own database server.
The engine is currently available in Preview.
AWS reports up to twice the query performance and up to 50% lower costs for queries processing 100 GB or more compared with Athena engine version 3. These are AWS’s reported figures, not independent benchmark results.
For Node.js developers, perhaps the more interesting detail is that the existing Athena API can be used to communicate with the new engine.
How DuckDB works inside Amazon Athena
DuckDB is commonly used as an embedded analytical database. It executes queries within the environment where it is installed, using the available CPU, memory, and storage.
Athena approaches execution differently.
Your application submits a SQL statement to AWS, where Athena manages the computational resources needed to execute it. Larger queries can be distributed across multiple machines through DuckDB Scales.
Your Node.js application does not need to install DuckDB locally.
It communicates with Athena through the AWS SDK, receives a query execution ID, and retrieves the results once the operation finishes.
1. Create a DuckDB workgroup
Before writing any TypeScript, configure the environment where the query will run.
As of December 1, 2026, the Preview is available in five AWS regions:
| Region | Region identifier |
|---|---|
| US East (N. Virginia) | us-east-1 |
| US East (Ohio) | us-east-2 |
| US West (Oregon) | us-west-2 |
| Europe (Ireland) | eu-west-1 |
| Asia Pacific (Tokyo) | ap-northeast-1 |
Open the Amazon Athena console in one of these regions.
Navigate to Workgroups and create a workgroup named duckdb-demo. Select Athena DuckDB as its analytics engine.
For this tutorial, use Athena-managed query results. This allows Athena to manage result storage without requiring you to configure a separate S3 output bucket.
Creating a separate workgroup is preferable to immediately switching an existing production workgroup. The DuckDB engine does not yet support every feature available in Athena engine version 3.
Before continuing, run this statement in the Athena query editor:
SELECT SUM(i) AS total
FROM range(1000) AS numbers(i);
The expected result is:
| total |
|---|
| 499500 |
DuckDB’s range(1000) generates integers from 0 through 999. The query calculates their sum.
This example deliberately avoids external tables. There is no need to upload a dataset or configure an AWS Glue table merely to check whether the new engine can execute a DuckDB SQL statement.
2. Prepare a Node.js TypeScript project
Create a project and install the AWS SDK v3 Athena client:
mkdir athena-duckdb-demo
cd athena-duckdb-demo
npm init -y
npm install @aws-sdk/client-athena
npm install -D typescript tsx @types/node
The tsx package allows us to execute TypeScript directly for this example.
Your AWS credentials must already be configured. If you use AWS IAM Identity Center with a configured CLI profile, authenticate with:
aws sso login --profile my-profile
Verify the active identity:
aws sts get-caller-identity --profile my-profile
The identity used by the application needs permission to start queries, inspect their execution status, and retrieve results from the selected workgroup.
The AWS SDK can obtain credentials through its default credential provider chain. There is no reason to place access keys directly inside the TypeScript source.
3. Submit the DuckDB SQL query
Create index.ts:
import {
AthenaClient,
StartQueryExecutionCommand,
} from "@aws-sdk/client-athena";
const client = new AthenaClient({
region: "eu-west-1",
});
async function main() {
const response = await client.send(
new StartQueryExecutionCommand({
WorkGroup: "duckdb-demo",
QueryString: `
SELECT SUM(i) AS total
FROM range(1000) AS numbers(i)
`,
}),
);
if (!response.QueryExecutionId) {
throw new Error("Athena returned no execution ID");
}
console.log(response.QueryExecutionId);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});
Use the region where you created your workgroup.
If you authenticate through a named AWS profile, run the script as follows:
AWS_PROFILE=my-profile npx tsx index.ts
In PowerShell:
$env:AWS_PROFILE = "my-profile"
npx tsx index.ts
The program prints an execution ID rather than the calculated sum.
Why?
Because StartQueryExecutionCommand submits a query. It does not wait for Athena to finish processing it.
The execution may still be queued when the API returns successfully.
4. Wait for the query to finish
Athena separates query submission, execution status, and result retrieval into different API operations.
flowchart TD
A["Node.js application"] --> B["StartQueryExecution"]
B --> C["Query execution ID"]
C --> D["GetQueryExecution"]
D --> E{"Query state"}
E -->|QUEUED or RUNNING| F["Wait"]
F --> D
E -->|SUCCEEDED| G["GetQueryResults"]
E -->|FAILED or CANCELLED| H["Handle error"]
G --> I["SQL result rows"]
To retrieve the result, the application must first establish that execution has succeeded.
Replace index.ts with the following complete example:
import {
AthenaClient,
StartQueryExecutionCommand,
GetQueryExecutionCommand,
GetQueryResultsCommand,
} from "@aws-sdk/client-athena";
import { setTimeout as sleep } from "node:timers/promises";
const client = new AthenaClient({
region: "eu-west-1",
});
async function waitForQuery(queryId: string) {
const deadline = Date.now() + 120_000;
while (Date.now() < deadline) {
const response = await client.send(
new GetQueryExecutionCommand({
QueryExecutionId: queryId,
}),
);
const execution = response.QueryExecution;
const status = execution?.Status;
switch (status?.State) {
case "SUCCEEDED":
console.log(
"Engine:",
execution?.EngineVersion?.EffectiveEngineVersion,
);
return;
case "FAILED":
case "CANCELLED":
throw new Error(
status.StateChangeReason ??
`Query ${status.State}`,
);
case "QUEUED":
case "RUNNING":
await sleep(2000);
break;
default:
throw new Error("Unknown query state");
}
}
throw new Error(`Polling timed out: ${queryId}`);
}
async function main() {
const response = await client.send(
new StartQueryExecutionCommand({
WorkGroup: "duckdb-demo",
QueryString: `
SELECT SUM(i) AS total
FROM range(1000) AS numbers(i)
`,
}),
);
const queryId = response.QueryExecutionId;
if (!queryId) {
throw new Error("Athena returned no execution ID");
}
console.log("Query ID:", queryId);
await waitForQuery(queryId);
const result = await client.send(
new GetQueryResultsCommand({
QueryExecutionId: queryId,
}),
);
const rows = result.ResultSet?.Rows ?? [];
console.table(
rows.map((row) =>
(row.Data ?? []).map(
(cell) => cell.VarCharValue ?? null,
),
),
);
}
main()
.catch((error) => {
console.error(error);
process.exitCode = 1;
})
.finally(() => {
client.destroy();
});
The program checks the execution state every two seconds and stops polling after a configured deadline.
Once Athena reports SUCCEEDED, it retrieves the result rows.
It also prints the effective engine version. This is useful when investigating SQL compatibility problems, since a workgroup’s name alone does not establish which engine executed the query.
Notice that a polling timeout does not cancel the remote query. If the application needs to terminate an ongoing execution, Athena provides a separate StopQueryExecutionCommand.
5. Understand the returned data
Although the SQL statement calculates an integer, the traditional GetQueryResults API represents individual cell values through VarCharValue.
A numeric SQL value can therefore arrive as a JavaScript string:
"499500"
For this particular query, converting the value to a number is safe.
For larger integers, however, JavaScript’s Number type may lose precision. Depending on the SQL column type, retaining the string or converting it to BigInt may be more appropriate.
Athena also returns column metadata through:
result.ResultSet?.ResultSetMetadata?.ColumnInfo
This allows an application to inspect the reported SQL types before converting their values.
Another consideration is pagination. The example retrieves one page because the query returns only one data row.
For larger results, follow the NextToken returned by GetQueryResults until no further token is available.
The new DuckDB engine also introduces Apache Arrow streaming capabilities. Those can be more suitable for large analytical result sets, although they are separate from the traditional row-based API used in this tutorial.
6. DuckDB SQL is not identical to Athena engine version 3
Athena’s previous SQL engine is based on Trino.
Both engines support familiar SQL operations, but they do not necessarily implement the same functions, syntax, or data types.
For example, DuckDB allows:
SELECT *
FROM range(5);
The resulting values are 0, 1, 2, 3, and 4.
A corresponding expression in Trino can use sequence() together with UNNEST():
SELECT i
FROM UNNEST(sequence(0, 4)) AS numbers(i);
The result is equivalent, but the syntax differs.
That distinction becomes more significant when migrating larger analytical queries involving date functions, nested data, casts, or engine-specific expressions.
A successful connection through the AWS SDK does not guarantee that an existing Trino query will run unchanged on DuckDB.
Current Preview limitations
Several Athena engine version 3 features are not yet supported by the DuckDB engine.
These include Lake Formation integration, federated queries, views and materialized views, user-defined functions, and certain table formats.
The Preview also requires each query to contain a single SQL statement.
AWS recommends keeping production workloads on Athena engine version 3 until general availability.
There is also a cost consideration. Serverless execution removes the need to maintain database infrastructure, but it does not make query execution free.
The performance and cost improvements reported by AWS concern larger analytical workloads. Our small range() query demonstrates API integration, not the claimed performance improvements.
What changes for Node.js developers?
From the application’s perspective, much of the integration remains familiar.
The AWS SDK submits a query, Athena executes it, and the application retrieves its results through an execution ID.
What changes is the SQL engine behind that interface.
A developer who prototypes analytical queries with DuckDB locally can now evaluate those queries against Athena’s managed infrastructure instead of immediately translating everything into another SQL dialect.
For larger workloads, the engine can distribute execution across multiple machines.
That does not remove every compatibility issue between local DuckDB and Athena. The managed service still has its own permissions, supported features, storage arrangements, and execution limits.
For now, a separate Preview workgroup is a sensible place to investigate those differences without changing the environment used by existing production queries.