Skip to content

[Train] Unable to gain long-term access to S3 storage for training state/checkpoints when running on AWS EKS #50823

Closed

Description

@jleben

What happened + What you expected to happen

I am using Ray Train + KubeRay to train on AWS EKS. I have configured EKS to give Ray pods a specific IAM role through Pod Identity Association. EKS Pod Identity Association provides a HTTP endpoint through which an application can "assume said IAM role" - i.e. gain a set of temporary credentials to AWS services. The boto3 library, for example, is able to automatically use this method to obtain credentials without any special user action.

However It seems pyarrow.fs.S3FileSystem is the only (built-in) vehicle for Ray Train to use S3 as storage. Unfortunately, pyarrow.fs.S3FileSystem is unable to automatically gain access through Pod Identity Association. I have filed a report about that in the Apache Arrow repository: apache/arrow#45603

As mentioned in the Apache Arrow issue report, it is possible for the user to manually obtain a set of temporary credentials from Pod Identity Association and pass them them to S3FileSystem in constructor arguments. However, Ray Train will keep using this same instance of S3FileSystem throughout training, during which time the temporary credentials may expire.

It is possible, however, for the user to store the temporary credentials into environment variables AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY and AWS_SESSION_TOKEN and an S3FileSystem created with no arguments will pick up those credentials. Moreover, the user can also keep updating these environment variables with new credentials when an old set expires, and the same instance of S3FileSystem will use the new credentials in every method call.

This methods of manually updating temporary credentials in environment variables is the only way I found that I am able to give Ray Train long-term access to S3 through Pod Identity Association. However, as mentioned in the Apache Arrow ticket, this has its own drawbacks:

  • The environment variables affect the entire Python process - it is not possible to give specific credentials to S3FileSytem.
  • The user needs to go through a significant effort to implement updating the environment variables in the context of a Ray Train session, and it is not clear whether it can be done reliably and safely at all. I have found the environment variables need to be updated in the process where TorchTrainer was created. I have found two ways to do this, neither ideal:
    • The user can do it on a separate thread, potentially risking race conditions with S3FileSytem use by Ray Train.
    • The user can implement a custom ray.tune.Callback, hoping to avoid thread safety issues that way. However:
      • The existing callback methods are all related to the different points in the progression of training and so inherently coupled to the timing of it, rather than the timing constraints of the credential expiry.
      • Moreover, it is still possible Ray Train uses S3FileSystem on a different thread under the hood, so it is not clear whether this is thread safe either.

While this issue could be resolved in pyarrow.fs.S3FileSystem, as suggested in the Apache Arrow bug report, I see possible changes in Ray Train that would alleviate the issue. Specifically:

  • Ray Train could switch to accessing S3 through boto3 instead of pyarrow.fs.S3FileSystem, since recent versions of boto3 provide seamless integration with EKS Pod Identity Association.
  • Alternatively, Ray Train could provide a specific, safe way for the user to periodically update the instance of S3FileSystem used by Ray Train. This way
    1. an update period could be guaranteed independently of the progression of training, and
    2. the user could pass temporary credentials to S3FileSystem constructor arguments, not affecting the rest of the Python process.

It is worth noting that the same issue occurs when using Ray Train in the context where AWS access is provided through SSO and you will find reproduction instruction below for both cases. However, the main blocker for my production environment is using it on AWS EKS, so I am focusing this bug report on that.

Versions / Dependencies

Ray version: 2.42.1

Reproduction script

Alternative 1: KubeRay + EKS

Set up:

  • an EKS cluster
  • an IAM role providing access to an S3 bucket (let's say it's called my-bucket)
  • set up an EKS Pod Identity Association assigning the IAM role to Ray pods (pods in a specific Kubernetes service account)
  • Install KubeRay operator on the EKS cluster.
  • Install a RayCluster on the EKS cluster.

Submit Ray job using entrypoint listed below using ray job submit -- python3 entrypoint.py

Alternative 2: Local Ray Cluster + AWS SSO

Set up access to AWS using an SSO profile - providing access to an S3 bucket (let's say it's called my-bucket)

Run the entrypoint listed below locally using python3 entrypoint.py, letting is create a local Ray cluster on the fly.

Entrypoint (entrypoint.py)

import ray.train.torch
import ray.train
import ray

ray.init()

