Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Aws Sdk Php Symfony Laravel Package

aws/aws-sdk-php-symfony

Symfony bundle integrating the AWS SDK for PHP. Install via Composer, register AwsBundle, and configure AWS clients/services through Symfony container parameters or env vars, with optional config validation/merging and support for instance profile credentials.

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation:

    composer require aws/aws-sdk-php-symfony
    

    For Symfony 6+ (Flex-compatible), the package auto-registers. For older versions, add to config/bundles.php:

    return [
        // ...
        Aws\Symfony\AwsBundle::class => ['all' => true],
    ];
    
  2. First Use Case: Inject the S3 client in a controller/service:

    use Aws\S3\S3Client;
    
    class UploadController
    {
        public function __construct(private S3Client $s3Client) {}
    
        public function upload()
        {
            $this->s3Client->putObject([
                'Bucket' => 'my-bucket',
                'Key' => 'file.txt',
                'Body' => fopen('file.txt', 'r'),
            ]);
        }
    }
    

    Symfony’s autowiring handles the S3Client injection automatically.

  3. Configuration: Add to .env:

    AWS_ACCESS_KEY_ID=your-key
    AWS_SECRET_ACCESS_KEY=your-secret
    AWS_DEFAULT_REGION=us-east-1
    

    Or use config/packages/aws.yaml:

    aws:
        version: latest
        region: us-west-2
        credentials:
            key: '%env(AWS_ACCESS_KEY_ID)%'
            secret: '%env(AWS_SECRET_ACCESS_KEY)%'
    
  4. Verify Services:

    php bin/console debug:container aws.s3
    

    Output should show the Aws\S3\S3Client service.


Implementation Patterns

Common Workflows

  1. Service Injection:

    // Controller/Service
    public function __construct(
        private Aws\DynamoDb\DynamoDbClient $dynamoDb,
        private Aws\Sqs\SqsClient $sqs
    ) {}
    

    Leverage Symfony’s autowiring for all AWS clients (e.g., S3Client, SNSClient).

  2. Dynamic Configuration: Override per-service settings in config/packages/aws.yaml:

    aws:
        region: us-east-1
        DynamoDb:
            region: eu-west-1  # Override for DynamoDB only
    
  3. Lazy-Loaded Clients: Clients are lazy-loaded by default (since v2.4.0), reducing boot time:

    $this->s3Client->listBuckets(); // Loads only when first used
    
  4. Custom Credentials: Use a service for credentials (e.g., from a secrets manager):

    services:
        app.aws_credentials:
            class: Aws\Credentials\Credentials
            arguments: ['%env(DB_KEY)%', '%env(DB_SECRET)%']
    
    aws:
        Sqs:
            credentials: '@app.aws_credentials'
    
  5. Instance Profile Credentials (EC2):

    aws:
        credentials: ~  # Auto-fetches from EC2 metadata
    

Integration Tips

  • Laravel + Symfony Hybrid Apps: Use the bundle in a Symfony microservice and share the AWS SDK via a shared container (e.g., symfony/dependency-injection).

    // In Laravel, manually create the SDK:
    $sdk = new Aws\Sdk([
        'region' => 'us-east-1',
        'version' => 'latest',
    ]);
    
  • Testing: Mock AWS clients in tests using Symfony’s Test\MockObject\MockObject:

    use Aws\S3\S3Client;
    use PHPUnit\Framework\TestCase;
    
    class UploadTest extends TestCase
    {
        public function testUpload()
        {
            $mock = $this->createMock(S3Client::class);
            $mock->method('putObject')->willReturn(true);
    
            $this->container->set(S3Client::class, $mock);
            // Test logic...
        }
    }
    
  • Event-Driven Workflows: Use AWS SDK events (e.g., Aws\Common\Event\Events) with Symfony’s event dispatcher:

    $sdk->register('before', function ($request) {
        $this->eventDispatcher->dispatch(new AwsEvent($request));
    });
    
  • Retry Logic: Configure retries globally in config/packages/aws.yaml:

    aws:
        retry:
            max_attempts: 3
            modes:
                - standard
    

