Amazon EMR Serverless Operators

Amazon EMR Serverless is a serverless option in Amazon EMR that makes it easy for data analysts and engineers to run open-source big data analytics frameworks without configuring, managing, and scaling clusters or servers. You get all the features and benefits of Amazon EMR without the need for experts to plan and manage clusters.

Prerequisite Tasks

To use these operators, you must do a few things:

Operators

Create an EMR Serverless Application

You can use EmrServerlessCreateApplicationOperator to create a new EMR Serverless Application. This operator can be run in deferrable mode by passing deferrable=True as a parameter. This requires the aiobotocore module to be installed.

tests/system/amazon/aws/example_emr_serverless.py

emr_serverless_app = EmrServerlessCreateApplicationOperator(
    task_id="create_emr_serverless_task",
    release_label="emr-6.6.0",
    job_type="SPARK",
    config={"name": "new_application"},
)

Start an EMR Serverless Job

You can use EmrServerlessStartJobOperator to start an EMR Serverless Job. This operator can be run in deferrable mode by passing deferrable=True as a parameter. This requires the aiobotocore module to be installed.

tests/system/amazon/aws/example_emr_serverless.py

start_job = EmrServerlessStartJobOperator(
    task_id="start_emr_serverless_job",
    application_id=emr_serverless_app_id,
    execution_role_arn=role_arn,
    job_driver=SPARK_JOB_DRIVER,
    configuration_overrides=SPARK_CONFIGURATION_OVERRIDES,
)

Open Application UIs

The operator can also be configured to generate one-time links to the application UIs and Spark stdout logs by passing the enable_application_ui_links=True as a parameter. Once the job starts running, these links are available in the Details section of the relevant Task. If enable_application_ui_links=False then the links will be present but grayed out.

You need to ensure you have the following IAM permissions to generate the dashboard link.

"emr-serverless:GetDashboardForJobRun"

If Amazon S3 or Amazon CloudWatch logs are enabled for EMR Serverless, links to the respective console will also be available in the task logs and task Details.

Stop an EMR Serverless Application

You can use EmrServerlessStopApplicationOperator to stop an EMR Serverless Application. This operator can be run in deferrable mode by passing deferrable=True as a parameter. This requires the aiobotocore module to be installed.

tests/system/amazon/aws/example_emr_serverless.py

stop_app = EmrServerlessStopApplicationOperator(
    task_id="stop_application",
    application_id=emr_serverless_app_id,
    force_stop=True,
)

Delete an EMR Serverless Application

You can use EmrServerlessDeleteApplicationOperator to delete an EMR Serverless Application. This operator can be run in deferrable mode by passing deferrable=True as a parameter. This requires the aiobotocore module to be installed.

tests/system/amazon/aws/example_emr_serverless.py

delete_app = EmrServerlessDeleteApplicationOperator(
    task_id="delete_application",
    application_id=emr_serverless_app_id,
)

Sensors

Wait on an EMR Serverless Job state

To monitor the state of an EMR Serverless Job you can use EmrServerlessJobSensor.

tests/system/amazon/aws/example_emr_serverless.py

wait_for_job = EmrServerlessJobSensor(
    task_id="wait_for_job",
    application_id=emr_serverless_app_id,
    job_run_id=start_job.output,
    # the default is to wait for job completion, here we just wait for the job to be running.
    target_states={*EmrServerlessHook.JOB_SUCCESS_STATES, "RUNNING"},
)

Wait on an EMR Serverless Application state

To monitor the state of an EMR Serverless Application you can use EmrServerlessApplicationSensor.

tests/system/amazon/aws/example_emr_serverless.py

wait_for_app_creation = EmrServerlessApplicationSensor(
    task_id="wait_for_app_creation",
    application_id=emr_serverless_app_id,
)

Was this entry helpful?