trainer = ray.train.torch.TorchTrainer(
    lambda: print("Hello"),
    scaling_config=ray.train.ScalingConfig(num_workers=1),
    run_config=ray.train.RunConfig(
        storage_path="s3://my-bucket",
        name="s3fs-bug",
    )
)
result = trainer.fit()

The job errors with this message:

Status message: Job entrypoint command failed with exit code 1, last available logs (truncated to 20,000 chars):
    result = trainer.fit()
  File "/usr/local/lib/python3.10/site-packages/ray/train/base_trainer.py", line 589, in fit
    storage = StorageContext(
  File "/usr/local/lib/python3.10/site-packages/ray/train/_internal/storage.py", line 461, in __init__
    self._create_validation_file()
  File "/usr/local/lib/python3.10/site-packages/ray/train/_internal/storage.py", line 489, in _create_validation_file
    self.storage_filesystem.create_dir(self.experiment_fs_path)
  File "pyarrow/_fs.pyx", line 603, in pyarrow._fs.FileSystem.create_dir
  File "pyarrow/error.pxi", line 91, in pyarrow.lib.check_status
OSError: When testing for existence of bucket 'my-bucket': AWS Error ACCESS_DENIED during HeadBucket operation: No response body.

Issue Severity

Medium: It poses a significant difficulty though I can work around it.

Activity

  1. added
    bug
    triage
    on Feb 22, 2025
  2. changed the title [-][Train] Unable to gain permanent (non-expiring) S3 storage for training state/checkpoints on AWS EKS[/-] [+][Train] Unable to gain long-term access to S3 storage for training state/checkpoints when running on AWS EKS[/+] on Feb 23, 2025
  3. added
    train
    on Feb 23, 2025
  4. jleben commented on Mar 4, 2025

    @jleben
    Author

    I have found a workaround that so far seems safe and reliable, although probably less performant and certainly convoluted and difficult to maintain.

    Specifically, I have implemented a subclass of pyarrow.fs.FileSystemHandler that:

    • Obtains a set of temporary credentials from boto3
    • Instantiates a pyarrow.fs.S3FileSystem internally, passing it the temporary credentials as constructor arguments
    • Forwards all filesystem methods to the pyarrow.fs.S3FileSystem; in each method it first checks whether the credentials are close to expiry, and if they are, fetches new ones and recreates the pyarrow.fs.S3FileSystem, before forwarding the method call to the latter.

    I am attaching the full code of S3FilesystemHandler. There is also a little helper S3FileSystem to help create a PyFileSystem using the said handler, given an S3 URI.

    With this, one can create a pyarrow.fs.PyFileSystem using this handler, and pass it to Ray Train in RunConfig like this:

    fs, path = S3FileSystem.from_uri("s3://bucket/path/")
    ray.train.RunConfig(
            storage_path=path,
            storage_filesystem=fs
    )
    

    Please note the following Python file has a .txt extension added that should be removed, to work around the GitHub limitation.

    s3_filesystem_handler.py.txt

  5. added
    P1
    on Mar 5, 2025
  6. removed
    triage
    on Jul 29, 2025
  7. wingkitlee0 commented on Aug 12, 2025

    @wingkitlee0
    Contributor

    @jleben Thanks for providing this workaround! It seems to be working in my jobs.

    Just some notes:

    • it seems that when using the provided s3_filesystem_handler, we would lose some retry logic for transient errors, compared to the vanilla pyarrow's S3FileSystem (e.g., AWSRetryStrategy.) Ray does have some retries in Ray Data but not in Ray Train ([Train] Allow retries when uploading checkpoint #55350). So one may want to wrap all those APIs with a retry logic (e.g., decorator) to be super safe.
    • From the linked arrow github issues, it seems that the solution is to upgrade pyarrow version to take advantage of the EKS Pod Identity.
  8. jleben commented on Sep 26, 2025

    @jleben
    Author

    This seems to no longer be an issue with Ray 2.49.2 and pyarrow 19.0.1, so I will close this issue.

    For anyone still experiencing this issue, note that another very simple workaround is to use s3fs.S3FileSystem instead of pyarrow.fs.S3FileSystem, since the former support EKS Pod Identity, as well as AWS SSO.

    Specifically, one can wrap an sf3s filesystem in a pyarrow filesystem, then pass the latter to Ray Train in a ray.train.RunConfig:

    fs = pyarrow.fs.PyFileSystem(pyarrow.fs.FSSpecHandler(S3FileSystem()))
    ray.train.RunConfig(storage_filesystem=fs)
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1bugcommunity-backlogtrain

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions