The issue is that the AWSserviceManagerProvider.php file is being loaded before the database connection is established during the migration process. To solve this, you can move the code that retrieves the database connectivity details from AWS secret manager to the database.php configuration file.
Here's an example of how you can do this:
- Open the config/database.php file.
- Replace the database configuration values with placeholders. For example:
'mysql' => [
'driver' => 'mysql',
'host' => '{DB_HOST}',
'port' => '{DB_PORT}',
'database' => '{DB_DATABASE}',
'username' => '{DB_USERNAME}',
'password' => '{DB_PASSWORD}',
'unix_socket' => env('DB_SOCKET', ''),
'charset' => 'utf8mb4',
'collation' => 'utf8mb4_unicode_ci',
'prefix' => '',
'strict' => true,
'engine' => null,
],
- In the AWSserviceManagerProvider.php file, retrieve the database connectivity details from AWS secret manager and replace the placeholders in the database configuration array with the actual values. For example:
use Illuminate\Support\Facades\Config;
// Retrieve the database connectivity details from AWS secret manager
$databaseDetails = getDatabaseDetailsFromAwsSecretManager();
// Replace the placeholders in the database configuration array with the actual values
Config::set('database.connections.mysql.host', $databaseDetails['host']);
Config::set('database.connections.mysql.port', $databaseDetails['port']);
Config::set('database.connections.mysql.database', $databaseDetails['database']);
Config::set('database.connections.mysql.username', $databaseDetails['username']);
Config::set('database.connections.mysql.password', $databaseDetails['password']);
- Now, when you run the migration command, Laravel will first load the database configuration file and replace the placeholders with the actual values before establishing the database connection.
Note: Make sure to handle any errors that may occur when retrieving the database connectivity details from AWS secret manager.