Amazon EMR on Amazon EKS¶
Amazon EMR on EKS provides a deployment option for Amazon EMR that allows you to run open-source big data frameworks on Amazon EKS.
Prerequisite Tasks¶
To use these operators, you must do a few things:
Create necessary resources using AWS Console or AWS CLI.
Install API libraries via pip.
pip install 'apache-airflow[amazon]'
Detailed information is available Installation
Operators¶
Create an Amazon EMR EKS virtual cluster¶
The EmrEksCreateClusterOperator
will create an Amazon EMR on EKS virtual cluster.
The example DAG below shows how to create an EMR on EKS virtual cluster.
To create an Amazon EMR cluster on Amazon EKS, you need to specify a virtual cluster name, the eks cluster that you would like to use , and an eks namespace.
Refer to the EMR on EKS Development guide for more details.
create_emr_eks_cluster = EmrEksCreateClusterOperator(
task_id="create_emr_eks_cluster",
virtual_cluster_name="emr_eks_virtual_cluster",
eks_cluster_name="eks_cluster",
eks_namespace="eks_namespace",
)
Submit a job to an Amazon EMR virtual cluster¶
Note
This example assumes that you already have an EMR on EKS virtual cluster configured. See the EMR on EKS Getting Started guide for more information.
The EmrContainerOperator
will submit a new job to an Amazon EMR on Amazon EKS virtual cluster
The example job below calculates the mathematical constant Pi
. In a
production job, you would usually refer to a Spark script on Amazon Simple Storage Service (S3).
To create a job for Amazon EMR on Amazon EKS, you need to specify your virtual cluster ID, the release of Amazon EMR you want to use, your IAM execution role, and Spark submit parameters.
You can also optionally provide configuration overrides such as Spark, Hive, or Log4j properties as well as monitoring configuration that sends Spark logs to Amazon S3 or Amazon Cloudwatch.
In the example, we show how to add an applicationConfiguration
to use the AWS Glue data catalog
and monitoringConfiguration
to send logs to the /aws/emr-eks-spark
log group in Amazon CloudWatch.
Refer to the EMR on EKS guide
for more details on job configuration.
JOB_DRIVER_ARG = {
"sparkSubmitJobDriver": {
"entryPoint": "local:///usr/lib/spark/examples/src/main/python/pi.py",
"sparkSubmitParameters": "--conf spark.executors.instances=2 --conf spark.executors.memory=2G --conf spark.executor.cores=2 --conf spark.driver.cores=1", # noqa: E501
}
}
CONFIGURATION_OVERRIDES_ARG = {
"applicationConfiguration": [
{
"classification": "spark-defaults",
"properties": {
"spark.hadoop.hive.metastore.client.factory.class": "com.amazonaws.glue.catalog.metastore.AWSGlueDataCatalogHiveClientFactory", # noqa: E501
},
}
],
"monitoringConfiguration": {
"cloudWatchMonitoringConfiguration": {
"logGroupName": "/aws/emr-eks-spark",
"logStreamNamePrefix": "airflow",
}
},
}
We pass the virtual_cluster_id
and execution_role_arn
values as operator parameters, but you
can store them in a connection or provide them in the DAG. Your AWS region should be defined either
in the aws_default
connection as {"region_name": "us-east-1"}
or a custom connection name
that gets passed to the operator with the aws_conn_id
parameter. The operator returns the Job ID of the job run.
job_starter = EmrContainerOperator(
task_id="start_job",
virtual_cluster_id=str(create_emr_eks_cluster.output),
execution_role_arn=JOB_ROLE_ARN,
release_label="emr-6.3.0-latest",
job_driver=JOB_DRIVER_ARG,
configuration_overrides=CONFIGURATION_OVERRIDES_ARG,
name="pi.py",
wait_for_completion=False,
)
Sensors¶
Wait on an Amazon EMR virtual cluster job¶
To wait on the status of an Amazon EMR virtual cluster job to reach a terminal state, you can use
EmrContainerSensor
job_waiter = EmrContainerSensor(
task_id="job_waiter",
virtual_cluster_id=str(create_emr_eks_cluster.output),
job_id=str(job_starter.output),
)