Running Spark on EC2

The spark-ec2 script, located in Spark’s ec2 directory, allows you to launch, manage and shut down Spark clusters on Amazon EC2. It automatically sets up Mesos, Spark and HDFS on the cluster for you. This guide describes how to use spark-ec2 to launch clusters, how to run jobs on them, and how to shut them down. It assumes you’ve already signed up for an EC2 account on the Amazon Web Services site.

spark-ec2 is designed to manage multiple named clusters. You can launch a new cluster (telling the script its size and giving it a name), shutdown an existing cluster, or log into a cluster. Each cluster is identified by placing its machines into EC2 security groups whose names are derived from the name of the cluster. For example, a cluster named test will contain a master node in a security group called test-master, and a number of slave nodes in a security group called test-slaves. The spark-ec2 script will create these security groups for you based on the cluster name you request. You can also use them to identify machines belonging to each cluster in the Amazon EC2 Console.

Before You Start

Launching a Cluster

You can also run ./spark-ec2 --help to see more usage options. The following options are worth pointing out:

Running Jobs


You can edit /root/spark/conf/ on each machine to set Spark configuration options, such as JVM options and, most crucially, the amount of memory to use per machine (SPARK_MEM). This file needs to be copied to every machine to reflect the change. The easiest way to do this is to use a script we provide called copy-dir. First edit your file on the master, then run ~/spark-ec2/copy-dir /root/spark/conf to RSYNC it to all the workers.

The configuration guide describes the available configuration options.

Terminating a Cluster

Note that there is no way to recover data on EC2 nodes after shutting them down! Make sure you have copied everything important off the nodes before stopping them.

Pausing and Restarting Clusters

The spark-ec2 script also supports pausing a cluster. In this case, the VMs are stopped but not terminated, so they lose all data on ephemeral disks but keep the data in their root partitions and their persistent-hdfs. Stopped machines will not cost you any EC2 cycles, but will continue to cost money for EBS storage.


If you have a patch or suggestion for one of these limitations, feel free to contribute it!

Using a Newer Spark Version

The Spark EC2 machine images may not come with the latest version of Spark. To use a newer version, you can run git pull to pull in /root/spark to pull in the latest version of Spark from git, and build it using sbt/sbt compile. You will also need to copy it to all the other nodes in the cluster using ~/spark-ec2/copy-dir /root/spark.

Accessing Data in S3

Spark’s file interface allows it to process data in Amazon S3 using the same URI formats that are supported for Hadoop. You can specify a path in S3 as input through a URI of the form s3n://<bucket>/path. You will also need to set your Amazon security credentials, either by setting the environment variables AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY before your program or through SparkContext.hadoopConfiguration. Full instructions on S3 access using the Hadoop input libraries can be found on the Hadoop S3 page.

In addition to using a single input file, you can also use a directory of files as input by simply giving the path to the directory.