Skip to content

Troubleshooting Socket Timeout Issue on SageMaker Processing Job

1

Hello -

I would appreciate some input/guidance to help me understand why my SageMaker processing job is failing, despite being able to run the container without issue from the terminal in my SageMaker notebook (and having the same IAM, Subnet and security group configurations)

I’m exploring using SageMaker processing jobs for a project, and am currently experimenting with a much simpler toy example (generate 10 random numbers and publish as an SNS notification). The script I’m running and the docker file can be found below. I’m able to execute the container without issue from the terminal in my SageMaker notebook (docker run -t -i test). However, it fails when being run as a SageMaker processing job (after having pushed the container to ECR). The exact error messages can be found below - it appears to be a socket timeout error related to establishing the SNS boto3 client. I’ve confirmed the IAM role, subnet and security group are all identical between my sagemaker notebook and the sagemaker processing job and am unsure how to proceed to better understand or address this issue.

Docker File

FROM public.ecr.aws/amazonlinux/amazonlinux:latest

RUN yum update -y && \
yum install -y python3-pip

RUN pip3 install boto3==1.35.54

ADD test.py /
ENTRYPOINT ["python3", "/test.py"]

Python Script

import boto3
from random import randint

randos = [randint(1, 100) for i in range(10)]
config = Config(connect_timeout=5, retries = { 'max_attempts': 2})

sns_client = boto3.client('sns', region_name = <<REGION>>, config = config)
sns_client.publish(
         TopicArn = <<ARN>>,
         Message = f"""This message was sent via a SageMaker processing job.\n Here are 10 random numbers: {randos}""",
         Subject ='Test',
)

Error Message (from CloudWatch Logs)