Gotchas and Tips

Pitfalls

  1. Configuration Merging:

    • Enable validation/merging with AWS_MERGE_CONFIG=true in .env.
    • Gotcha: Merging fails if duplicate non-standard keys are defined. Use ~ to omit or ensure uniqueness.
  2. Service Aliases:

    • Clients are registered as aws.{service} (e.g., aws.s3) and as the class name (e.g., Aws\S3\S3Client).
    • Tip: Prefer class-based injection for clarity:
      // Good: Explicit type
      public function __construct(private S3Client $s3Client) {}
      
      // Avoid: Magic strings
      public function __construct(private ContainerInterface $container) {}
      
  3. Lazy Loading:

    • Clients are lazy-loaded by default (since v2.4.0). If you need eager initialization (e.g., for bootstrapping), use:
      services:
          aws.s3:
              lazy: false
      
  4. PHP 8.5+ Features:

    • The bundle supports PHP 8.5’s typed properties/enums, but some AWS SDK classes may not yet use them. Check the AWS SDK changelog for updates.
  5. Symfony 6+ Deprecations:

    • If using Symfony 6.2+, ensure your config/packages/aws.yaml uses the new services structure:
      framework:
          secret: '%env(APP_SECRET)%'
      aws:
          # ... (rest of config)
      

Debugging

  1. Missing Services:

    • Run php bin/console debug:container aws to list available services.
    • Common fix: Ensure the bundle is registered in config/bundles.php (Symfony <6) or composer.json (Symfony ≥6).
  2. Configuration Errors:

    • Enable debug mode (APP_DEBUG=1) and check logs for Aws\Symfony\Exception\ConfigurationException.
    • Tip: Use AWS_MERGE_CONFIG=true to catch misconfigurations early.
  3. Credential Issues:

    • For EC2 instance profiles, ensure the IAM role has the correct permissions and the instance has an IAM role attached.
    • Debug: Test credentials manually:
      aws sts get-caller-identity --profile your-profile
      
  4. Performance:

    • Lazy-loading reduces boot time, but may cause delays on first use. For critical paths, pre-load clients:
      $this->container->get(S3Client::class); // Force initialization
      

Extension Points

  1. Custom SDK Configuration: Extend the bundle’s configuration by creating a custom compiler pass:

    use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
    use Symfony\Component\DependencyInjection\ContainerBuilder;
    
    class CustomAwsConfigPass implements CompilerPassInterface
    {
        public function process(ContainerBuilder $container)
        {
            $definition = $container->getDefinition('aws.sdk');
            $definition->addMethodCall('configure', [
                ['custom_key' => 'custom_value']
            ]);
        }
    }
    

    Register the pass in services.yaml:

    services:
        App\DependencyInjection\CustomAwsConfigPass:
            tags: [container.compiler_pass]
    
  2. Middleware: Add custom middleware to the SDK:

    $sdk = $this->container->get('aws.sdk');
    $sdk->registerMiddleware(new class implements Aws\Common\Middleware\MiddlewareInterface {
        public function __invoke($request, callable $next)
        {
            // Pre-process request
            $response = $next($request);
            // Post-process response
            return $response;
        }
    });
    
  3. Testing Helpers: Create a base test case for AWS interactions:

    abstract class AwsTestCase extends TestCase
    {
        protected function getMockClient(string $service): object
        {
            $mock = $this->createMock("Aws\\{$service}\\{$service}Client");
            $mock->method('__call')->willReturn(true);
            return $mock;
        }
    
        protected function setMockClient(string $service, object $mock)
        {
            $this->container->set("Aws\\{$service}\\{$service}Client", $mock);
        }
    }
    
  4. Environment-Specific Configs: Use Symfony’s environment-aware configuration:

    #
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor