Push Amazon EMR step logs from Amazon EC2 instances to Amazon CloudWatch logs

Centralizing EMR step output in CloudWatch changes the operational model: instead of chasing logs on individual EC2 hosts, teams can query, filter, and correlate Spark step activity from a single control plane. That matters for incident response, but it also introduces a dependency on consistent agent installation, log path coverage, and IAM permissions across every cluster node.

The deployment pattern is straightforward, yet the trade-off is architectural: the CloudWatch agent must be bootstrapped onto the EMR instances and configured to tail the right directories as steps execute. This makes the solution sensitive to cluster lifecycle events, bootstrap timing, and how EMR writes logs on master versus core nodes. If the logging workflow is not aligned with the clusterโ€™s startup sequence, visibility gaps can appear before the agent is fully active.

Step concurrency is a key design constraint. The source explicitly calls out a concurrency level of 1, which means the logging approach should be evaluated against workload parallelism before standardizing it. For teams running multiple steps simultaneously, the log-to-step mapping and troubleshooting workflow may become harder to interpret, so the logging design should be validated against the clusterโ€™s execution model rather than treated as a generic add-on.

There is also an important observability extension point here: once logs are in CloudWatch, they can feed metric filters, alarms, and notification workflows. That creates a path from raw execution logs to automated operations, but it also adds maintainability concerns around naming conventions, log group discovery, and cleanup of transient resources. In practice, the value is highest when logging, alerting, and cluster provisioning are managed together as code.




ย 

Amazonย ย EMRย is a big data service offered by AWS to run Apache Spark and other open-source applications on AWS to build scalable data pipelines in a cost-effective manner.ย Monitoring the logs generated from the jobs deployed on EMR clusters is essential to help detect critical issues in real time and identify root causes quickly.

Pushing those logs intoย Amazon CloudWatchย enables you to centralize and drive actionable intelligence from your logs to address operational issues without needing to provision servers or manage software. You can instantly begin writing queries with aggregations, filters, and regular expressions. In addition, you can visualize time series data, drill down into individual log events, and export query results to CloudWatch dashboards.

To ingest logs that are persisted on theย 

Amazon Elastic Compute Cloud

ย (Amazon EC2) instances of an EMR cluster into CloudWatch, you can use theย CloudWatch agent. This provides a simple way to push logs from an EC2 instance to CloudWatch.

The CloudWatch agent is a software package that autonomously and continuously runs on your servers. You can install and configure the CloudWatch agent to collect system and application logs from EC2 instances, on-premises hosts, and containerized applications. CloudWatch processes and stores the logs collected by the CloudWatch agent, which further helps with the performance and health monitoring of your infrastructure and applications.

In this post, we create an EMR cluster and centralize the EMR step logs of the jobs in CloudWatch.ย This will make it easier for you to manage your EMR cluster, troubleshoot issues, and monitor performance. This solution is particularly helpful if you want to use CloudWatch to collect and visualize real-time logs, metrics, and event data, streamlining your infrastructure and application maintenance.

Overview of solution

The solution presented in this post is based on a specific configuration where the EMR step concurrency level is set to 1. This means that only one step is run at a time on the cluster. Itโ€™s important to note that if the EMR step concurrency level is set to a value greater than 1, the solution may not work as expected. We highly recommend verifying yourย EMR step concurrencyย configuration before implementing the solution presented in this post.

The following diagram illustrates the solution architecture.

Solution Architecture Diagram

ย 
ย 

The workflow includes the following steps:

  1. Users start an Apache Spark EMR job, creating a step on the EMR cluster. Using Apache Spark, the workload is distributed across the different nodes of the EMR cluster.
  2. In each node (EC2 instance) of the cluster, a CloudWatch agent watches different logs directories, capturing new entries in the log files and pushing them to CloudWatch.
  3. Users can view the step logs accessing the different log groups from the CloudWatch console. The step logs written by Amazon EMR are as follows:
    • controllerย โ€” Information about the processing of the step. If your step fails while loading, you can find the stack trace in this log.
    • stderrย โ€” The standard error channel of Spark while it processes the step.
    • stdoutย โ€” The standard output channel of Spark while it processes the step. Note that for Spark applications, the driverโ€™s log output only appears in the step logs for applications run in Client mode. More details can be found in theย EMR Submit Step documentation.

We provide anย AWS CloudFormationย template in this post as a general guide. The template demonstrates how to configure a CloudWatch agent on Amazon EMR to push Spark logs to CloudWatch. You can review and customize it as needed to include your Amazon EMR security configurations. As a best practice, we recommend including your Amazon EMR security configurations in the template toย encrypt data in transit.

You should also be aware that some of the resources deployed by this stack incur costs when they remain in use.

In the next sections, we go through the following steps:

  1. Create and upload the bootstrap script to anย Amazon Simple Storage Serviceย (Amazon S3) bucket.
  2. Use the CloudFormation template to create the following resources:
  3. Monitor the Spark logs on the CloudWatch console.

Prerequisites

