How to make the AWS SDK much smaller

Only need a small portion of the AWS SDK, and wish you could make it a lot smaller?

Joel Clermont
Joel Clermont
2023-10-03

You don't always need to install an SDK to integrate with a third-party service, but sometimes it actually is a good idea to use the vendor-provided SDK. For example, if you tried to integrate with an AWS service purely with Laravel's Http client, it would probably be more work than you'd want to take on.

Last time I looked, there were hundreds of Amazon services covered by their SDK, but what if you only need to use two or three of them? Do you really need to install that giant SDK, most of which you'll never use?

The answer is no! Thankfully, Amazon provides an option to "slim down" their SDK by providing some extra configuration in your composer.json file.

By adding one script definition, and specifying the services you intend to use, the SDK will delete unnecessary modules when you run composer install, keeping your dependencies much smaller.

Here's an example of what that looks like:

{
    "require": {
        "aws/aws-sdk-php": "<version here>"
    },
    "scripts": {
        "pre-autoload-dump": "Aws\\Script\\Composer\\Composer::removeUnusedServices"
    },
    "extra": {
        "aws/aws-sdk-php": [
            "Ec2",
            "CloudWatch"
        ]
    }
}

Without this configuration, the vendor/aws folder was 37.5 MB and contained 3,749 files! After "slimming" it down to just EC2 and CloudWatch, it was only 5.4 MB with 457 files. Quite a nice improvement!

To read more about this, check out Amazon's documentation. A similar process exists for the Google SDK.

Here to help,

Joel

P.S. Validation is really important in your app, but it doesn't always get the attention it deserves. We've written the ultimate guide to Laravel validation rules to help you get the most out of this powerful feature.

Toss a coin in the jar if you found this helpful.
Want a tip like this in your inbox every weekday? Sign up below 👇🏼

Level up your Laravel skills!

Each 2-minute email has real-world advice you can use.