Gaining granular visibility into application-level costs on Amazon EMR on Amazon Elastic Compute Cloud (Amazon EC2) clusters presents an opportunity for customers looking for ways to further optimize resource utilization and implement fair cost allocation and chargeback models. By breaking down the usage of individual applications running in your EMR cluster, you can unlock several benefits:
- Informed workload management – Application-level cost insights empower organizations to prioritize and schedule workloads effectively. Resource allocation decisions can be made with a better understanding of cost implications, potentially improving overall cluster performance and cost-efficiency.
- Cost optimization – With granular cost attribution, organizations can identify cost-saving opportunities for individual applications. They can right-size underutilized resources or prioritize optimization efforts for applications that are driving high usage and costs.
- Transparent billing – In multi-tenant environments, organizations can implement fair and transparent cost allocation models based on individual application resource consumption and associated costs. This fosters accountability and enables accurate chargebacks to tenants.
In this post, we guide you through deploying a comprehensive solution in your Amazon Web Services (AWS) environment to analyze Amazon EMR on EC2 cluster usage. By using this solution, you will gain a deep understanding of resource consumption and associated costs of individual applications running on your EMR cluster. This will help you optimize costs, implement fair billing practices, and make informed decisions about workload management, ultimately enhancing the overall efficiency and cost-effectiveness of your Amazon EMR environment. This solution has been only tested on Spark workloads running on EMR on EC2 that uses YARN as its resource manager. It hasn’t been tested on workloads from other frameworks that run on YARN, such as HIVE or TEZ.
Solution overview
The solution works by running a Python script on the EMR cluster’s primary node to collect metrics from the YARN resource manager and correlate them with cost usage details from the AWS Cost and Usage Reports (AWS CUR). The script activated by a cronjob makes HTTP requests to the YARN resource manager to collect two types of metrics from paths /ws/v1/cluster/metrics
for cluster metrics and /ws/v1/cluster/apps
for application metrics. The cluster metrics contain utilization information of cluster resources, and the application metrics contain utilization information of an application or job. These metrics are stored in an Amazon Simple Storage Service (Amazon S3) bucket.
There are two YARN metrics that capture the resource utilization information of an application or job.
- memorySeconds – This is the memory (in MB) allocated to an application times the number of seconds the application ran
- vcoreSeconds – This is the number of YARN vcores allocated to an application times the number of seconds application ran
The solution uses memorySeconds to derive the cost of running the application or job. It can be modified to use vcoreSeconds instead if necessary.
The metadata of the YARN metrics collected in Amazon S3 is created, stored, and represented as database and tables in AWS Glue Data Catalog, which is in turn available to Amazon Athena for further processing. You can now write SQL queries in Athena to correlate the YARN metrics with the cost usage information from AWS CUR to derive the detailed cost breakdown of your EMR cluster by infrastructure and application. This solution creates two corresponding Athena views of the respective cost breakdown that will become the data source to Amazon QuickSight for visualization.
The following diagram shows the solution architecture.
Prerequisites
To perform the solution, you need the following prerequisites:
- Confirm that a CUR is created in your AWS account. It needs an S3 bucket to store the report files. Follow the steps described in Creating Cost and Usage Reports to create the CUR on the AWS Management Console. When creating the report, make sure the following settings are enabled:
-
- Include resource IDs
- Time granularity is set to hourly
- Report data integration to Athena
It can take up to 24 hours for AWS to start delivering reports to your S3 bucket. Thereafter, your CUR gets updated at least one time a day.
- The solution needs Athena to run queries against the data from the CUR using standard SQL. To automate and streamline the integration of Athena with CUR, AWS provides an AWS CloudFormation template, crawler-cfn.yml, which is automatically generated in the same S3 bucket during CUR creation. Follow the instructions in Setting up Athena using AWS CloudFormation templates to integrate Athena with the CUR. This template will create an AWS Glue database that references to the CUR, an AWS Lambda event and an AWS Glue crawler that gets invoked by S3 event notification to update the AWS Glue database whenever the CUR gets updated.
- Make sure to activate the AWS generated cost allocation tag,
aws:elasticmapreduce:job-flow-id
. This enables the field,resource_tags_aws_elasticmapreduce_job_flow_id
, in the CUR to be populated with the EMR cluster ID and is used by the SQL queries in the solution. To activate the cost allocation tag from the management console, follow these steps:- Sign in to the payer account’s AWS Management Console and open the AWS Billing and Cost Management console
- In the navigation pane, choose Cost Allocation Tags
- Under AWS generated cost allocation tags, choose the
aws:elasticmapreduce:job-flow-id
tag - Choose Activate. It can take up to 24 hours for tags to activate.
The following screenshot shows an example of the aws:elasticmapreduce:job-flow-id
tag being activated.
You can now test out this solution on an EMR cluster in a lab environment. If you’re not already familiar with EMR, follow the detailed instructions provided in Tutorial: Getting started with Amazon EMR to launch a new EMR cluster and run a sample Spark job.
Deploying the solution
To deploy the solution, follow the steps in the next sections.
Installing scripts to the EMR cluster
Download two scripts from the GitHub repository and save them into an S3 bucket:
emr_usage_report.py
– Python script that makes the HTTP requests to YARN Resource Manageremr_install_report.sh
– Bash script that creates a cronjob to run the python script every minute
To install the scripts, add a step to the EMR cluster through the console or AWS Command Line Interface (AWS CLI) using aws emr add-step
command.
Replace:
REGION
with the AWS Regions where the cluster is running (for example, Europe (Ireland)eu-west-1
)MY-BUCKET
with the name of the bucket where the script is stored (for example,my.artifact.bucket
)MY_REPORT_BUCKET
with the bucket name where you want to collect YARN metrics (for example,my.report.bucket
)
You can now run some Spark jobs on your EMR cluster to start generating application usage metrics.
Launching the CloudFormation stack
When the prerequisites are met and you have the scripts deployed so that your EMR clusters are sending YARN metrics to an S3 bucket, the rest of the solution can be deployed using CloudFormation.
Before launching the stack, upload a copy of this QuickSight definition file into an S3 bucket required by the CloudFormation template to build the initial analysis in QuickSight. When ready, proceed to launch your stack to provision the remaining resources of the solution.
This automatically launches AWS CloudFormation in your AWS account with a template. It prompts you to sign in as needed and make sure you create the stack in your intended Region.
The CloudFormation stack requires a few parameters, as shown in the following screenshot.
The following table describes the parameters.
Parameter | Description |
Stack name | A meaningful name for the stack; for example, EMRUsageReport |
S3 configuration | |
YARNS3BucketName |
Name of S3 bucket where YARN metrics are stored |
Cost Usage Report configuration | |
CURDatabaseName |
Name of Cost Usage Report database in AWS Glue |
CURTableName |
Name of Cost Usage Report table in AWS Glue |
AWS Glue Database configuration | |
EMRUsageDBName |
Name of AWS Glue database to be created for the EMR Cost Usage Report |
EMRInfraTableName |
Name of AWS Glue table to be created for infrastructure usage metrics |
EMRAppTableName |
Name of AWS Glue table to be created for application usage metrics |
QuickSight configuration | |
QSUserName |
Name of QuickSight user in default namespace to manage the EMR Usage Report resources in QuickSight. |
QSDefinitionsFile |
S3 URI of the definition JSON file for the EMR Usage Report. |
- Enter the parameter values from the preceding table.
- Choose Next.
- On the next screen, enter any necessary tags, an AWS Identity and Access Management (IAM) role, stack failure, or advanced options if necessary. Otherwise, you can leave them as default.
- Choose Next.
- Review the details on the final screen and select the check boxes confirming AWS CloudFormation might create IAM resources with custom names or require
CAPABILITY_AUTO_EXPAND
. - Choose Create.
The stack will take a couple of minutes to create the remaining resources for the solution. After the CloudFormation stack is created, on the Outputs tab, you can find the details of the resources created.
Reviewing the correlation results
The CloudFormation template creates two Athena views containing the correlated cost breakdown details of the YARN cluster and application metrics with the CUR. The CUR aggregates cost hourly and therefore correlation to derive the cost of running an application is prorated based on the hourly running cost of the EMR cluster.
The following screenshot shows the Athena view for the correlated cost breakdown details of YARN cluster metrics.
The following table describes the fields in the Athena view for YARN cluster metrics.
Field | Type | Description |
cluster_id |
string | ID of the cluster. |
family |
string | Resource type of the cluster. Possible values are compute instance, elastic map reduce instance, storage and data transfer. |
billing_start |
timestamp | Start billing hour of the resource. |
usage_type |
string | A specific type or unit of the resource such as BoxUsage:m5.xlarge of compute instance. |
cost |
string | Cost associated with the resource. |
The following screenshot shows the Athena view for the correlated cost breakdown details of YARN application metrics.
The following table describes the fields in the Athena view for YARN application metrics.
Field | Type | Description |
cluster_id |
string | ID of the cluster |
id |
string | Unique identifier of the application run |
user |
string | User name |
name |
string | Name of the application |
queue |
string | Queue name from YARN resource manager |
finalstatus |
string | Final status of application |
applicationtype |
string | Type of the application |
startedtime |
timestamp | Start time of the application |
finishedtime |
timestamp | End time of the application |
elapsed_sec |
double | Time taken to run the application |
memoryseconds |
bigint | The memory (in MB) allocated to an application times the number of seconds the application ran |
vcoreseconds |
int | The number of YARN vcores allocated to an application times the number of seconds application ran |
total_memory_mb_avg |
double | Total amount of memory (in MB) available to the cluster in the hour |
memory_sec_cost |
double | Derived unit cost of memoryseconds |
application_cost |
double | Derived cost associated with the application based on memoryseconds |
total_cost |
double | Total cost of resources associated with the cluster for the hour |
Building your own visualization
In QuickSight, the CloudFormation template creates two datasets that reference Athena views as data sources and a sample analysis. The sample analysis has two sheets, EMR Infra Spend
and EMR App Spend
. They have a prepopulated bar chart and pivot tables to demonstrate how you can use the datasets to build your own visualization to present the cost breakdown details of your EMR clusters.
EMR Infra Spend
sheet references to the YARN cluster metrics dataset. There is a filter for date range selection and a filter for cluster ID selection. The sample bar chart shows the consolidated cost breakdown of the resources for each cluster during the period. The pivot table breaks them down further to show their daily expenditure.
The following screenshot shows the EMR Infra Spend
sheet from sample analysis created by the CloudFormation template.
EMR App Spend
sheet references to the YARN application metrics. There is a filter for date range selection and a filter for cluster ID selection. The pivot table in this sheet shows how you can use the fields in the dataset to present the cost breakdown details of the cluster by users to observe the applications that were run, whether they were completed successfully or not, the time and duration of each run, and the derived cost of the run.
The following screenshot shows the EMR App Spend
sheet from sample analysis created by the CloudFormation template.
Cleanup
If you no longer need the resources you created during this walkthrough, delete them to prevent incurring additional charges. To clean up your resources, complete the following steps:
- On the CloudFormation console, delete the stack that you created using the template
- Terminate the EMR cluster
- Empty or delete the S3 bucket used for YARN metrics
Conclusion
In this post, we discussed how to implement a comprehensive cluster usage reporting solution that provides granular visibility into the resource consumption and associated costs of individual applications running on your Amazon EMR on EC2 cluster. By using the power of Athena and QuickSight to correlate YARN metrics with cost usage details from your Cost and Usage Report, this solution empowers organizations to make informed decisions. With these insights, you can optimize resource allocation, implement fair and transparent billing models based on actual application usage, and ultimately achieve greater cost-efficiency in your EMR environments. This solution will help you unlock the full potential of your EMR cluster, driving continuous improvement in your data processing and analytics workflows while maximizing return on investment.
About the authors
Boon Lee Eu is a Senior Technical Account Manager at Amazon Web Services (AWS). He works closely and proactively with Enterprise Support customers to provide advocacy and strategic technical guidance to help plan and achieve operational excellence in AWS environment based on best practices. Based in Singapore, Boon Lee has over 20 years of experience in IT & Telecom industries.
Kyara Labrador is a Sr. Analytics Specialist Solutions Architect at Amazon Web Services (AWS) Philippines, specializing in big data and analytics. She helps customers in designing and implementing scalable, secure, and cost-effective data solutions, as well as migrating and modernizing their big data and analytics workloads to AWS. She is passionate about empowering organizations to unlock the full potential of their data.
Vikas Omer is the Head of Data & AI Solution Architecture for ASEAN at Amazon Web Services (AWS). With over 15 years of experience in the data and AI space, he is a seasoned leader who leverages his expertise to drive innovation and expansion in the region. Vikas is passionate about helping customers and partners succeed in their digital transformation journeys, focusing on cloud-based solutions and emerging technologies.
Lorenzo Ripani is a Big Data Solution Architect at AWS. He is passionate about distributed systems, open source technologies and security. He spends most of his time working with customers around the world to design, evaluate and optimize scalable and secure data pipelines with Amazon EMR.