Spark on AWS
Run Apache Spark at enterprise scale in the cloud — with AWS EMR, Glue, S3, and Databricks on AWS.
Start Learning Spark on AWSWhat You'll Learn
- The AWS data engineering stack: S3, EMR, Glue, Athena, and Redshift
- How to read from and write to S3 in PySpark using the Hadoop S3A connector
- Creating and running Spark jobs on Amazon EMR
- AWS Glue — serverless ETL with PySpark and the Glue DynamicFrame
- Querying S3 data with Amazon Athena (serverless SQL)
- IAM roles and S3 bucket policies for secure data access
- Delta Lake on AWS S3 — building a Lakehouse architecture
- Cost optimisation — Spot instances, S3 storage classes, and job sizing
Introduction to Spark on AWS
Running Spark on AWS means you can process petabytes of data without managing physical servers. Instead of buying and maintaining a Spark cluster, you provision one on-demand, run your job, and pay only for the compute you used. AWS offers three main ways to run Spark: Amazon EMR (Elastic MapReduce) — a managed Spark cluster; AWS Glue — a serverless ETL service; and Databricks on AWS — the most powerful fully managed Spark platform used by thousands of enterprise data teams.
Amazon S3 is the backbone of every AWS data platform. It's where your raw data lands, where processed data is stored, and where Delta Lake tables live. Spark on AWS reads from and writes to S3 natively, treating it as a distributed filesystem. With S3's virtually unlimited storage and 11-nines durability, it's the standard data lake storage layer.
The modern AWS data architecture follows the Medallion/Lakehouse pattern: raw data lands in S3 as JSON or CSV (Bronze layer), Glue or EMR jobs clean and standardise it into Parquet/Delta (Silver layer), and further aggregations create analytics-ready tables (Gold layer). These are then queried by Athena (serverless SQL on S3), Redshift (data warehouse), or connected to BI tools like QuickSight and Tableau.
Video Tutorials
Handpicked free YouTube videos to accelerate your understanding
AWS Glue PySpark ETL — Full Tutorial
Build serverless ETL jobs with AWS Glue and PySpark — DynamicFrames, Glue Data Catalog, job bookmarks, and S3 output explained.
Amazon EMR with PySpark — Complete Guide
Run PySpark jobs on Amazon EMR clusters — set up, submit Spark steps, monitor jobs, and cut costs with Spot Instances.
PySpark job reading from S3 and writing Delta to S3 (AWS EMR / Glue)
Copy the code below and paste it into your Python environment or our free online compiler.
from pyspark.sql import SparkSession
from pyspark.sql.functions import col, to_date, year, month
from delta.tables import DeltaTable
# 1. Create SparkSession with S3 and Delta Lake support
spark = SparkSession.builder \
.appName("SalesETL_S3") \
.config("spark.sql.extensions",
"io.delta.sql.DeltaSparkSessionExtension") \
.config("spark.sql.catalog.spark_catalog",
"org.apache.spark.sql.delta.catalog.DeltaCatalog") \
.getOrCreate()
# S3 bucket paths (replace with your bucket)
RAW_PATH = "s3://my-company-datalake/raw/sales/"
SILVER_PATH = "s3://my-company-datalake/silver/sales_clean/"
# 2. Read raw JSON from S3 (Bronze layer)
raw_df = spark.read \
.option("multiLine", True) \
.json(RAW_PATH)
print(f"Loaded {raw_df.count():,} raw records")
# 3. Clean and transform (Silver layer)
clean_df = raw_df \
.filter(col("sale_amount") > 0) \
.filter(col("customer_id").isNotNull()) \
.withColumn("sale_date", to_date(col("sale_ts"))) \
.withColumn("year", year(col("sale_date"))) \
.withColumn("month", month(col("sale_date"))) \
.select(
"customer_id", "product_id", "sale_date",
"sale_amount", "year", "month"
)
# 4. Write to S3 as partitioned Delta table (Silver layer)
clean_df.write \
.format("delta") \
.mode("append") \
.partitionBy("year", "month") \
.save(SILVER_PATH)
print(f"Wrote {clean_df.count():,} clean records to Delta on S3")
# 5. Optimise the Delta table (run periodically)
dt = DeltaTable.forPath(spark, SILVER_PATH)
spark.sql(f"""
OPTIMIZE delta.`${SILVER_PATH}`
ZORDER BY (customer_id, sale_date)
""")
# The Delta table is now queryable via Amazon Athena with:
# SELECT * FROM delta_lake_table WHERE year=2024 AND month=3Key Concepts Explained
Master these terms and you'll understand 80% of the conversations in this field.
Amazon S3
Simple Storage Service — object storage with virtually unlimited capacity. The standard data lake storage layer. Spark accesses it via the s3:// (EMR) or s3a:// (open-source Spark) URI scheme.
Amazon EMR
Elastic MapReduce — a managed cluster service that provisions and auto-scales Spark (and Hadoop) clusters on EC2 instances. You submit jobs via the EMR console, CLI, or SDK.
AWS Glue
A serverless ETL service powered by Apache Spark. No cluster to manage — you write PySpark code and AWS runs it. Includes a Data Catalog (metadata store) and job scheduler.
Glue DynamicFrame
AWS Glue's enhanced DataFrame that handles schema inconsistencies (different types across files). Can be converted to/from a Spark DataFrame with toDF() / fromDF().
Amazon Athena
Serverless SQL query engine that reads data directly from S3 (Parquet, Delta, JSON, CSV). Pay per TB scanned. Perfect for ad-hoc analytics without loading data into a database.
IAM Role
Identity and Access Management — defines what AWS resources your Spark job can access. Your EMR/Glue job assumes an IAM role that grants it read/write access to specific S3 buckets.
Spot Instances
AWS EC2 instances available at up to 90% discount that can be reclaimed with 2 minutes notice. Using Spot Instances for Spark worker nodes dramatically reduces EMR job costs.
Medallion Architecture
A data organisation pattern: Bronze (raw data, as-is), Silver (cleaned, standardised), Gold (business-level aggregates, star schema). Each layer improves data quality and query performance.
Your Spark on AWS Learning Path
Follow these steps in order — each one builds on the last. Designed for complete beginners.
- 1
AWS Fundamentals
Create an AWS free tier account. Learn S3 basics (create bucket, upload files, set permissions). Understand IAM roles and policies.
- 2
PySpark on Your Laptop
Be comfortable writing PySpark jobs locally before moving to the cloud. The code is nearly identical — only the file paths change (local → s3://).
- 3
S3 + PySpark
Configure Spark to read from S3 locally using the hadoop-aws JAR. Practice reading CSV, JSON, and Parquet from S3.
- 4
Your First EMR Job
Create an EMR cluster, upload a PySpark script to S3, and submit it as an EMR Step. View the output in S3.
- 5
AWS Glue ETL
Create a Glue job in the console. Write PySpark with DynamicFrames. Use the Glue Data Catalog to register your tables.
- 6
Athena & Query Layer
Point Athena at your processed S3 data. Write SQL queries. Connect to QuickSight or a BI tool for dashboards.
- 7
Cost Optimisation
Switch EMR workers to Spot Instances. Use S3 Intelligent-Tiering for storage costs. Right-size clusters based on CloudWatch metrics.
Ready to master Spark on AWS?
Explore our free tutorials, hands-on code examples, and interview questions. No sign-up. No paywalls. Forever free.