How to specify metadata_service_timeout during boto client creation?

Viewed 902

I'm using the following way to create a botoclient

def create_boto_client(self, service_name: str):
    return boto3.session.Session().client(
        service_name=service_name
    )
    # or I can do: return boto3.client(service_name)

How I could set the "metadata_service_timeout" and "metadata_service_num_attempts" values?

I need to specify them, because otherwise boto can fail to fetch credentials in IAM role from Metadata server.

I read in doc that it's possible to specify this value like this:

 # ~/.aws/config
[default]
metadata_service_timeout = 5.0
metadata_service_num_attempts = 10

but I don't have an ability to add this file to instance.

1 Answers

You can use env variables:

  • AWS_METADATA_SERVICE_TIMEOUT: The number of seconds before a connection to the instance metadata service should time out. When attempting to retrieve credentials on an EC2 instance that has been configured with an IAM role, a connection to the instance metadata service will time out after 1 second by default. If you know you are running on an EC2 instance with an IAM role configured, you can increase this value if needed.

  • AWS_METADATA_SERVICE_NUM_ATTEMPTS: When attempting to retrieve credentials on an EC2 instance that has been configured with an IAM role, boto3 will only make one attempt to retrieve credentials from the instance metadata service before giving up. If you know your code will be running on an EC2 instance, you can increase this value to make boto3 retry multiple times before giving up.

To set them from inside python:

os.environ["AWS_METADATA_SERVICE_TIMEOUT"] = "5.0"
os.environ["AWS_METADATA_SERVICE_NUM_ATTEMPTS"] = "10"
Related