Lesson 3 of 8 Amazon Athena and Data Analysis

Querying Sources Beyond S3 with Athena Federated Query

This lesson explains how Athena federated query reads data in databases, logs and other sources through Lambda-based connectors and joins it with lake data in one SQL statement, without first copying it.

Learning outcomes

After completing this lesson, you will be able to:

  • Describe how data source connectors work.
  • Deploy and configure a connector.
  • Decide when federation is better than copying data.

How federated query works

Athena's engine reads data from Amazon S3 natively. To read other sources, it uses data source connectors, which run as AWS Lambda functions. When a query refers to a table in another source, Athena calls the connector, which reads the data from the source, applies any filters it can push down to the source, and returns rows to Athena in a common format. Athena then joins them with data from S3 or other connectors in the same query.

AWS provides connectors for many sources, including Amazon DynamoDB, Amazon RDS and Aurora with MySQL or PostgreSQL, Amazon Redshift, Amazon CloudWatch Logs, Amazon DocumentDB and Amazon OpenSearch Service. Connectors also exist for on-premises databases through JDBC and for software-as-a-service sources, and teams can write their own with the Athena Query Federation SDK.

The following diagram shows how the query reaches each source.

How the investigation's query reaches three sources. Athena reads the clickstream data in S3 natively and calls Lambda connectors for DynamoDB and Aurora MySQL, which push filters down to their sources, read credentials from Secrets Manager and spill large results to an S3 bucket. Athena joins the rows in one SQL query.
Each connector is deployed once and reused by later queries.

Deploying connectors

A connector must be deployed and configured before queries can use it. It is deployed from the Athena console or the AWS Serverless Application Repository, and it can be registered as an AWS Glue connection so that its tables appear in the catalogue. Because the connector is a Lambda function, it needs network access to its source: for a database in a VPC, the function is configured to run in that VPC with a security group that the database allows.

Two further pieces are required. Lambda responses have a size limit, so each connector needs an S3 spill bucket where it writes results that are too large to return directly. And the credentials for the source are stored in AWS Secrets Manager, from which the connector retrieves them, so no passwords appear in the configuration.

Select all that apply

Deploying a connector

Exam skills 3.1.7 · 2.2.5

Identify what an Athena federated query connector needs.

Log in to try this activity Interactive activities are free with an account.

When to federate

Athena Federated Query uses Lambda-based connectors to query sources outside S3, such as DynamoDB, RDS or other databases, in the same SQL as lake tables. Each query reads the source live, through a Lambda function, so it suits occasional, ad hoc joins across systems and exploring data before deciding whether a pipeline is worth building. For heavy analytics that run regularly, or any query that would slow the source system, it is better to copy the data into S3 or Redshift, where it can be queried efficiently without touching production. The following table summarises the choice.

Use federated query forInstead, copy data to S3 or Redshift for
Occasional, ad hoc joins across systemsHeavy, recurring analytics
Exploring data before building a pipelineAnything that would slow down or load the source system
Sort into groups

Federate or copy?

Exam skills 3.1.7 · 2.1.5 · 1.2.3

Decide whether Athena federated query fits each use.

Log in to try this activity Interactive activities are free with an account.
Worked example

One query across three sources

An analyst needs to join customer records in DynamoDB, orders in Aurora MySQL and clickstream data in S3, once, for an investigation. The following table compares the two approaches.

ApproachEffortSuitable for
Build ETL pipelines to copy everything into S3DaysRegular, high-volume reporting
Athena federated query with DynamoDB and MySQL connectorsHoursOccasional queries across sources

Federated query is the right choice for an occasional investigation. If the same join became a daily report, copying the data into the lake (or using zero-ETL) would be faster and avoid putting query load on the operational databases.

Check yourself

Where do Athena federated query connectors run?

Show answer

As Lambda functions.

Exam Tips

Remember for the exam

  • SQL over DynamoDB or CloudWatch Logs calls for Athena federated query.
  • Federated query connectors run as Lambda functions.
  • To join a database table with S3 data in one query, use a federated query.
  • Connectors need network access to the source and an S3 spill bucket.
  • For heavy, recurring analytics, land the data in S3 or Redshift rather than federating.