2024-12-24T22:29:35.241Z
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/urllib3/connection.py", line 174, in _new_conn
    conn = connection.create_connection(
  File "/usr/local/lib/python3.9/site-packages/urllib3/util/connection.py", line 95, in create_connection
    raise err
  File "/usr/local/lib/python3.9/site-packages/urllib3/util/connection.py", line 85, in create_connection
    sock.connect(sa)

Traceback (most recent call last): File "/usr/local/lib/python3.9/site-packages/urllib3/connection.py", line 174, in _new_conn conn = connection.create_connection( File "/usr/local/lib/python3.9/site-packages/urllib3/util/connection.py", line 95, in create_connection raise err File "/usr/local/lib/python3.9/site-packages/urllib3/util/connection.py", line 85, in create_connection sock.connect(sa)
2024-12-24T22:29:35.241Z
socket.timeout: timed out

socket.timeout: timed out
2024-12-24T22:29:35.241Z
During handling of the above exception, another exception occurred:

During handling of the above exception, another exception occurred:
2024-12-24T22:29:35.241Z
Traceback (most recent call last):
  File "/usr/local/lib/python3.9/site-packages/botocore/httpsession.py", line 464, in send
    urllib_response = conn.urlopen(
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 802, in urlopen
    retries = retries.increment(
  File "/usr/local/lib/python3.9/site-packages/urllib3/util/retry.py", line 527, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/usr/local/lib/python3.9/site-packages/urllib3/packages/six.py", line 770, in reraise
    raise value
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 716, in urlopen
    httplib_response = self._make_request(
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 404, in _make_request
    self._validate_conn(conn)
  File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 1061, in _validate_conn
    conn.connect()
  File "/usr/local/lib/python3.9/site-packages/urllib3/connection.py", line 363, in connect
    self.sock = conn = self._new_conn()
  File "/usr/local/lib/python3.9/site-packages/urllib3/connection.py", line 179, in _new_conn
    raise ConnectTimeoutError(

Traceback (most recent call last): File "/usr/local/lib/python3.9/site-packages/botocore/httpsession.py", line 464, in send urllib_response = conn.urlopen( File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 802, in urlopen retries = retries.increment( File "/usr/local/lib/python3.9/site-packages/urllib3/util/retry.py", line 527, in increment raise six.reraise(type(error), error, _stacktrace) File "/usr/local/lib/python3.9/site-packages/urllib3/packages/six.py", line 770, in reraise raise value File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 716, in urlopen httplib_response = self._make_request( File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 404, in _make_request self._validate_conn(conn) File "/usr/local/lib/python3.9/site-packages/urllib3/connectionpool.py", line 1061, in _validate_conn conn.connect() File "/usr/local/lib/python3.9/site-packages/urllib3/connection.py", line 363, in connect self.sock = conn = self._new_conn() File "/usr/local/lib/python3.9/site-packages/urllib3/connection.py", line 179, in _new_conn raise ConnectTimeoutError(
2024-12-24T22:29:35.241Z
urllib3.exceptions.ConnectTimeoutError: (<botocore.awsrequest.AWSHTTPSConnection object at 0x7fc71a403c10>, 'Connection to sns.<<REGION>>.amazonaws.com timed out. (connect timeout=60)')

urllib3.exceptions.ConnectTimeoutError: (<botocore.awsrequest.AWSHTTPSConnection object at 0x7fc71a403c10>, 'Connection to sns.<<REGION>>.amazonaws.com timed out. (connect timeout=60)')
2024-12-24T22:29:35.241Z
During handling of the above exception, another exception occurred:

During handling of the above exception, another exception occurred:
2024-12-24T22:29:35.242Z
Traceback (most recent call last):
  File "/test.py", line 17, in <module>
    send_sns_notification(<<ARN>>, 
  File "/test.py", line 8, in send_sns_notification
    sns_client.publish(
  File "/usr/local/lib/python3.9/site-packages/botocore/client.py", line 569, in _api_call
    return self._make_api_call(operation_name, kwargs)
  File "/usr/local/lib/python3.9/site-packages/botocore/client.py", line 1005, in _make_api_call
    http, parsed_response = self._make_request(
  File "/usr/local/lib/python3.9/site-packages/botocore/client.py", line 1029, in _make_request
    return self._endpoint.make_request(operation_model, request_dict)
  File "/usr/local/lib/python3.9/site-packages/botocore/endpoint.py", line 119, in make_request
    return self._send_request(request_dict, operation_model)
  File "/usr/local/lib/python3.9/site-packages/botocore/endpoint.py", line 200, in _send_request
    while self._needs_retry(
  File "/usr/local/lib/python3.9/site-packages/botocore/endpoint.py", line 360, in _needs_retry
    responses = self._event_emitter.emit(
  File "/usr/local/lib/python3.9/site-packages/botocore/hooks.py", line 412, in emit
    return self._emitter.emit(aliased_event_name, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/botocore/hooks.py", line 256, in emit
    return self._emit(event_name, kwargs)
  File "/usr/local/lib/python3.9/site-packages/botocore/hooks.py", line 239, in _emit
    response = handler(**kwargs)
  File "/usr/local/lib/python3.9/site-packages/botocore/retryhandler.py", line 207, in __call__
    if self._checker(**checker_kwargs):
  File "/usr/local/lib/python3.9/site-packages/botocore/retryhandler.py", line 284, in __call__
    should_retry = self._should_retry(
  File "/usr/local/lib/python3.9/site-packages/botocore/retryhandler.py", line 320, in _should_retry
    return self._checker(attempt_number, response, caught_exception)
  File "/usr/local/lib/python3.9/site-packages/botocore/retryhandler.py", line 363, in __call__
    checker_response = checker(
  File "/usr/local/lib/python3.9/site-packages/botocore/retryhandler.py", line 247, in __call__
    return self._check_caught_exception(
  File "/usr/local/lib/python3.9/site-packages/botocore/retryhandler.py", line 416, in _check_caught_exception
    raise caught_exception
  File "/usr/local/lib/python3.9/site-packages/botocore/endpoint.py", line 279, in _do_get_response
    http_response = self._send(request)
  File "/usr/local/lib/python3.9/site-packages/botocore/endpoint.py", line 383, in _send
    return self.http_session.send(request)
  File "/usr/local/lib/python3.9/site-packages/botocore/httpsession.py", line 499, in send
    raise ConnectTimeoutError(endpoint_url=request.url, error=e)

Traceback (most recent call last): File "/test.py", line 17, in <module> send_sns_notification('', File "/test.py", line 8, in send_sns_notification sns_client.publish( File "/usr/local/lib/python3.9/site-packages/botocore/client.py", line 569, in _api_call return self._make_api_call(operation_name, kwargs) File "/usr/local/lib/python3.9/site-packages/botocore/client.py", line 1005, in _make_api_call http, parsed_response = self._make_request( File "/usr/local/lib/python3.9/site-packages/botocore/client.py", line 1029, in _make_request return self._endpoint.make_request(operation_model, request_dict) File "/usr/local/lib/python3.9/site-packages/botocore/endpoint.py", line 119, in make_request return self._send_request(request_dict, operation_model) File "/usr/local/lib/python3.9/site-packages/botocore/endpoint.py", line 200, in _send_request while self._needs_retry( File "/usr/local/lib/python3.9/site-packages/botocore/endpoint.py", line 360, in _needs_retry responses = self._event_emitter.emit( File "/usr/local/lib/python3.9/site-packages/botocore/hooks.py", line 412, in emit return self._emitter.emit(aliased_event_name, **kwargs) File "/usr/local/lib/python3.9/site-packages/botocore/hooks.py", line 256, in emit return self._emit(event_name, kwargs) File "/usr/local/lib/python3.9/site-packages/botocore/hooks.py", line 239, in _emit response = handler(**kwargs) File "/usr/local/lib/python3.9/site-packages/botocore/retryhandler.py", line 207, in __call__ if self._checker(**checker_kwargs): File "/usr/local/lib/python3.9/site-packages/botocore/retryhandler.py", line 284, in __call__ should_retry = self._should_retry( File "/usr/local/lib/python3.9/site-packages/botocore/retryhandler.py", line 320, in _should_retry return self._checker(attempt_number, response, caught_exception) File "/usr/local/lib/python3.9/site-packages/botocore/retryhandler.py", line 363, in __call__ checker_response = checker( File "/usr/local/lib/python3.9/site-packages/botocore/retryhandler.py", line 247, in __call__ return self._check_caught_exception( File "/usr/local/lib/python3.9/site-packages/botocore/retryhandler.py", line 416, in _check_caught_exception raise caught_exception File "/usr/local/lib/python3.9/site-packages/botocore/endpoint.py", line 279, in _do_get_response http_response = self._send(request) File "/usr/local/lib/python3.9/site-packages/botocore/endpoint.py", line 383, in _send return self.http_session.send(request) File "/usr/local/lib/python3.9/site-packages/botocore/httpsession.py", line 499, in send raise ConnectTimeoutError(endpoint_url=request.url, error=e)
2024-12-24T22:29:35.244Z
botocore.exceptions.ConnectTimeoutError: Connect timeout on endpoint URL: "https://sns.<<REGION>>.amazonaws.com/"

botocore.exceptions.ConnectTimeoutError: Connect timeout on endpoint URL: "https://sns.<<REGION>>.amazonaws.com/"

asked 2 years ago910 views

2 Answers
4

Hello.

Looking at the log, the connection to the following Amazon SNS service endpoint times out.
Is a NAT Gateway or Amazon SNS VPC endpoint set up in the VPC to which SageMaker Job is connected?
Without these settings, you will not be able to access Amazon SNS from the SageMaker job and a timeout error will occur.
By the way, even if an interface VPC endpoint is set, the connection will fail if HTTPS is not allowed in the VPC endpoint's security group.

botocore.exceptions.ConnectTimeoutError: Connect timeout on endpoint URL: "https://sns.<<REGION>>.amazonaws.com/"

https://docs.aws.amazon.com/sagemaker/latest/dg/process-vpc.html#process-vpc-nat

  • Connect to other AWS services – If your model needs access to an AWS service that supports interface Amazon VPC endpoints, create an endpoint to connect to that service. For a list of services that support interface endpoints, see AWS services that integrate with AWS PrivateLink in the AWS PrivateLink User Guide. For information about creating an interface VPC endpoint, see Access an AWS service using an interface VPC endpoint in the AWS PrivateLink User Guide.
  • Connect to resources over the internet – If your models are running on instances in an Amazon VPC that does not have a subnet with access to the internet, the models won't have access to resources on the internet. If your model needs access to an AWS service that doesn't support interface VPC endpoints, or to a resource outside of AWS, ensure that you are running your models in a private subnet that has access to the internet using a public NAT gateway in a public subnet. After you have your models running in the private subnet, configure your security groups and network access control lists (NACLs) to allow outbound connections from the private subnet to the public NAT gateway in the public subnet. For information, see NAT gateways in the Amazon VPC User Guide.
EXPERT

answered 2 years ago

EXPERT

reviewed 2 years ago

  • Riku, thank you for your detailed and insightful response! I posted my answer before I saw your expert guidance. As an AWS expert and top contributor, you’ve highlighted critical points about NAT Gateway and VPC endpoints for Amazon SNS, especially ensuring HTTPS is allowed in the endpoint’s security group.

    I hope my response complements yours by adding CLI commands and troubleshooting steps. It’s great to collaborate and provide a well-rounded solution for the user. Your expertise is always appreciated!

1

Greeting

Hi Re-Post User,

Thank you for sharing the details of your issue. I understand how challenging it can be to troubleshoot these kinds of errors, especially when the setup works perfectly in one environment but not in another. Let’s work through this together and get your SageMaker processing job running smoothly. 😊


Clarifying the Issue

You’re looking for guidance on why your SageMaker processing job is failing. You’re exploring using SageMaker processing jobs for a project and currently experimenting with a simpler toy example. While you’re able to execute the Docker container without issue from the terminal in your SageMaker notebook, it fails when run as a SageMaker processing job after pushing the container to ECR.

The failure appears to be a socket timeout error related to establishing the SNS boto3 client. You’ve confirmed that the IAM role, subnet, and security group are identical between your SageMaker notebook and the SageMaker processing job, and you’re unsure how to proceed to better understand or address the issue.


Key Terms

  • SageMaker Processing Job: A managed AWS service that allows you to run custom data processing scripts using prebuilt or custom containers.
  • Socket Timeout: A network error that occurs when a connection attempt exceeds the allowed time limit without a response.
  • SNS boto3 Client: The AWS SDK for Python that facilitates interactions with Amazon Simple Notification Service (SNS).

The Solution (Our Recipe)

Steps at a Glance:

  1. Confirm outbound internet connectivity from the SageMaker processing job.
  2. Verify security group settings to allow outbound HTTPS traffic.
  3. Check the network settings for private subnets and ensure routing through a NAT Gateway.
  4. Test the job in a public subnet to isolate the issue.
  5. Ensure the IAM role has permissions for sns:Publish.

Step-by-Step Guide:

  1. Confirm outbound internet connectivity from the SageMaker processing job
    • SageMaker processing jobs do not automatically have outbound internet access if running in a private subnet without a NAT Gateway. Ensure the processing job has internet connectivity to reach the SNS service endpoint.

  1. Verify security group settings to allow outbound HTTPS traffic

    • Check the security group associated with the SageMaker processing job and ensure it permits outbound traffic on port 443 (HTTPS).

    Example AWS CLI command to check security group rules:

    aws ec2 describe-security-groups --group-ids <your-security-group-id>

  1. Check the network settings for private subnets and ensure routing through a NAT Gateway

    • If your processing job is in a private subnet, confirm that the subnet has a route to the internet via a NAT Gateway. Update the route table in the VPC as necessary.

    Example AWS CLI command to describe route tables:

    aws ec2 describe-route-tables --filters "Name=association.subnet-id,Values=<your-subnet-id>"

    If the route table lacks a NAT Gateway entry, add it:

    aws ec2 create-route --route-table-id <route-table-id> \
        --destination-cidr-block 0.0.0.0/0 --gateway-id <nat-gateway-id>

  1. Test the job in a public subnet to isolate the issue

    • Temporarily configure the processing job to run in a public subnet. If this resolves the issue, it confirms that the problem is related to network settings or internet access.

    Example Python script using boto3 to test SNS connectivity in a public subnet:

    import boto3
    
    sns_client = boto3.client('sns', region_name='us-east-1')  # Replace with your region
    try:
        response = sns_client.list_topics()
        print("Connection successful:", response)
    except Exception as e:
        print("Connection failed:", str(e))

  1. Ensure the IAM role has permissions for sns:Publish

    • Verify that the IAM role used by the SageMaker processing job includes permissions for the sns:Publish action. Attach or update the necessary policy.

    Example AWS CLI command to attach a policy:

    aws iam attach-role-policy --role-name <your-role-name> \
        --policy-arn arn:aws:iam::aws:policy/AmazonSNSFullAccess

Closing Thoughts

Your issue likely stems from network connectivity restrictions in the SageMaker processing environment. By ensuring proper internet access and validating IAM permissions, you should be able to resolve the socket timeout error. If you need further clarification or run into additional challenges, feel free to reach out—I’m here to help! 😊


Farewell

Best of luck with your SageMaker experiment! Let me know how it goes or if you need more assistance. 🚀✨


Cheers,

Aaron 😊

answered 2 years ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.