Build incremental data pipelines to load transactional data changes using AWS DMS, Delta 2.0, and Amazon EMR Serverless
For engineering teams, the key architectural move here is separating ingestion, transformation, and query access into distinct managed layers. AWS DMS handles historical load plus CDC into S3, while EMR Serverless turns that raw landing zone into Delta tables. That split reduces coupling between source systems and downstream analytics, but it also means teams must be deliberate about partitioning strategy, replay windows, and schema evolution handling.
The operational advantage is elasticity without cluster lifecycle work. EMR Serverless removes the burden of sizing, patching, and keeping Spark infrastructure warm, which is attractive for periodic pipelines with uneven volumes. The trade-off is that performance tuning moves from node management to job configuration: executor sizing, Spark properties, input pruning, and Delta merge behavior become the primary levers for throughput and cost control.
This pattern also introduces an integration constraint that is easy to underestimate. Native Delta access through Athena simplifies ad hoc querying, but the metadata story is not identical across the stack. Glue catalog exposure, Delta logs, and optional manifest generation create multiple synchronization points, so platform teams need a clear policy for which engine is authoritative for table definitions and how often metadata should be refreshed.
From a resilience and operations perspective, the design is strongest when pipeline cadence matches data arrival cadence. Hourly CDC loads, multi-AZ placement, CloudWatch observability, and reusable job arguments make the workflow manageable at scale, but only if retry logic, backfill handling, and permission boundaries are defined up front. In practice, the hardest part is not running one job; it is keeping the full ingestion-to-query chain predictable as tables, partitions, and consumers grow.
Solution overview
The following diagram shows the overall architecture of the solution that we implement in this post.- Data storage and data generation โ We create an Aurora PostgreSQL database and generate fictional trip data by running a stored procedure. The data will have attributes like trip ID (primary key), timestamp, source location, and destination location. Incremental data is generated in the PostgreSQL table by running custom SQL scripts.
- Data ingestion โ Steps 1 and 2 use AWS DMS, which connects to the source database and moves full and incremental data (CDC) to Amazon S3 in Parquet format. Letโs refer to this S3 bucket as the raw layer.
- Data transformation โ Steps 3 and 4 represent an EMR Serverless Spark application (Amazon EMR 6.9 with Apache Spark version 3.3.0) created using Amazon EMR Studio. The script reads input data from the S3 raw bucket, and then invokes Delta Lakeโs MERGE statements to merge the data with the target S3 bucket (curated layer). The script also creates and updates a manifest file on Amazon S3 every time the job is run to enable data access from Athena and Amazon Redshift Spectrum.
- Data access โ The EMR Serverless job has code snippets that create a Delta table in the AWS Glue Data Catalog in Step 5. Steps 6 and 7 describe using Athena and Redshift Spectrum to query data from the Delta tables using standard SQL through the AWS Glue Data Catalog.
- Data pipeline โ Step 8 describes the process for triggering the data pipeline in a periodic manner through Airflow operators using Amazon Managed Workflows for Apache Airflow (Amazon MWAA). Refer to Submitting EMR Serverless jobs from Airflow for additional details. In this post, AWS DMS has been configured to replicate data from Amazon Aurora PostgreSQL-Compatible Edition into an S3 bucket with hourly partitions. The Airflow DAG can be configured to call an EMR Serverless job to process the past X hours of data based on specific project requirements. Implementation of the Airflow setup is not explored within the scope of this post.
- Reliability โ The end-to-end architecture is made resilient with the Multi-AZ feature of EMR Serverless and using Multi-AZ deployments for AWS DMS and Amazon Aurora PostgreSQL-Compatible Edition. When you submit jobs to an EMR Serverless application, those jobs are automatically distributed to different Availability Zones in the Region. A job is run in a single Availability Zone to avoid performance implications of network traffic across Availability Zones. In case an Availability Zone is impaired, a job submitted to your EMR Serverless application is automatically run in a different (healthy) Availability Zone. When using resources in a private VPC, EMR Serverless recommends that you specify the private VPC configuration for multiple Availability Zones so that EMR Serverless can automatically select a healthy Availability Zone.
- Cost optimization โ When you run Spark or Hive applications using EMR Serverless, you pay for the amount of vCPU, memory, and storage resources consumed by your applications, leading to optimal utilization of resources. There is no separate charge for Amazon Elastic Compute Cloud (Amazon EC2) instances or Amazon Elastic Block Store (Amazon EBS) volumes. For additional details on cost, refer to Amazon EMR Serverless cost estimator.
- Performance efficiency โ You can run analytics workloads at any scale with automatic on-demand scaling that resizes resources in seconds to meet changing data volumes and processing requirements. EMR Serverless includes the Amazon EMR performance-optimized runtime for Apache Spark and Hive. The Amazon EMR runtime for Spark is 100% API-compatible with OSS Spark and is over 3.5 times as fast as the standard open-source, so your jobs run faster and incur less compute costs. With fast and fine-grained scaling in EMR Serverless, if a pipeline runs daily and needs to process 1 GB of data one day and 100 GB of data another day, EMR Serverless automatically scales to handle that load.
- Monitoring โ EMR Serverless sends metrics to Amazon CloudWatch at the application and job level every 1 minute. You can set up a single-view dashboard in CloudWatch to visualize application-level and job-level metrics using an AWS CloudFormation template provided on the EMR Serverless CloudWatch Dashboard GitHub repository. Also, EMR Serverless can store application logs in a managed storage, Amazon S3, or both based on your configuration settings. After you submit a job to an EMR Serverless application, you can view the real-time Spark UI or the Hive Tez UI for the running job from the EMR Studio console or request a secure URL using the GetDashboardForJobRun API. For completed jobs, you can view the Spark History Server or the Persistent Hive Tez UI from the EMR Studio console.
- Connect to the Aurora PostgreSQL instance and generate a sample dataset.
- Set up a data pipeline for loading data from Amazon Aurora PostgreSQL-Compatible Edition into Delta Lake on Amazon S3 and query using Athena:
- Start the AWS DMS task to perform full table load and capture ongoing replication to the S3 raw layer.
- Run the EMR Serverless Spark application to load data into Delta Lake.
- Query the Delta tables (native tables) through Athena.
- Run the data pipeline to capture incremental data changes into Delta Lake:
- Generate an incremental (CDC) dataset and insert it into the Aurora PostgreSQL database.
- Run the EMR Serverless Spark application to merge CDC data in the S3 curated layer (incremental load).
- Query the Delta Lake tables through Athena to validate the merged data.
Prerequisites
We use a CloudFormation template to provision the AWS resources required for the solution. The CloudFormation template requires you to select an EC2 key pair. This key is configured on an EC2 instance that lives in the public subnet. We use this EC2 instance to connect to the Aurora PostgreSQL instance that lives in the private subnet. Make sure you have a key in the Region where you deploy the template. If you donโt have one, you can create a new key pair. To walk through this post, we use Delta Lake version > 2.0.0, which is supported in Apache Spark 3.2.x. Choose the Delta Lake version compatible with your Spark version by visiting the Delta Lake releases page. We use an EMR Serverless application with version emr-6.9.0, which supports Spark version 3.3.0.Deploy your resources
To provision the resources needed for the solution, complete the following steps:- Choose Launch Stack:
- For Stack name, enter
emr-serverless-deltalake-blog. - For DatabaseUserName, enter the user name for logging in to Amazon Aurora PostgreSQL-Compatible Edition. Keep the default value if you donโt want to change it.
- For DatabasePassword, enter the password for logging in to Amazon Aurora PostgreSQL-Compatible Edition.
- For ClientIPCIDR, enter the IP address of your SQL client that will be used to connect to the EC2 instance. We use this EC2 instance to connect to the Aurora PostgreSQL database.
- For KeyName, enter the key pair to be used in your EC2 instance. This EC2 instance will be used as a proxy to connect from your SQL client to the Aurora PostgreSQL source database.
- For EC2ImageId, PrivateSubnet1CIDR, PrivateSubnet2CIDR, PublicSubnetCIDR, and VpcCIDR, keep the default values or choose appropriate values for the VPC and EC2 image for your specific environment.
- Choose Next.
- Choose Next again.
- On the review page, select I acknowledge that AWS CloudFormation might create IAM resources with custom names.
- Choose Create stack.
- S3 raw and curated buckets
- Aurora PostgreSQL database
- AWS DMS migration task, replication instance, and other resources
- EC2 instance for running data ingestion scripts
- AWS Identity and Access Management (IAM) roles and policies needed to perform the necessary activities as part of this solution
- VPC, subnets, security groups, and relevant network components
- AWS Lambda functions that perform setup activities required for this workflow
- Additional components needed for running the EMR Serverless workflow
<CloudFormation template name>-rawS3bucket-<random string>. Make a note of the S3 path to the emr_delta_cdc.py script; you need this information while submitting the Spark job via the EMR Serverless application.
Connect to the Aurora PostgreSQL instance and generate a sample dataset
Connect to the Aurora PostgreSQL endpoint using your preferred client. For this post, we use the PSQL command line tool. Note that the IP address of the client machine from which youโre connecting to the database must be updated in the Aurora PostgreSQL security group. This is done by the CloudFormation template based on the input parameter value for ClientIPCIDR. If youโre accessing the database from another machine, update the security group accordingly.- Connect to your EC2 instance from the command line using the public DNS of the EC2 instance from the CloudFormation template output.
- Log in to the EC2 instance and connect to the Aurora PostgreSQL instance using the following commands (the Aurora PostgreSQL endpoint is available on the Outputs tab of the CloudFormation stack):
psql -h << Aurora PostgreSQL endpoint >> -p 5432 -U <<username>> -d emrdelta_source_db
- Run the following commands to create a schema and table for the fictional trip dataset:
create schema delta_emr_source; create table delta_emr_source.travel_details (trip_id int PRIMARY KEY,tstamp timestamp, route_id varchar(2),destination varchar(50),source_location varchar(50));
- Create the following stored procedure to generate the records for the trip dataset and insert the records into the table.
create or replace procedure delta_emr_source.insert_records(records int) language plpgsql as $ declare max_trip_id integer; begin --get max trip_id select coalesce(max(trip_id),1) into max_trip_id from delta_emr_source.travel_details; --insert records for i in max_trip_id+1..max_trip_id+records loop INSERT INTO delta_emr_source.travel_details (trip_id, tstamp, route_id,destination,source_location) values (i, current_timestamp, chr(65 + (i % 10)),(array['Seattle', 'New York', 'New Jersey', 'Los Angeles', 'Las Vegas', 'Tucson', 'Washington DC', 'Philadelphia', 'Miami', 'San Francisco'])[(floor(random() * 10))+1],(array['Seattle', 'New York', 'New Jersey', 'Los Angeles', 'Las Vegas', 'Tucson', 'Washington DC', 'Philadelphia', 'Miami', 'San Francisco'])[(floor(random() * 10))+1]); end loop; commit; raise notice 'Inserted record count - %', records; end; $; - Call the preceding stored procedure to insert 20,000 records into the Aurora PostgreSQL database:
call delta_emr_source.insert_records(20000); - After the stored procedure is complete, verify that the records have been inserted successfully:
select count(*) from delta_emr_source.travel_details;
Set up a data pipeline for loading data into Delta tables on Amazon S3 and query using Athena
In this section, we walk through the steps to set up a data pipeline that loads data from Amazon Aurora PostgreSQL-Compatible Edition into Delta tables on Amazon S3 and then query the data using Athena.Start the AWS DMS task to perform full table load to the S3 raw layer
To perform the full table load, complete the following steps:- On the AWS DMS console, choose Database migration tasks in the navigation pane.
- Select the task that was created by the CloudFormation template (
emrdelta-postgres-s3-migration). - On the Actions menu, choose Restart/Resume.
- Wait for the job to complete.
- Navigate to the S3 bucket created from the CloudFormation template to store raw data from AWS DMS.The bucket will have the naming structure
<CloudFormation template name>-rawS3bucket-<random string>. - Navigate to the folder
delta_emr_source/travel_detailsin the raw S3 bucket. You can verify the S3 folder has Parquet data populated from the AWS DMS task.
Run the EMR Serverless Spark application to load data into Delta tables
We use EMR Studio to manage and submit jobs in an EMR Serverless application.- Launch EMR Studio and create an EMR Serverless application.
- For Name, enter
emr-delta-blog. - For Type, choose Spark.
- For Release version, choose your release version.
- For Architecture, select x86_64.
- For Application setup options, select Choose default settings.
- Choose Create application and verify that the EMR application has been created successfully on the Amazon EMR console.
- Choose
emr_delta_blogand then choose Start application. You can verify that the EMR application has started successfully on the Amazon EMR console, as shown in the following screenshot.
The application will move to Stopped status after a period of inactivity. When you submit the job to the application, it will start again and start the job. This provides cost savings because the jobs are run on demand as opposed to maintaining a running EMR cluster.
- While the application is in Started status, choose Submit job to submit the job to the application.
- For Name, enter
emr-delta-load-job. - For Runtime role, choose
emrserverless-execution-role. - For S3 URI, enter the S3 (raw bucket) path where the script
emr_delta_cdc.pyis uploaded. - For Script arguments, enter
["I","delta_emr_source","9999-12-31-01","travel_details","route_id"].
- I โ The first argument represents the data load type. The allowed values are I for full load and U for incremental data load.
- delta_emr_source โ The second argument represents the source database schema from which data is being migrated through the AWS DMS task.
- 9999-12-31-01 โ The third argument represents the partition from which data needs to be loaded in an incremental fashion. This argument is used only during CDC data load; for full load, we have provided a default value (9999-12-31-01).
- travel_details โ The fourth argument represents the source database table from which data is being migrated through the AWS DMS task. Use a semicolon as a delimiter when entering multiple tables.
- route_id โ The fifth argument represents the partition keys on which the table data should be partitioned when stored in the S3 curated bucket. Use a semicolon as a delimiter when entering comma-separated partition keys for multiple tables.
I along with the new table names) and then change the argument list of the existing EMR Serverless job to add those new tables to capture incremental data load going forward.
EMR Serverless version 6.9.0 comes pre-installed with Delta version 2.1.0. Refer to About Amazon EMR Releases for more details about pre-installed libraries and applications for a specific Amazon EMR release. Before this, we have to upload the Delta JAR files to an S3 bucket in your account and provide the JAR file path in the application configurations using the spark.jars option. In this walkthrough, we create an EMR Serverless 6.9.0 application and use the pre-installed Delta jars from Amazon EMR.
- Under Spark properties, choose Edit in text and enter the following configurations:
--conf spark.jars=/usr/share/aws/delta/lib/delta-core.jar,/usr/share/aws/delta/lib/delta-storage.jar --conf spark.sql.extensions=io.delta.sql.DeltaSparkSessionExtension --conf spark.sql.catalog.spark_catalog=org.apache.spark.sql.delta.catalog.DeltaCatalog --conf spark.submit.pyFiles=/usr/share/aws/delta/lib/delta-core.jar --conf spark.hadoop.hive.metastore.client.factory.class=com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory
- Leave the rest of the configurations at their default and choose Submit job.
- Wait for the job to complete successfully. You can verify this on the EMR Serverless console.
- Additionally, go to the S3 location (the curated bucket created by AWS CloudFormation) and verify that the Delta files are created along with the manifest file.
- Select a job run and then choose Spark History Server (Completed jobs) on the View Application UIs menu.
You can now use the Spark History Server UI to navigate to various tabs and analyze the job run in a detailed manner. For Spark error and output logs, you can navigate to the Executors tab and explore the driver or executor logs as required. This can help you to debug the job in case of failures by looking at the Spark logs.You can also choose Spark UI (Running jobs) to track the progress of the EMR Serverless Spark jobs while they are running.
from pyspark.sql import SparkSession
from datetime import datetime
from pyspark.sql.functions import *
from pyspark.sql.window import Window
import boto3
import sys
from delta import *
# S3 bucket location, auto-populated for this post. Replace for other jobs
raw_bucket="<<raw_bucket_name>>"
curated_bucket= "<<curated_bucket_name>>"
spark = (
SparkSession.builder.appName("SparkSQL")
.config("spark.sql.extensions", "io.delta.sql.DeltaSparkSessionExtension")
.config("spark.sql.catalog.spark_catalog", "org.apache.spark.sql.delta.catalog.DeltaCatalog")
.enableHiveSupport()
.getOrCreate()
)
#Check for argument list and if it doesn't match the expected argument count, exit the program
if len(sys.argv) != 6:
print("This script requires 5 arguments for successful execution - Load_type,database_schema,CDC_path,source_table,Partition_keys")
print(sys.argv)
sys.exit(0)
s3 = boto3.client('s3')
# Split table names into a list if there are more than one table seperated by semicolon
tables = sys.argv[4].split(";")
schema = sys.argv[2]
load_type = sys.argv[1]
cdc_partition = sys.argv[3]
deltaHivePath = "s3://" + curated_bucket + "/" + schema + "/"
columns_to_drop = ["Op","schema_name", "table_name", "update_ts_dms", "tstamp"]
db_name = "emrserverless_delta"
# Split table partition keys into a list if there are more than one table separated by semicolon
partition_keys = sys.argv[5].split(";")
# Exit if length of table names and partition keys are different to ensure data is provided for all tables.
if len(tables)!=len(partition_keys):
print("Please enter partition keys for all tables. if partition key is not present enter empty semicolon - T1_PK;;T3PK")
sys.exit(0)
i = 0
while i < len(tables):
table = tables[i]
partition_key = partition_keys[i].split(",")
print(partition_key)
if load_type == 'I':
print("Moving to Full-load logic for the table", table)
# Read the data from the raw bucket
source_df1 = spark.read.format("parquet").load(
"s3://" + raw_bucket + "/" + schema + "/" + table + "/")
# There is no target table in Delta format. Loading for the first time
# The following code segment populates Delta table in S3 and also
# updated the Glue catalog for querying with Athena.
additional_options = {"path": deltaHivePath + table + "/"}
if columns_to_drop is not None and columns_to_drop != '':
source_df1 = source_df1.drop(*columns_to_drop)
#Check for presence of partition key and before writing data to Curated bucket
if partition_key[0]:
source_df1.write.mode("append")\
.format("delta")\
.partitionBy(*partition_key)\
.options(**additional_options)\
.saveAsTable(db_name + ".spark_" + table)
else:
source_df1.write.mode("append")\
.format("delta")\
.options(**additional_options)\
.saveAsTable(db_name + ".spark_" + table)
# Generate symlink for Amazon Redshift Spectrum to read data
deltaTable = DeltaTable.forPath(spark, deltaHivePath + table + "/")
deltaTable.generate("symlink_format_manifest")
else:
print("Moving to upsert logic, Reading data from partition - ",cdc_partition)
# The below logic will verify if the CDC path has data before proceeding with
# incremental load. if CDC path is not available for a specific table the load
# process is skipped to avoid spark read error.
resp = s3.list_objects_v2(
Bucket=raw_bucket,
Prefix=schema +"/" +table +"/" +cdc_partition,
Delimiter='/',
MaxKeys=1)
if 'CommonPrefixes' in resp:
update_df = spark.read.format("parquet").load(
"s3://" + raw_bucket + "/" + schema + "/" + table + "/" + cdc_partition + "/")
# Get recent record for each primary key to update the recent transaction to the Delta table
# This step is needed to de-dup transactions like inserts and deletes within the same batch
sort_order = Window.partitionBy(
col('trip_id')).orderBy(
col('update_ts_dms').desc())
update_df = update_df.withColumn("rec_val", row_number().over(
sort_order)).filter("rec_val=1").drop("rec_val")
# upsert script using Merge operation. The below script updates/inserts data
# on all columns. In case you need to insert/update specific columns
# use whenNotMatchedInsert/whenMatchedUpdate functions and parameterize the input for each table
deltaTable = DeltaTable.forPath(spark, deltaHivePath + table + "/")
deltaTable.alias('trg') \
.merge(update_df.alias('src'),'trg.trip_id = src.trip_id')\
.whenNotMatchedInsertAll(condition="src.Op = 'I'") \
.whenMatchedUpdateAll(condition="src.Op='U'") \
.whenMatchedDelete(condition="src.Op = 'D'") \
.execute()
# Generate symlink for Amazon Redshift Spectrum to read data
deltaTable.generate("symlink_format_manifest")
else:
print("The path is empty for table -", table)
i = i + 1
print("The Job has completed execution...")
Monitor EMR Serverless application using CloudWatch dashboards
We can optionally monitor the EMR Serverless application using CloudWatch dashboards by installing the CloudFormation template from the EMR Serverless CloudWatch Dashboard GitHub repository.Follow the instructions on the Getting started section on the GitHub repository and deploy the CloudFormation template in your account. You need to provide the EMR Serverless application ID as a parameter while deploying the CloudFormation stack, which can be obtained on the EMR Studio Applications page as shown in the following screenshot.Query the Delta tables through Athena
Previously, Delta tables were accessed through Athena by generating the manifest files (which maintain the list of data files to read for querying a Delta table). With the newly launched support in Athena for reading native Delta tables, itโs no longer required to generate and update manifest files. The Athena SQL engine version 3 can directly query native Delta tables. If youโre using an older engine version, change the engine version. Navigate to the Athena console and start querying the data. Run a SELECT query and fetch the first 10 records to verify the data:SELECT * FROM "AwsDataCatalog"."emrserverless_delta"."spark_travel_details" limit 10;
Run the data pipeline to load incremental data changes into the Delta tables
In this section, we walk through the steps to run the data pipeline.Generate an incremental (CDC) dataset and insert it into the Aurora PostgreSQL database
- Log in to the EC2 instance via SSH and using the PSQL CLI, run the following SQL commands to generate CDC data on the source database:
update delta_emr_source.travel_details set destination='Tucson' where destination='Miami';call delta_emr_source.insert_records(200);delete from delta_emr_source.travel_details where destination='Los Angeles';
- Navigate to the AWS DMS console and verify whether the incremental records are populated to the S3 raw bucket by the replication task.
Run the EMR Serverless Spark application to merge CDC data in the S3 curated layer (incremental load)
After the AWS DMS task has successfully loaded the incremental data, submit the Spark job on the EMR Serverless application to load the incremental data (CDC) with the following script arguments:["U", "delta_emr_source", "2022-10-25-21", "travel_details","route_id"]
- Under Spark properties, choose Edit in text and enter the following configurations:
--conf spark.jars=/usr/share/aws/delta/lib/delta-core.jar,/usr/share/aws/delta/lib/delta-storage.jar --conf spark.sql.extensions=io.delta.sql.DeltaSparkSessionExtension --conf spark.sql.catalog.spark_catalog=org.apache.spark.sql.delta.catalog.DeltaCatalog --conf spark.submit.pyFiles=/usr/share/aws/delta/lib/delta-core.jar --conf spark.hadoop.hive.metastore.client.factory.class=com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory
- When the job is successful, verify in Amazon S3 that more files are created in the
_delta_logfolder, capturing the changes from the current run.
Query the Delta tables through Athena to validate the merged data
Go to the Athena console to query the data and validate count to ensure that the table contains the most recent data:
SELECT destination, count(*) FROM "AwsDataCatalog"."emrserverless_delta"."spark_travel_details" group by destination;
Clean up
To avoid incurring ongoing charges, clean up your infrastructure by deleting the stack from the AWS CloudFormation console. Delete the EMR Serverless application and any other resources you created during this exercise.Conclusion
In this post, we demonstrated how to create a transactional data lake with Delta table format using EMR Serverless and AWS DMS. With the flexibility provided by EMR Serverless, you can use the latest version of open-source Delta framework on EMR Serverless (with the latest version of Spark) in order to support a wider range of transactional data lake needs based on various use cases. Now you can build a transactional data lake for your organization with Delta table format and access data using Athena and Redshift Spectrum for various analytical workloads. You could use this high-level architecture for any other use cases where you need to use the latest version of Spark on EMR Serverless.About the Authors
Enjoyed this article? Sign up for our newsletter to receive regular insights and stay connected.
