ARROW-12810: [Python] Stop AWS SDK from looking for metadata service

This significantly speeds up Python tests.

I opted to put it in Python instead of in the shell script so that interactive local development also benefits.

Closes #10348 from lidavidm/arrow-12810

Authored-by: David Li <>
Signed-off-by: David Li <>
diff --git a/python/pyarrow/tests/ b/python/pyarrow/tests/
index c6e4966..3de07c4 100644
--- a/python/pyarrow/tests/
+++ b/python/pyarrow/tests/
@@ -231,6 +231,16 @@
     return pathlib.Path(__file__).parent / 'data'
+def disable_aws_metadata(monkeypatch):
+    """Stop the AWS SDK from trying to contact the EC2 metadata server.
+    Otherwise, this causes a 5 second delay in tests that exercise the
+    S3 filesystem.
+    """
+    monkeypatch.setenv("AWS_EC2_METADATA_DISABLED", "true")
 # TODO(kszucs): move the following fixtures to once the previous
 # parquet dataset implementation and hdfs implementation are removed.
diff --git a/python/pyarrow/tests/ b/python/pyarrow/tests/
index 1af6967..acdff25 100644
--- a/python/pyarrow/tests/
+++ b/python/pyarrow/tests/
@@ -1020,12 +1020,9 @@
-def test_s3_options(monkeypatch):
+def test_s3_options():
     from pyarrow.fs import S3FileSystem
-    # Avoid wait for unavailable metadata server in ARN role example below
-    monkeypatch.setenv("AWS_EC2_METADATA_DISABLED", "true")
     fs = S3FileSystem(access_key='access', secret_key='secret',
                       session_token='token', region='us-east-2',
                       scheme='https', endpoint_override='localhost:8999')