How do I troubleshoot Amazon EMR connectivity issues with external databases?
I want to troubleshoot connectivity issues between my Amazon EMR cluster and external databases.
Short description
If your Amazon EMR cluster fails to connect to external databases because of network, security, and configuration issues, then you receive one of the following error messages:
- "Connection refused"
- "Communications link failure"
- "ClassNotFoundException"
- "IO Error: Received fatal alert: handshake_failure"
Note: If you have connectivity issues between your Amazon EMR cluster and AWS services, then see How do I troubleshoot Amazon EMR connectivity issues with AWS services?
Resolution
Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshooting errors for the AWS CLI. Also, make sure that you're using the most recent AWS CLI version.
Resolve connectivity error messages for Amazon EMR on EC2 clusters
Your Amazon EMR cluster fails to connect to an Amazon Relational Database Service (Amazon RDS) for MySQL or Amazon Aurora MySQL-Compatible database for one of the following reasons:
- If the database port is blocked or the database isn't running, then you receive a "Connection refused" error message.
- If you have a network interruption or a timeout occurs during the connection, then you receive a "Communications link failure" error message.
- If the executor nodes don't include the MySQL Java Database Connectivity (JDBC) driver JAR, then you receive a "ClassNotFoundException" error message.
To resolve these errors, configure the MySQL driver for Aurora connectivity.
Complete the following steps:
-
To create the project directories and download the MySQL JDBC driver, run the following commands:
mkdir -p ~/emr-jdbc-test/{scripts,libs} cd ~/emr-jdbc-test/libs wget https://repo1.maven.org/maven2/mysql/mysql-connector-java/8.0.28/mysql-connector-java-8.0.28.jar -
To upload the JDBC driver to your Amazon Simple Storage Service (Amazon S3) bucket, run the following s3 cp AWS CLI command:
aws s3 cp mysql-connector-java-8.0.28.jar s3://YOUR-BUCKET/libs/mysql-connector-j-8.0.28.jarNote: Replace YOUR-BUCKET with your Amazon S3 bucket name.
-
To connect to the Amazon RDS for MySQL database with pyspark, run the following code:
from pyspark.sql import SparkSession spark = SparkSession.builder \ .appName("RDS_MySQL_Connection") \ .config("spark.jars", "s3://YOUR-BUCKET/libs/mysql-connector-j-8.0.28.jar") \ .getOrCreate() jdbc_url = "jdbc:mysql://YOUR-RDS-ENDPOINT:PORT/DATABASE_NAME" df = spark.read \ .format("jdbc") \ .option("url", jdbc_url) \ .option("dbtable", "YOUR_TABLE") \ .option("user", "YOUR_USERNAME") \ .option("password", "YOUR_PASSWORD") \ .option("driver", "com.mysql.cj.jdbc.Driver") \ .load()Note: Replace YOUR-BUCKET with your S3 bucket name, YOUR-RDS-ENDPOINT with your Amazon RDS endpoint, PORT with your database port, and DATABASE_NAME with your database name. Replace YOUR_TABLE with your table name, YOUR_USERNAME with your database username, and YOUR_PASSWORD with your database password.
-
To submit the Spark job with the JDBC driver, run the following spark-submit command:
spark-submit \ --conf spark.executor.cores=1 \ --conf spark.executor.memory=4g \ --conf spark.driver.cores=1 \ --conf spark.driver.memory=4g \ --jars s3://YOUR-BUCKET/libs/mysql-connector-j-8.0.28.jarNote: Replace YOUR-BUCKET with your S3 bucket name.
-
To test network connectivity from the Amazon EMR primary node, run the following command:
nc -vz YOUR-RDS-ENDPOINT PORTNote: Replace YOUR-RDS-ENDPOINT with your Amazon RDS endpoint and PORT with your database port.
Important: Always use --jars or spark.jars to distribute the driver JAR to all executor nodes.
Resolve the "Connection refused" error message for EMR Serverless
Amazon EMR Serverless applications require explicit network configuration to connect to databases. If you didn't configure Amazon Virtual Private Cloud (Amazon VPC), then the application can't establish connections. If your EMR Serverless application fails to connect to a database, then you receive the "Connection refused" error message.
Important: EMR Serverless supports only private subnets. Don't use public subnets. It's a best practice to use private subnets with a NAT gateway for internet-routed services or VPC endpoints for AWS services.
To resolve this error, take the following actions.
Check your current network configuration
- To check your current network configuration, run the following get-application command:
Note: Replace YOUR_APPLICATION_ID with your EMR Serverless application ID.aws emr-serverless get-application --application-id YOUR_APPLICATION_ID
In the output, confirm that the networkConfiguration section includes your subnetIds and securityGroupIds. If the networkConfiguration field is missing or empty, then the application doesn't have a VPC configuration. - To add network configuration to your EMR Serverless application, run the following update-application command:
Note: Replace YOUR_APPLICATION_ID with your EMR Serverless application ID, subnet-EXAMPLE1 and subnet-EXAMPLE2 with your subnet IDs, and sg-EXAMPLE with your security group ID.aws emr-serverless update-application \ --application-id YOUR_APPLICATION_ID \ --network-configuration '{ "subnetIds": ["subnet-EXAMPLE1", "subnet-EXAMPLE2"], "securityGroupIds": ["sg-EXAMPLE"] }'
Configure EMR Serverless security groups
To configure the EMR Serverless security group and the Amazon RDS security group to allow traffic between the two services, complete the following steps:
- Open the Amazon VPC console.
- In the navigation pane, choose Security groups.
- Select the security group associated with your EMR Serverless application.
- Choose Actions, and then choose Edit outbound rules.
- Choose Add rule.
For Type, choose Custom TCP.
For Port range, enter 3306 for MySQL or Aurora, 1521 for Oracle, or 5432 for PostgreSQL.
For Destination, enter the Amazon RDS security group ID. - Choose Save rules.
- Choose Actions, Edit inbound rules.
- Choose Add rule.
For Type, choose Custom TCP.
For Port range, enter the same port as the outbound rule.
For Source, enter the same EMR Serverless security group ID as a self-reference. - Choose Save rules.
- Select the Amazon RDS security group.
- Choose Actions, and then choose Edit inbound rules.
- Choose Add rule.
For Type, choose Custom TCP.
For Port range, enter 3306 for MySQL or Aurora, 1521 for Oracle, or 5432 for PostgreSQL.
For Source, enter the EMR Serverless security group ID. - Choose Save rules.
Plan subnets for EMR Serverless
Each EMR Serverless worker consumes one IP address. If your application can scale to 4,000 vCPU with 4 vCPU workers, then you must have 1,000 available IP addresses. Choose subnets across at least two Availability Zones with sufficient IP addresses.
Connect from SageMaker AI Studio to EMR Serverless
If connections fail from Amazon SageMaker AI to EMR Serverless, then confirm the following:
- The EMR Serverless application is in the same VPC or a peered VPC.
- The SageMaker AI execution role has emr-serverless:* permissions.
- You turned on pre-initialized capacity if required by your configuration.
- Security groups allow traffic between SageMaker AI and EMR Serverless.
Resolve the "IO Error: Received fatal alert: handshake_failure" error for Oracle database SSL/TLS handshake failures
If your Amazon EMR cluster fails to connect to an Oracle database over SSL/TLS, then you receive the following error message:
"IO Error: Received fatal alert: handshake_failure"
To resolve the error, turn on SSL debugging. Add the following Spark configuration properties to the spark-defaults.conf file or pass them as --conf parameters when you submit a Spark job:
spark.driver.extraJavaOptions=-Djavax.net.debug=ssl,handshake spark.executor.extraJavaOptions=-Djavax.net.debug=ssl,handshake
Then, to check the Java version on the Amazon EMR primary node, run the following command:
java -version
Note: Java 17.0.18 and later versions removed support for the TLS_RSA_WITH_AES_256_CBC_SHA cipher. If your Oracle database uses this cipher, then update the Amazon RDS configuration.
Update the RDS option group to add cipher suites compatible with newer Java versions. This approach provides compatibility with current and future Java versions and doesn't require Amazon EMR cluster changes.
To configure Oracle connection timeouts when you submit a Spark job, add the following properties to the spark-defaults.conf file or pass them as --conf parameters:
--conf spark.driver.extraJavaOptions="-Doracle.net.CONNECT_TIMEOUT=60000 -Doracle.jdbc.ReadTimeout=120000" \ --conf spark.executor.extraJavaOptions="-Doracle.net.CONNECT_TIMEOUT=60000 -Doracle.jdbc.ReadTimeout=120000"
Then, to test the Oracle database connectivity, run the following command:
telnet YOUR-ORACLE-ENDPOINT 1521
Note: Replace YOUR-ORACLE-ENDPOINT with your Oracle database endpoint.
Configure cross-account database connectivity
Amazon EMR clusters that connect to databases in other AWS accounts require additional network configuration.
To configure cross-account connectivity, complete the following steps:
- Create a VPC peering connection between your accounts, or attach the VPCs on both accounts to AWS Transit Gateway.
- Update route tables in both VPCs to route traffic through the peering connection or Transit Gateway.
- Confirm that the Amazon RDS security group in the target account allows inbound traffic from the Amazon EMR VPC CIDR range.
Note: You must use VPC peering to support cross-account security group references. - Confirm that DNS resolution works across accounts. Use the Amazon RDS endpoint hostname instead of the private IP address, because IP addresses can change.
- To isolate the issue, launch an Amazon Elastic Compute Cloud (Amazon EC2) instance with the same VPC, subnet, and security group as your Amazon EMR application. Then, run the following command to test connectivity:
Note: Replace CROSS-ACCOUNT-RDS-ENDPOINT with your Amazon RDS endpoint in the target account.telnet CROSS-ACCOUNT-RDS-ENDPOINT 3306
Note: If the Amazon EC2 instance connects but Amazon EMR doesn't, then you must reconfigure Amazon EMR rather than network connectivity. To identify the root cause, review the Amazon EMR cluster logs and security group configuration. For more information, see Troubleshoot Amazon EMR clusters.
Related information
Working with Amazon EMR-managed security groups
Configuring VPC access for EMR Serverless applications to connect to data
- Topics
- Analytics
- Tags
- Amazon EMR
- Language
- English

Relevant content
asked 4 years ago
asked 2 years ago
asked 3 years ago
AWS OFFICIALUpdated 2 years ago