This post assumes that you have the following:

  • Anย AWS account.
  • An S3 bucket for storing the bootstrap script.
  • A VPC created inย Amazon Virtual Private Cloudย (Amazon VPC), where your EMR cluster will be launched.
  • Default IAM service roles for Amazon EMR permissions to AWS services and resources. You can create these roles with theย aws emr create-default-rolesย command in theย AWS Command Line Interfaceย (AWS CLI).
  • An optional EC2 key pair, if you plan to connect to your cluster through SSH rather thanย AWS Systems Manager Session Manager.

Create and upload the bootstrap script to an S3 bucket

For more information, seeย Uploading objectsย andย Installing and running the CloudWatch agent on your servers.

To create and the upload the bootstrap script, complete the following steps:

  1. Create a local file namedย bootstrap_cloudwatch_agent.shย with the following content:
    #!/bin/bash
    
    echo -e 'Installing CloudWatch Agent... \n'
    sudo rpm -Uvh --force https://s3.amazonaws.com/amazoncloudwatch-agent/amazon_linux/amd64/latest/amazon-cloudwatch-agent.rpm
    
    echo -e 'Starting CloudWatch Agent... \n'
    sudo amazon-cloudwatch-agent-ctl -a fetch-config -m ec2 -c ssm:AmazonCloudWatch-Config.json -s
  2. On the Amazon S3 console, choose your S3 bucket.
  3. On theย Objectsย tab, chooseย Upload.
  4. Chooseย Add files, then choose the bootstrap script.
  5. Chooseย Upload, then choose the file name:ย bootstrap_cloudwatch_agent.sh.
  6. Chooseย Copy S3 URI. We use this value in a later step.

Provision resources with the CloudFormation template

Chooseย Launch Stackย to launch a CloudFormation stack in your account and deploy the template:

ย 

This template creates an IAM role, IAM instance profile, Systems Manager parameter, and EMR cluster. The cluster starts theย Spark PI estimation example application. You will be billed for the AWS resources used if you create a stack from this template.

The CloudFormation wizard will ask you to modify or provide these parameters:

  • InstanceTypeย โ€“ Theย type of instanceย for all instance groups. The default is m4.xlarge.
  • InstanceCountCoreย โ€“ The number of instances in the core instance group. The default is 2.
  • EMRReleaseLabelย โ€“ Theย Amazon EMR release labelย you want to use. The default is emr-6.9.0.
  • BootstrapScriptPathย โ€“ The S3 path of your CloudWatch agent installation bootstrap script that you copied earlier.
  • Subnetย โ€“ The EC2 subnet where the cluster launches. You must provide this parameter.
  • EC2KeyPairNameย โ€“ An optional EC2 keypair for connecting to cluster nodes, as an alternative to Session Manager.

Monitor the log streams

After the CloudFormation stack deploys successfully, on the CloudWatch console, chooseย Log groupsย in the navigation pane. Then filter the log groups by the prefixย /aws/emr/master.

choose Log groups in the navigation pane

ย 
ย 

The ID in the log group corresponds to the EC2 instance ID of the EMR primary node. If you have multiple EMR clusters, you can use this ID to identify a particular EMR cluster, based on the primary node ID.

In the log group, you will find the three different log streams.

In the log group, you will find the three different log streams.

ย 
ย 

The log streams contain the following information:

  • step-stdoutย โ€“ The standard output channel of Spark while it processes the step.
    The standard output channel of Spark while it processes the step
    ย 
    ย 
  • step-stderrย โ€“ The standard error channel of Spark while it processes the step.
    The standard error channel of Spark while it processes the step.
    ย 
    ย 
  • step-controllerย โ€“ Information about the processing of the step. If your step fails while loading, you can find the stack trace in this log.
    Information about the processing of the step.
    ย 
    ย 

Clean up

To avoid future charges in your account, delete the resources you created in this walkthrough. The EMR cluster will incur charges as long as the cluster is active, so stop it when youโ€™re done.

  1. On the CloudFormation console, in the navigation pane, chooseย Stacks.
  2. Choose the stack you launched (EMR-CloudWatch-Demo), then chooseย Delete.
  3. Empty the S3 bucketย you created.
  4. Delete the S3 bucketย you created.

Conclusion

Now that you have completed the steps in this walkthrough, you have the CloudWatch agent running on your cluster hosts and configured to push EMR step logs to CloudWatch. With this feature, you can effectively monitor the health and performance of your Spark jobs running on Amazon EMR, detecting critical issues in real time and identifying root causes quickly.

You can package and deploy this solution through a CloudFormation template like this example template, which creates the IAM instance profile role, Systems Manager parameter, and EMR cluster.

To take this further, consider using these logs in CloudWatch alarms for alerts on aย log group-metric filter. You could collect them with other alarms into aย composite alarmย or configure alarm actions such as sendingย Amazon Simple Notification Serviceย (Amazon SNS) notifications to trigger event-driven processes such asย AWS Lambdaย functions.

ย 


About the Author

Ennio Pastore is a Senior Data Architect on the AWS Data Lab team. He is an enthusiast of everything related to new technologies that have a positive impact on businesses and general livelihood. Ennio has over 10 years of experience in data analytics. He helps companies define and implement data platforms across industries, such as telecommunications, banking, gaming, retail, and insurance.

ย 

Original Post

Enjoyed this article? Sign up for our newsletter to receive regular insights and stay connected.