diff --git a/README.md b/README.md index 41d99ca4c96..840ea3b1802 100644 --- a/README.md +++ b/README.md @@ -125,6 +125,23 @@ Aws.config[:credentials] = Aws::Credentials.new( For more information on how to configure credentials, see the developer guide for [configuring AWS SDK for Ruby](https://docs.aws.amazon.com/sdk-for-ruby/v3/developer-guide/setup-config.html). +## Eager loading + +The SDK normally loads service constants when your application first uses them. +Applications that want to load SDK constants during startup can opt in after +requiring their service gems: + +```ruby +require 'aws-sdk-s3' +require 'aws-sdk-sqs' + +Aws.eager_load! +``` + +`Aws.eager_load!` loads SDK constants from gems that were required before the +call. It does not make any AWS service requests. Requiring only the service +gems your application uses limits the code loaded into memory. + ## API Clients Construct a service client to make API calls. Each client provides a 1-to-1 diff --git a/gems/aws-sdk-core/CHANGELOG.md b/gems/aws-sdk-core/CHANGELOG.md index f7cc093acff..18730bb888d 100644 --- a/gems/aws-sdk-core/CHANGELOG.md +++ b/gems/aws-sdk-core/CHANGELOG.md @@ -1,6 +1,8 @@ Unreleased Changes ------------------ +* Feature - Add `Aws.eager_load!` to eagerly resolve constants registered by loaded AWS SDK gems. + 3.271.1 (2026-11-20) ------------------ diff --git a/gems/aws-sdk-core/lib/aws-sdk-core.rb b/gems/aws-sdk-core/lib/aws-sdk-core.rb index ca061fbbbe5..91c5a2df0b9 100644 --- a/gems/aws-sdk-core/lib/aws-sdk-core.rb +++ b/gems/aws-sdk-core/lib/aws-sdk-core.rb @@ -153,6 +153,35 @@ def empty_connection_pools! end end + # Eager loads SDK constants from gems that have already been required. + # + # Call this method during application startup, after requiring the service + # gems your application uses: + # + # require 'aws-sdk-s3' + # require 'aws-sdk-sqs' + # + # Aws.eager_load! + # + # Service gems required after this call are not eager loaded. This method + # loads Ruby code but does not make any AWS service requests. Eager loading + # can increase memory usage, so applications should require only the + # service gems they use. + # + # @return [void] + def eager_load! + Aws.constants.each do |name| + namespace = Aws.const_get(name) + next unless namespace.is_a?(Module) + + namespace.constants.each do |constant| + namespace.const_get(constant) + end + end + + nil + end + # @api private def eager_autoload!(*args) msg = 'Aws.eager_autoload is no longer needed, usage of '\ diff --git a/gems/aws-sdk-core/sig/aws-sdk-core.rbs b/gems/aws-sdk-core/sig/aws-sdk-core.rbs index 4dbe99710a7..eaf09294533 100644 --- a/gems/aws-sdk-core/sig/aws-sdk-core.rbs +++ b/gems/aws-sdk-core/sig/aws-sdk-core.rbs @@ -4,4 +4,6 @@ module Aws def self.config=: (Hash[Symbol, untyped] config) -> Hash[Symbol, untyped] def self.use_bundled_cert!: () -> String + + def self.eager_load!: () -> nil end diff --git a/gems/aws-sdk-core/spec/aws_spec.rb b/gems/aws-sdk-core/spec/aws_spec.rb index 961dc77c280..3a935a281ad 100644 --- a/gems/aws-sdk-core/spec/aws_spec.rb +++ b/gems/aws-sdk-core/spec/aws_spec.rb @@ -58,4 +58,19 @@ module Aws expect(conn_pool.size).to eq(0) end end + + describe '.eager_load!' do + it 'resolves constants under loaded SDK namespaces' do + service = Module.new + stub_const('Aws::TestService', service) + + allow(Aws).to receive(:constants).and_return([:TestService]) + allow(service).to receive(:constants).and_return([:Client]) + expect(Aws).to receive(:const_get) + .with(:TestService).and_call_original + expect(service).to receive(:const_get).with(:Client) + + expect(Aws.eager_load!).to be_nil + end + end end